Dimension out of range (expected to be in range of [-1, 0], but got 1) in pytorch

I get the following error

IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)

from this line: x = torch.cat([a, b], 1)

in the snippet code below:

class Critic(nn.Module):
    def __init__(self, input_size, hidden_size, output_size):
        super(Critic, self).__init__()
        self.linear1 = nn.Linear(input_size, hidden_size)
        self.linear2 = nn.Linear(hidden_size, hidden_size)
        self.linear3 = nn.Linear(hidden_size, output_size)

    def forward(self, a, b):

        x = torch.cat([a, b], 1)
        x = F.relu(self.linear1(x))
        x = F.relu(self.linear2(x))
        x = self.linear3(x)

        return x

Any help on this appreciated.

The error is raised, if a and b have less that two dimensions, while torch.cat tries to concatenate these tensors in dim1. Here is an example:

# both tensors have a single dimension
a, b = torch.randn(10), torch.randn(10)
x = torch.cat([a, b], 1)
> IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)

# works
a, b = torch.randn(10, 1), torch.randn(10, 1)
x = torch.cat([a, b], 1)

Make sure that both tenors have at least 2 dimensions before passing them into the forward method.