Pycaffe:如何在 python 层中创建自定义权重?

Pycaffe: How to create custom weights in a python layer?

我已经在网络和 caffe 源代码中搜索了一段时间,没有任何解决方案可言,但在自定义应用程序神经网络中,我正在构建一些 。正向传递和反向传递在功能上运行良好,我可以在我的设置例程中创建自定义权重参数,但尽管我可能会尝试,但我无法让 caffe 为我的图层设置 "official" 权重。这当然会允许更好的快照、更容易的求解器实现等。

知道我在这里遗漏了什么吗?

[编辑:来自如下所示图层的代码。为简洁起见,删除了一些内容。该层的目的是为来自卷积层的扁平化激活过滤器添加颜色]

def setup(self, bottom, top):
    global weights
    self.weights = np.random.random((CHANNELS))

def reshape(self, bottom, top):
    top[0].reshape(1,2*XDIM,2*YDIM)

def forward(self, bottom, top):
    arrSize = bottom[0].data.shape
    #Note: speed up w/ numpy ops for this later...
    for j in range(0, 2*arrSize[1]):
            for k in range(0, 2*arrSize[2]):
                    # Set hue/sat from hueSat table.
                    top[0].data[0,j,k] = self.weights[bottom[0].data[0,int(j/2),int(k/2)]]*239

def backward(self, top, propagate_down, bottom):
    diffs = np.zeros((CHANNELS))
    for i in range(0,300):
            for j in range(0,360):
                    diffs[bottom[0].data[0,i/2,j/2]] = top[0].diff[0,i,j]

    #stand in for future scaling
    self.weights[...] += diffs[...]/4 

是来自未来的我!以下是如何解决您的问题:

最近在 Caffe 中实现了 blob 添加 Python。这是一个执行此操作的示例层:

class Param(caffe.Layer):
    def setup(self, bottom, top):
        self.blobs.add_blob(1,2,3)
        self.blobs[0].data[...] = 0

    def reshape(self, bottom, top):
        top[0].reshape(10)

    def forward(self, bottom, top):
        print(self.blobs[0].data)
        self.blobs[0].data[...] += 1

    def backward(self, top, propagate_down, bottom):
        pass

要访问差异,只需使用 self.blobs[0].diff[...] 即可。求解器将处理其余部分。有关详细信息,请参阅 https://github.com/BVLC/caffe/pull/2944