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 …


class DeepQNetwork(nn.Module):

def __init__(self, no_inputs, no_outputs):

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

def forward(self, x):
    print('input = {}'.format(x))
    output = Variable(x)
    output = self.lin1(
    output = F.relu(output)
    output = self.lin2(
    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/”, line 28, in forward
output = self.lin1(
File “/gymcartpole/venv/lib/python3.6/site-packages/torch/nn/modules/”, line 489, in call
result = self.forward(*input, **kwargs)
File “/gymcartpole/venv/lib/python3.6/site-packages/torch/nn/modules/”, line 60, in forward
File “/gymcartpole/venv/lib/python3.6/site-packages/torch/nn/modules/”, line 169, in _check_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.