This is an example of backward hook applied on a Conv2d. Can someone explain what is being returned by this hook and how it is being calculated?
conv_model.conv2.register_backward_hook: Conv2d(1, 4, kernel_size=(1, 1), stride=(1, 1)) (tensor([[[[0.0112, 0.0112, 0.0112, 0.0112, 0.0112],
[0.0112, 0.0112, 0.0112, 0.0112, 0.0112],
[0.0112, 0.0112, 0.0112, 0.0112, 0.0112],
[0.0112, 0.0112, 0.0112, 0.0112, 0.0112],
[0.0112, 0.0112, 0.0112, 0.0112, 0.0112]]]]), tensor([[[[-0.0459]]],
[[[-0.0650]]],
[[[-0.5905]]],
[[[-0.1576]]]]), tensor([0.1069, 0.1513, 1.3748, 0.3670])) (tensor([[[[0.0043, 0.0043, 0.0043, 0.0043, 0.0043],
[0.0043, 0.0043, 0.0043, 0.0043, 0.0043],
[0.0043, 0.0043, 0.0043, 0.0043, 0.0043],
[0.0043, 0.0043, 0.0043, 0.0043, 0.0043],
[0.0043, 0.0043, 0.0043, 0.0043, 0.0043]],
[[0.0061, 0.0061, 0.0061, 0.0061, 0.0061],
[0.0061, 0.0061, 0.0061, 0.0061, 0.0061],
[0.0061, 0.0061, 0.0061, 0.0061, 0.0061],
[0.0061, 0.0061, 0.0061, 0.0061, 0.0061],
[0.0061, 0.0061, 0.0061, 0.0061, 0.0061]],
[[0.0550, 0.0550, 0.0550, 0.0550, 0.0550],
[0.0550, 0.0550, 0.0550, 0.0550, 0.0550],
[0.0550, 0.0550, 0.0550, 0.0550, 0.0550],
[0.0550, 0.0550, 0.0550, 0.0550, 0.0550],
[0.0550, 0.0550, 0.0550, 0.0550, 0.0550]],
[[0.0147, 0.0147, 0.0147, 0.0147, 0.0147],
[0.0147, 0.0147, 0.0147, 0.0147, 0.0147],
[0.0147, 0.0147, 0.0147, 0.0147, 0.0147],
[0.0147, 0.0147, 0.0147, 0.0147, 0.0147],
[0.0147, 0.0147, 0.0147, 0.0147, 0.0147]]]]),)
Following is my model:
class try_conv_model(nn.Module):
def __init__(self):
super(try_conv_model, self).__init__()
self.conv1 = nn.Conv2d(1, 1, kernel_size=1, bias=False)
self.conv2 = nn.Conv2d(1, 4, kernel_size=1)
self.gap = nn.AdaptiveAvgPool2d((1,1))
def forward(self, x):
print("-"*150)
print("before conv1: ",x)
x = self.conv1(x)
print("before conv2: ",x)
x = self.conv2(x)
print("before AdaptiveAvgPool2D: ",x)
x = self.gap(x)
print("after AdaptiveAvgPool2D: ",x)
x = x.view(-1, self.num_flat_features(x))
return(x)
def num_flat_features(self, x):
size = x.size()[1:] # all dimensions except the batch dimension
num_features = 1
for s in size:
num_features *= s
return num_features
conv_model = try_conv_model()