【问题标题】:[Caffe-CNN]How to get gradients of the output w.r.t. to a convolutional layer?[Caffe-CNN]如何获得输出 w.r.t 的梯度到卷积层?
【发布时间】:2017-08-07 14:34:03
【问题描述】:

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

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 conv 层值的梯度。

非常感谢任何帮助!

【问题讨论】:

    标签: python caffe gradient


    【解决方案1】:

    我通过阅读this postthis discussion 了解了如何做到这一点。这是代码。

    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]
    

    希望这会有所帮助!

    【讨论】:

    猜你喜欢
    • 2022-01-14
    • 2016-11-24
    • 2017-07-24
    • 2017-09-27
    • 2018-08-20
    • 1970-01-01
    • 2015-09-28
    • 2018-06-21
    • 2018-02-10
    相关资源
    最近更新 更多