pytorch-summary icon indicating copy to clipboard operation
pytorch-summary copied to clipboard

unable to handle batchnorm1d

Open duhd1993 opened this issue 5 years ago • 2 comments

No issues for this:

----------------------------------------------------------------
        Layer (type)               Output Shape         Param #
================================================================
            Linear-1               [-1, 1, 100]          30,100
              Tanh-2               [-1, 1, 100]               0
            Linear-3                [-1, 1, 50]           5,050
              Tanh-4                [-1, 1, 50]               0
            Linear-5                 [-1, 1, 3]             153
================================================================

When I add batchnorm1d, errors:

RuntimeError: running_mean should contain 1 elements not 100

duhd1993 avatar Jul 27 '19 19:07 duhd1993

Hello!

I wonder if this is an issue with the network structure? The returned error looks like it is one from Pytorch, and not from pytorch-summary.

Based on your output above, it looks like Linear-1 has a 1 channel, but a length of 100. Based on the documentation (https://pytorch.org/docs/stable/nn.html#batchnorm1d ), then the running mean should indeed have 1 element, not 100.

Hope that helps!

Naireen avatar Dec 28 '19 04:12 Naireen

I also got the same error.

when my network structure is:

model = nn.Sequential(
            nn.BatchNorm1d(self.feater_dim, affine=self.is_train),
            nn.Linear(in_features=self.feater_dim, out_features=self.config['hidden1']),
            nn.GELU(),
            nn.Linear(in_features=self.config['hidden1'], out_features=self.config['hidden2']),
            nn.GELU(),
            nn.Linear(in_features=self.config['hidden2'], out_features=self.config['hidden3']),
            nn.GELU(),
            nn.Linear(in_features=self.config['hidden3'], out_features=self.action_dim),
            nn.Softmax(dim=1)
        )

I run pytorchsummary.summary(model, (8, 20)) and I got the error:

        return torch.batch_norm(
>           input, weight, bias, running_mean, running_var, training, momentum, eps, torch.backends.cudnn.enabled
        )
E       RuntimeError: running_mean should contain 8 elements not 20

But when I change the model architecture to :

model = nn.Sequential(
            # nn.BatchNorm1d(self.feater_dim, affine=self.is_train),
            nn.Linear(in_features=self.feater_dim, out_features=self.config['hidden1']),
            nn.GELU(),
            nn.Linear(in_features=self.config['hidden1'], out_features=self.config['hidden2']),
            nn.GELU(),
            nn.Linear(in_features=self.config['hidden2'], out_features=self.config['hidden3']),
            nn.GELU(),
            nn.Linear(in_features=self.config['hidden3'], out_features=self.action_dim),
            nn.Softmax(dim=1)
        )

the run result is correct:

----------------------------------------------------------------
        Layer (type)               Output Shape         Param #
================================================================
            Linear-1               [-1, 8, 128]           2,688
              GELU-2               [-1, 8, 128]               0
            Linear-3               [-1, 8, 128]          16,512
              GELU-4               [-1, 8, 128]               0
            Linear-5               [-1, 8, 128]          16,512
              GELU-6               [-1, 8, 128]               0
            Linear-7                 [-1, 8, 4]             516
           Softmax-8                 [-1, 8, 4]               0
================================================================
Total params: 36,228
Trainable params: 36,228
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.00
Forward/backward pass size (MB): 0.05
Params size (MB): 0.14
Estimated Total Size (MB): 0.19
----------------------------------------------------------------

herywang avatar May 10 '22 09:05 herywang