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

时间:2015-12-28 18:10:19

标签: python caffe pycaffe

我一直在搜索网络和caffe源代码一段时间没有任何解决方案可言,但在自定义应用程序神经网络中,我正在构建一些custom layers in python。向前传球和向后传球在功能上运作良好,我可以在我的设定程序中创建自定义重量参数,但尝试我可能无法获得设置"官方"我的图层的权重。这当然会允许更好的快照,更容易的求解器实现等。

知道我在这里缺少什么吗?

[编辑:下面显示的图层代码。为简洁起见,删除了一些东西。此层的目的是为卷积层中的扁平激活滤镜添加颜色]

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 

1 个答案:

答案 0 :(得分:4)

这是我的未来!以下是解决问题的方法:

最近blob添加在Caffe中实现了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