1.
register_forward_hook(hook)
2.
register_backward_hook(hook)
/home/lyu/anaconda2/lib/python2.7/site-packages/torch/nn/modules/module.pyc in __call__(self, *input, **kwargs)
204
205 def __call__(self, *input, **kwargs):
--> 206 result = self.forward(*input, **kwargs)
207 for hook in self._forward_hooks.values():
208 hook_result = hook(self, input, result)
eg.
dic = {}
def get_fea(name):
def hook(m, x, y):
dic[name] = y
return hook
mm.conv1.register_forward_hook(get_fea('conv1'))
..
mm(x)
...
--------------------------reference------------------------------------
1. https://github.com/jacobgil/pytorch-grad-cam/blob/master/grad-cam.py
2. http://blog.csdn.net/u012436149/article/details/69230136
3. https://discuss.pytorch.org/t/why-cant-i-see-grad-of-an-intermediate-variable/94/6 ***
4. https://discuss.pytorch.org/t/why-cant-i-see-grad-of-an-intermediate-variable/94/12