Ize mismatch, m1: [30 x 2], m2: [30 x 2]

I am getting the bellow error message:

Traceback (most recent call last):

  File "C:\Users\Sam\Desktop\Bitcoin\Q_Learning\DQN_NEW_Original.py", line 122, in <module>
    agent = Agent(lr=0.001, input_dims=env.observation_space.shape, n_actions=env.action_space.n)

  File "C:\Users\Sam\Desktop\Bitcoin\Q_Learning\DQN_NEW_Original.py", line 57, in __init__
    self.Q = LinearDQN(self.lr, self.n_actions, self.input_dims)

  File "C:\Users\Sam\Desktop\Bitcoin\Q_Learning\DQN_NEW_Original.py", line 22, in __init__
    self.fc1 = T.flatten(nn.Linear(*input_dims, 128))

TypeError: flatten(): argument 'input' (position 1) must be Tensor, not Linear


runfile('C:/Users/Sam/Desktop/Bitcoin/Q_Learning/DQN_NEW_Original.py', wdir='C:/Users/Sam/Desktop/Bitcoin/Q_Learning')
  File "C:\Users\Sam\Desktop\Bitcoin\Q_Learning\DQN_NEW_Original.py", line 37
    actions = self.fc2(layer1)
    ^
SyntaxError: invalid syntax


runfile('C:/Users/Sam/Desktop/Bitcoin/Q_Learning/DQN_NEW_Original.py', wdir='C:/Users/Sam/Desktop/Bitcoin/Q_Learning')
Traceback (most recent call last):

  File "C:\Users\Sam\Desktop\Bitcoin\Q_Learning\DQN_NEW_Original.py", line 133, in <module>
    agent.learn(obs, action, reward, obs_)

  File "C:\Users\Sam\Desktop\Bitcoin\Q_Learning\DQN_NEW_Original.py", line 80, in learn
    q_pred = self.Q.forward(states)[actions]

  File "C:\Users\Sam\Desktop\Bitcoin\Q_Learning\DQN_NEW_Original.py", line 36, in forward
    layer1 = F.relu(T.flatten(self.fc1(state)))

  File "C:\Users\Sam\anaconda3\envs\tensorflow2\lib\site-packages\torch\nn\modules\module.py", line 550, in __call__
    result = self.forward(*input, **kwargs)

  File "C:\Users\Sam\anaconda3\envs\tensorflow2\lib\site-packages\torch\nn\modules\linear.py", line 87, in forward
    return F.linear(input, self.weight, self.bias)

  File "C:\Users\Sam\anaconda3\envs\tensorflow2\lib\site-packages\torch\nn\functional.py", line 1610, in linear
    ret = torch.addmm(bias, input, weight.t())

RuntimeError: size mismatch, m1: [30 x 2], m2: [30 x 2] at C:/w/b/windows/pytorch/aten/src\THC/generic/THCTensorMathBlas.cu:283 

Here is my network setup:

     super(LinearDQN, self).__init__()
     self.fc1 = nn.Linear(*input_dims, 128)
     self.fc2 = nn.Linear(128, n_actions) 

The input dims are [30,2] and n_actions is 2.

Also note that the error ‘m1: [30 x 2]’ increases in multiples of my batch size (no idea why).

Cheers all,

The tensor shapes of [30, 2] x [30, 2] are invalid for a matrix multiplication and you would either need to permute one tensor or adapt the in_features of a linear layer as seen here:

a = torch.randn(30, 2)
b = torch.randn(30, 2)

c = torch.matmul(a, b)
> RuntimeError: mat1 and mat2 shapes cannot be multiplied (30x2 and 30x2)

b = torch.randn(2, 30)
c = torch.matmul(a, b) # works

b = torch.randn(2, 2)
c = torch.matmul(a, b) # works

Your current output is hard to read so note that you can post code snippets and logs by wrapping them into three backticks ```, which would make debugging easier.

1 Like

Sorry about that - i’ve just updated my code.

I’m not able to permute the input tensor as it wont match the input data and comes up with another error. What else can I try?

You could try to use the second suggestion: adapt the in_features so that the linear layer creates the internal weight matrix in the expected shape for the incoming activation.