英文:
How to calculate the gradient with respect to "Input" layer in Caffe?
问题
I want to implement the algorithm proposed by paper "Generalizing to unseen domains via adversarial data augmentation" using Caffe framework. I have to compute the gradient with regard to the input layer to add it onto the input blob. In PyTorch, it can be done by grad = torch.autograd.grad(loss, data)[0]
. But in Caffe, there is no function to do this as far as I know. So how to compute the gradient of the "Input" layer in Caffe? The "Input" layer means the input image in semantic segmentation.
I have tried calling net->input_blobs()[0]->cpu_diff()
after backpropagation, but the values in cpu_diff
are all 0. Obviously, Caffe does not compute the gradient of the input layer by default. The overall algorithm is as the image shows.
英文:
I want to implement the algorithm proposed by paper "Generalizing to unseen domains via adversarial data augmentation" using Caffe framework. I have to compute the gradient with regard of input layer to add it onto the input blob. In PyTorch, it can be done by grad = torch.autograd.grad(loss, data)[0]
. But in Caffe, there is no function to do this as I know. So how to compute the gradient of "Input" layer in Caffe. The "Input" layer means input image in semantic segmentation.
I have tried call net->input_blobs()[0]->cpu_diff()
after backpropagation, but values in cpu_diff
are all 0. Obviously, Caffe does not compute the gradient of input layer in default. The overall algorithm is as the image shows.enter image description here
答案1
得分: 0
要获得你想要的,尝试类似以下代码:
for (int i = 0; i < top_vec[0]->count(); i++) {
top_vec[0]->mutable_cpu_diff()[i] = 1.0;
}
net->Backward(top_vec, propagate_down, bottom_vec);
for (int i = 0; i < bottom_vec[0]->count(); i++) {
std::cout << i << " : " << bottom_vec[0]->cpu_diff()[i] << std::endl;
}
<details>
<summary>英文:</summary>
To get what you want, try something like
for (int i=0; i<top_vec[0]->count(); i++) {
top_vec[0]->mutable_cpu_diff()[i] = 1.0;
}
net->Backward(top_vec, propagate_down, bottom_vec);
for (int i=0; i<bottom_vec[0]->count(); i++) {
std::cout << i << " : " << bottom_vec[0]->cpu_diff()[i] << std::endl;
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论