Caffe python层backword pass实现

Caffe python layer backword pass implementation

我正在编写一个 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

你的功能很简单,如果你愿意忽略np.around:

x=x_minx=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.