Hello,
I am trying to modify a custom Conv2D’s custom attribute, but failing as of now.
For example, in the following custom Conv2D which inherits from PyTorch’s nn.Conv2d, I have a ‘count’ attribute that should get counted up each time the forward function in an instantiated Conv2d object is called. However, the value of count resets to 0 (i.e. the printed output is always 0) when the forward function is invoked every time. Can you please point out what I am doing wrong here?
Thanks in advance!
Code snippet:
class Conv2d(nn.Conv2d):
def __init__(self, in_channels, out_channels, kernel_size, stride=1,
padding=0, dilation=1, groups=1, bias=True):
super(Conv2d, self).__init__(in_channels, out_channels, kernel_size, stride,
padding, dilation, groups, bias)
self.count = 0
def forward(self, x):
weight = self.weight
print(self.count) # always prints 0
self.count += 1
return F.conv2d(x, weight, self.bias, self.stride,
self.padding, self.dilation, self.groups)