RuntimeError: running_mean should contain 1 elements not 512

Hi I got a run time error:

---------------------------------------------------------------------------

RuntimeError                              Traceback (most recent call last)

<ipython-input-53-7395aace3bff> in <module>
      1 from torchsummary import summary
      2 model = MancalaModel()
----> 3 summary(model, (1, 16), device='cpu')
      4 

d:\environments\python\lib\site-packages\torchsummary\torchsummary.py in summary(model, input_size, batch_size, device)
     70     # make a forward pass
     71     # print(x.shape)
---> 72     model(*x)
     73 
     74     # remove these hooks

d:\environments\python\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
    725             result = self._slow_forward(*input, **kwargs)
    726         else:
--> 727             result = self.forward(*input, **kwargs)
    728         for hook in itertools.chain(
    729                 _global_forward_hooks.values(),

<ipython-input-51-34fe4c94f943> in forward(self, x)
     29         x = F.relu(self.linear1(x))
     30         print(x.shape)
---> 31         x = F.dropout(self.batch_norm1(x), p=0.1)
     32         x = F.dropout(self.batch_norm2(F.relu(self.linear2(x))), p=0.1)
     33         x = self.batch_norm3(F.relu(self.linear3(x)))

d:\environments\python\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
    725             result = self._slow_forward(*input, **kwargs)
    726         else:
--> 727             result = self.forward(*input, **kwargs)
    728         for hook in itertools.chain(
    729                 _global_forward_hooks.values(),

d:\environments\python\lib\site-packages\torch\nn\modules\batchnorm.py in forward(self, input)
    129         used for normalization (i.e. in eval mode when buffers are not None).
    130         """
--> 131         return F.batch_norm(
    132             input,
    133             # If buffers are not to be tracked, ensure that they won't be updated

d:\environments\python\lib\site-packages\torch\nn\functional.py in batch_norm(input, running_mean, running_var, weight, bias, training, momentum, eps)
   2054         _verify_batch_size(input.size())
   2055 
-> 2056     return torch.batch_norm(
   2057         input, weight, bias, running_mean, running_var,
   2058         training, momentum, eps, torch.backends.cudnn.enabled

RuntimeError: running_mean should contain 1 elements not 512

Heres my code:

class MancalaModel(nn.Module):

    def __init__(self, n_inputs=16, n_outputs=16):
        super().__init__()

        n_neurons = 512

        self.linear1 = nn.Linear(n_inputs, n_neurons)
        self.batch_norm1 = nn.BatchNorm1d(n_neurons)
        self.linear2 = nn.Linear(n_neurons, n_neurons)
        self.batch_norm2 = nn.BatchNorm1d(n_neurons)
        self.linear3 = nn.Linear(n_neurons, n_neurons)
        self.batch_norm3 = nn.BatchNorm1d(n_neurons)

        self.actor = nn.Linear(n_neurons, n_outputs)
        self.critics = nn.Linear(n_neurons, 1)

        self.apply(init_weights)

    def forward(self, x):
        x = F.dropout(self.batch_norm1(F.relu(self.linear1(x))), p=0.1)
        x = F.dropout(self.batch_norm2(F.relu(self.linear2(x))), p=0.1)
        x = self.batch_norm3(F.relu(self.linear3(x)))
        return F.softmax(self.actor(x)), self.critics(x)

from torchsummary import summary
model = MancalaModel()
summary(model, (1, 16), device='cpu')

May I know why the batch normalization layer says the size is not expected?

1 Like

Ah I found it, the batch norm layer’s input dimension should be 1 instead of n_neurons, it is the number of channels rather than the number of features.

4 Likes