使用python层Caffe实现Bhattacharyya损失函数 [英] Implement Bhattacharyya loss function using python layer Caffe

查看:226
本文介绍了使用python层Caffe实现Bhattacharyya损失函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用python层caffe来实现我的自定义损失层。我已经将此
谢谢


请注意,表中的p [i]表示第 ith 个输出神经元值。

解决方案

假设 bottom [0] .data p bottom\ [1] .data q Db(p,q)表示 p q 之间的Bhattacharyya距离。



向后函数中唯一需要做的就是相对于其输入计算 Db 的偏导数( p q ),并将它们存储在相应的底部diff Blob中:





< img s rc = https://i.stack.imgur.com/6s5Jl.gif alt = diff_q = dDb(p,q)/ dq>



所以您的向后函数将类似于:

  def向后(自己,顶部,propaly_down,底部):
如果propagate_down [0]:
bottom [0] .diff [...] =#如果property_down [1]:
bottom [1] .diff,计算dDb(p,q)/ dp
[...] =#计算dDb(p,q)/ dq

请注意,您通常使用批次的平均(而不是总数)错误。然后,您将得到这样的结果:

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

def向后(self,top,propaly_down,底部):
,如果propagation_down [0]:
底部[0] .diff [...] =#计算dDb(p,q)/ dp
/底部[0] .num
如果propagate_down [1]:
bottom [1] .diff [...] =#计算dDb(p,q)/ dq
/ bottom [1] .num

一旦计算了 Db 的偏导数,就可以将它们插入到上面的模板中,就像您使用前进传递功能一样。


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.

解决方案

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.

这篇关于使用python层Caffe实现Bhattacharyya损失函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆