[Caffe-CNN]如何将输出的梯度 w.r.t. 获取到卷积层?



我正在尝试实现这篇论文 - https://arxiv.org/abs/1610.02391 使用Python。为此,我想获得特定类的输出相对于最后一个卷积层的梯度。我遇到了以下 backward(( 函数的用法。

label = np.zeros((1, 6))
label[0, interested_class] = 1
net.backward(**{net.output[0]: label}) 

假设我的网络中有六个类。

但是,它为输入层提供了梯度w.r.t。

我尝试使用以下用法,但它没有给出理想的输出。

label = np.zeros((1,6))
label[0,interested_class] = 1
net.backward(end=conv, **{net.output[0]:label}) 

准确地说,我想获取输出层 w.r.t卷积层值的梯度。

任何帮助都非常感谢!

我通过阅读这篇文章和这个讨论弄清楚了如何做到这一点。这是代码。

layer_name = 'conv' #Convolutional layer of interest
class_label= 4 # the class of interest
label = np.zeros((1,6))
label[0,interested_class] = 1    
grads= net.backward(diffs= [layer_name], **{net.outputs[0]:diff})
gradients = grads[layer_name]

希望这会有所帮助!

相关内容

最新更新