Caffe中net.layers.blobs和net.params之间的区别是什么?

时间:2017-10-27 08:39:51

标签: python neural-network deep-learning caffe pycaffe

我正在使用Python Caffe,并与net.layers [layer_index] .blobs和net.params [layer_type]混淆。如果我理解的话,net.params包含所有网络参数。以LeNet为例,net.params [' conv1']表示' conv1'的网络系数。层。然后net.layer [layer_index] .blobs应该表示相同。但是,我发现它们并不完全相同。我使用以下代码进行测试:

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)

关于他们之间的区别的任何想法?谢谢。

1 个答案:

答案 0 :(得分:4)

您正在将 trainable 网络参数(存储在net.params中)和输入数据混合到网络中(存储在net.blobs中):
完成模型训练后,net.params将被修复,不会更改。但是,对于您输入网络的每个新输入示例,net.blobs将存储不同图层对该特定输入的响应。