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

Sam_A: <p>I am getting the bellow error message:</p>
<pre><code class="lang-auto">Traceback (most recent call last):

  File "C:\Users\Sam\Desktop\Bitcoin\Q_Learning\DQN_NEW_Original.py", line 122, in &lt;module&gt;
    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 &lt;module&gt;
    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 

</code></pre>
<p>Here is my network setup:</p>
<pre><code class="lang-auto">     super(LinearDQN, self).__init__()
     self.fc1 = nn.Linear(*input_dims, 128)
     self.fc2 = nn.Linear(128, n_actions) 
</code></pre>
<p>The input dims are [30,2] and n_actions is 2.</p>
<p>Also note that the error ‘m1: [30 x 2]’ increases in multiples of my batch size (no idea why).</p>
<p>Cheers all,</p>

ptrblck: <p>The tensor shapes of <code>[30, 2] x [30, 2]</code> are invalid for a matrix multiplication and you would either need to <code>permute</code> one tensor or adapt the <code>in_features</code> of a linear layer as seen here:</p>
<pre><code class="lang-python">a = torch.randn(30, 2)
b = torch.randn(30, 2)

c = torch.matmul(a, b)
&gt; 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
</code></pre>
<p>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.</p>

Sam_A: <p>Sorry about that - i’ve just updated my code.</p>

Sam_A: <p>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?</p>

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