BatchNorm1d ValueError: expected 2D or 3D input (got 1D input)

Hi all!

I’m trying to implement batch normalizing to my Neural Network, but I always get the same error. I looked through internet and couldn’t find a satisfying answer for my problem.

Here’s My code:
import …

HIDDEN_LAYER = 32

class DeepQNetwork(nn.Module):

def __init__(self, no_inputs, no_outputs):
    super(DeepQNetwork,self).__init__()

    self.lin1 = nn.Linear(no_inputs,HIDDEN_LAYER)
    self.lin2 = nn.Linear(HIDDEN_LAYER,HIDDEN_LAYER)
    self.lin3 = nn.Linear(HIDDEN_LAYER,no_outputs)

    self.bn = nn.BatchNorm1d(num_features=HIDDEN_LAYER)

def forward(self, x):
    print('input = {}'.format(x))
    output = Variable(x)
    output = self.lin1(self.bn(output))
    output = F.relu(output)
    output = self.lin2(self.bn(output))
    output = F.relu(output)
    output = self.lin3(output)

    return F.relu(output)

output of the code:

input = tensor([ 0.0011, 0.0148, 0.0056, -0.0481], device=‘cuda:0’)

Traceback (most recent call last):

File “/gymcartpole/venv/DeepQ.py”, line 28, in forward
output = self.lin1(self.bn(output))
File “/gymcartpole/venv/lib/python3.6/site-packages/torch/nn/modules/module.py”, line 489, in call
result = self.forward(*input, **kwargs)
File “/gymcartpole/venv/lib/python3.6/site-packages/torch/nn/modules/batchnorm.py”, line 60, in forward
self._check_input_dim(input)
File “/gymcartpole/venv/lib/python3.6/site-packages/torch/nn/modules/batchnorm.py”, line 169, in _check_input_dim
.format(input.dim()))
ValueError: expected 2D or 3D input (got 1D input)

please help me :slight_smile:

nn.BatchNorm1d expects an input of the shape [batch_size, channels] or [batch_size, channels, length].
Currently you are just passing a tensor with a single dimension to the layer.
If your data has 4 features, you should add the batch dimension using:

input = input.unsqueeze(0)

before passing it to your model.

2 Likes