您的 Python 层与输入层非常相似:它没有反向传播,因此很容易实现。详情请见this thread。
您的层需要"label" 底部,并生成"H" 矩阵作为顶部:
层{
名称:“classWeightH”
底部:“标签”
顶部:“H”
类型:“Python”
python_param {
module: # python 代码所在的文件名
层:“classWeightHLayer”
}
}
python 代码应该类似于:
import sys, os, numpy as np
sys.path.insert(0, os.environ['CAFFE_ROOT']+'/python')
import caffe
class classWeightHLayer(caffe.Layer):
def setup(self,bottom,top):
assert len(bottom)==1, "expecting exactly one input"
assert len(top)==1, "producing exactly one output"
# you might want to get L - the number of labels as a parameter...
def reshape(self,bottom,top):
top[0].reshape(1,1,L,L) # reshape the output to the size of H
def forward(self,bottom,top):
labels = bottom[0].data
H = np.zeros((1,1,L,L), dtype='f4')
# do your magic here...
top[0].data[...] = H
def backward(self, top, propagate_down, bottom):
# no back-prop for input layers
pass