我正在编写一个caffe python图层,它沿特定轴(附加代码)转移[0 255]之间的输入,并且正向传递工作正常。这种图层需要向后传球吗?如果是的话,我该如何实现呢?
caffe_root = 'caffe_root'
import sys
sys.path.insert(0, caffe_root + 'python')
import caffe
import numpy as np
class scale_layer(caffe.Layer):
def setup(self, bottom, top):
assert len(bottom)==1 and len(top)==1, "scale_layer expects a single input and a single output"
def reshape(self, bottom, top):
top[0].reshape(*bottom[0].data.shape)
def forward(self, bottom, top):
in_ = np.array(bottom[0].data)
x_min = in_.min(axis=(0, 1), keepdims=True)
x_max = in_.max(axis=(0, 1), keepdims=True)
top[0].data[...] = np.around(255*((in_-x_min)/(x_max-x_min)))
def backward(self, top, propagate_down, bottom):
# backward pass is not implemented!
???????????????????????????
pass
答案 0 :(得分:1)
如果您愿意忽略np.around
:
对于x=x_min
和x=x_max
,导数为零,对于所有其他x
导数为255/(x_max-x_min)
。
这可以通过
实现def forward(self, bottom, top):
in_ = bottom[0].data
self.x_min = in_.min(axis=(0, 1), keepdims=True) # cache min/max for backward
self.x_max = in_.max(axis=(0, 1), keepdims=True)
top[0].data[...] = 255*((in_-self.x_min)/(self.x_max-self.x_min)))
def backward(self, top, propagate_down, bottom):
in_ = bottom[0].data
b, c = in_.shape[:2]
diff = np.tile( 255/(self.x_max-self.x_min), (b, c, 1, 1) )
diff[ in_ == self.x_min ] = 0
diff[ in_ == self.x_max ] = 0
bottom[0].diff[...] = diff * top[0].diff
不要忘记对此进行数字测试。这可以通过例如test_gradient_for_python_layer
来完成。