你的Python層非常相似的輸入層:它沒有反向傳播,這使得它易於實現。有關更多詳細信息,請參閱this thread。
你的層期望"label"
底部,並製作"H"
矩陣作爲頂部:
層{ 名: 「classWeightH」 底部: 「標籤」 頂部: 「H」 類型: 「蟒」 python_param { 模塊:其中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
非常感謝您的支持和幫助。你總是樂於助人。 –