I am using Python Caffe, and confused with net.layers[layer_index].blobs and net.params[layer_type]. If I understand well, net.params contains all the network parameters. Take the LeNet for example, net.params['conv1'] represents the network coefficients for the 'conv1' layer. Then net.layer[layer_index].blobs should represent the same. However, what I found is that they are not exactly the same. I use the following codes to test it:
def _differ_square_sum(self,blobs):
import numpy as np
gradients = np.sum(np.multiply(blobs[0].diff,blobs[0].diff)) + np.sum(np.multiply(blobs[1].diff,blobs[1].diff))
return gradients
def _calculate_objective(self, iteration, solver):
net = solver.net
params = net.params
params_value_list = list(params.keys())
[print(k,v.data.shape) for k,v in net.blobs.items()]
layer_num = len(net.layers)
j = 0
for layer_index in range(layer_num):
if(len(net.layers[layer_index].blobs)>0):
cur_gradient = self._differ_square_sum(net.layers[layer_index].blobs)
key = params_value_list[j]
cur_gradient2 = self._differ_square_sum(params[key])
print([cur_gradient,cur_gradient2])
assert(cur_gradient == cur_gradient2)
Any ideas on the difference between them? Thanks.
You are mixing the trainable net parameters (stored in net.params
) and the input data to the net (stored in net.blobs
):
Once you are done training the model, net.params
are fixed and will not change. However, for each new input example you are feeding to the net, net.blobs
will store the different layers' response to that particular input.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With