Hi,
I write a subclass of nn.Conv2d, it looks like below:
class strangeConv2d(nn.Conv2d):

    def __init__(self, in_channels, out_channels, kernel_size, stride=1,
                 padding=0, dilation=1, groups=1, bias=True,eps1=1e-6,eps2=1e-3,beta=0.99,showGrad=False):
        nn.Conv2d.__init__(self, in_channels, out_channels, kernel_size, stride=stride,padding=padding, dilation=dilation, groups=groups, bias=bias)
        self.eps1=nn.Parameter(torch.Tensor([eps1]),requires_grad=False)
        self.eps2=nn.Parameter(torch.Tensor([eps2]),requires_grad=False)
        self.beta=nn.Parameter(torch.Tensor([beta]),requires_grad=False)
        ...
        if showGrad:
            self.register_backward_hook(self.confgrad_hook)

    def confgrad_hook(self,Module,grad_in,grad_out):
        with torch.no_grad():
            ...
            print(something)


I use it in my network.  When the net is set to train(), everything works fine, but if you set net=net.eval(), the output become strange. It seems that the batchnorm layer doesn’t work properly.
I tried to make the subclass more simple and remove the backward_hook, the code looks like:
class strangeConv2d(nn.Conv2d):

    def __init__(self, in_channels, out_channels, kernel_size, stride=1,
                 padding=0, dilation=1, groups=1, bias=True,eps1=1e-6,eps2=1e-3,beta=0.99,showGrad=False):
        nn.Conv2d.__init__(self, in_channels, out_channels, kernel_size, stride=stride,padding=padding, dilation=dilation, groups=groups, bias=bias)
        self.eps1=nn.Parameter(torch.Tensor([eps1]),requires_grad=False)
        self.eps2=nn.Parameter(torch.Tensor([eps2]),requires_grad=False)
        self.beta=nn.Parameter(torch.Tensor([beta]),requires_grad=False)


The problem still exists.
what can I do to solve it?