2017-10-18 89 views
0

我正在寫一個caffe python圖層,它沿着特定軸(附加代碼)轉售[0 255]之間的輸入,並且正向傳遞正常工作。這個圖層是否需要向後傳遞?如果是這樣,我該如何執行它?Caffe python圖層底部字符傳遞實現

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

爲什麼np.around?你如何計劃區分這一點? – Shai

+0

您是否想過使用任何解決方法來執行np.around? – Mak

+0

完全忽略它? – Shai

回答

1

你的功能是相當簡單的,如果你願意忽略np.around

enter image description here

對於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 

來實現,不要忘記測試這個numberically。這可以通過例如test_gradient_for_python_layer來完成。

+0

感謝您的回放。 1.我沒有得到最後一行: bottom [0] .diff [...] = diff * top [0] .diff 不應該是:top [0] .diff [... ] = diff * bottom [0] .diff。 另一個問題是如何運行「test_gradient_for_python_layer」?當我運行訓練時應該執行它嗎?你能否提供一些步驟來做到這一點。 – Mak

+0

@Mak top [0] .diff保存頂層diff,你需要乘以它,所以漸變將繼續傳播你的圖層 – Shai

+0

@Mak你應該在PR中有一個exple如何使用梯度測試工具 – Shai