Implement Bhattacharyya loss function using python layer Caffe

爷,独闯天下 提交于 2019-12-12 22:25:28

问题


Trying to implement my custom loss layer using python layer,caffe. I've used this example as the guide and have wrote the forward function as follow:

    def forward(self,bottom,top):
        score = 0;
        self.mult[...] = np.multiply(bottom[0].data,bottom[1].data)
        self.multAndsqrt[...] = np.sqrt(self.mult)
        top[0].data[...] = -math.log(np.sum(self.multAndsqrt))

However, the second task, that is implementing the backward function is kinda much difficult for me as I'm totally unfamiliar with python. So please help me with coding the backward section. Here is the cost function and its derivative for stocashtic gradient decent to be implemented:

Thanks in advance.

Note that p[i] in the table indicates the ith output neuron value.


回答1:


Lets say bottom[0].data is p, bottom\[1].data is q and Db(p,q) denotes the Bhattacharyya Distance between p and q.

The only thing you need to do in your backward function is to compute the partial derivatives of Db with respect to its inputs (p and q), and store them in the respective bottom diff blobs:

So your backward function would look something like:

def backward(self, top, propagate_down, bottom):
    if propagate_down[0]:
        bottom[0].diff[...] = # calculate dDb(p,q)/dp
    if propagate_down[1]:
        bottom[1].diff[...] = # calculate dDb(p,q)/dq

Note that you normally use the average (instead of the total) error of your batch. Then you would end up with something like this:

def forward(self,bottom,top):
    self.mult[...] = np.multiply(bottom[0].data,bottom[1].data)
    self.multAndsqrt[...] = np.sqrt(self.mult)
    top[0].data[...] = -math.log(np.sum(self.multAndsqrt)) / bottom[0].num

def backward(self, top, propagate_down, bottom):
    if propagate_down[0]:
        bottom[0].diff[...] = # calculate dDb(p,q)/dp
                                / bottom[0].num
    if propagate_down[1]:
        bottom[1].diff[...] = # calculate dDb(p,q)/dq
                                / bottom[1].num

Once you calculated the partial derivatives of Db, you can insert them in the templates above as you did for the function of the forward pass.



来源:https://stackoverflow.com/questions/39977133/implement-bhattacharyya-loss-function-using-python-layer-caffe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!