【问题标题】:Pycaffe: How to create custom weights in a python layer?Pycaffe:如何在 python 层中创建自定义权重?
【发布时间】:2016-04-02 14:21:36
【问题描述】:

我一直在搜索网络和 caffe 源代码,但没有任何解决方案可言,但在自定义应用程序神经网络中,我正在构建一些 custom layers in python。前向传递和后向传递在功能上运行良好,我可以在我的设置例程中创建自定义权重参数,但我可能无法让 caffe 为我的层设置“官方”权重。这当然可以实现更好的快照、更简单的求解器实现等。

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

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

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 

【问题讨论】:

标签: python caffe pycaffe


【解决方案1】:

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

最近 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

【讨论】:

  • caffe 的未来如何?
  • 对我来说看起来不错。如果您正在寻找 python 实现,文档实际上不存在/分散到银河系的四个角落,但 caffe 本身非常好。 GoogLeNet 以及其他几个很棒的 convnet 都非常简洁地在其中实现。不过学习曲线非常陡峭。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-20
  • 2016-02-20
  • 1970-01-01
  • 2017-11-29
  • 2020-01-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多