RuntimeError: size mismatch, m1: [5 x 10], m2: [5 x 32] at /pytorch/aten/src/TH/generic/THTensorMath.cpp

Masilive_Sifanele: <p>I need your help. Running the code below throws:</p>
<p><code>RuntimeError: size mismatch, m1: [5 x 10], m2: [5 x 32] at /pytorch/aten/src/TH/generic/THTensorMath.cpp</code></p>
<p>I looked at similar questions but they are image related and suggest flattening the input, I tried them with no luck.</p>
<p>I’m using Python 3.6.8 and torch 1.1.0</p>
<p>**<strong>code sample:</strong></p>
<pre><code class="lang-auto">state = [[307, 1588204935.0, 1.0869, 1.08708, 1.08659, 1.08662, 1.08708, 1.08724, 1.08674, 1.08677],
          [370, 1588204920.0, 1.08668, 1.08709, 1.08661, 1.08693, 1.08682, 1.08724, 1.08677, 1.08708],
          [243, 1588204905.0, 1.08637, 1.08671, 1.08624, 1.08669, 1.08651, 1.08686, 1.08639, 1.08683],
          [232, 1588204890.0, 1.08614, 1.08656, 1.08609, 1.08636, 1.08628, 1.0867, 1.08626, 1.0865],
          [349, 1588204875.0, 1.086, 1.08623, 1.08585, 1.08614, 1.08614, 1.08638, 1.08597, 1.0863]]

def predict(state, state_dim=5, action_dim=3, hidden_dim=32, lr=0.05):
    """ Compute Q values for all actions using the DQL. """
    criterion = torch.nn.MSELoss()
    model = torch.nn.Sequential(torch.nn.Linear(state_dim, hidden_dim),
                                     torch.nn.LeakyReLU(),
                                     torch.nn.Linear(hidden_dim, hidden_dim*2),
                                     torch.nn.LeakyReLU(),
                                     torch.nn.Linear(hidden_dim*2, action_dim))

    optimizer = torch.optim.Adam(model.parameters(), lr)

    with torch.no_grad():
        return model(torch.Tensor(state)) # Error throw here

predict(state).tolist()
</code></pre>

KFrank: <p>Hi Masilive!</p>
<aside class="quote no-group" data-username="Masilive_Sifanele" data-post="1" data-topic="79714" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/masilive_sifanele/48/11840_2.png" class="avatar"> Masilive_Sifanele:</div>
<blockquote>
<p><code>RuntimeError: size mismatch, m1: [5 x 10], m2: [5 x 32] at /pytorch/aten/src/TH/generic/THTensorMath.cpp</code></p>
</blockquote>
</aside>
<p>The short answer is that you have the dimensions of your input<br>
swapped.  You would want something like, for example, “m1: [10 x 5]”.</p>
<aside class="quote no-group">
<blockquote>
<pre><code class="lang-auto">state = [[307, 1588204935.0, 1.0869, 1.08708, 1.08659, 1.08662, 1.08708, 1.08724, 1.08674, 1.08677],
          [370, 1588204920.0, 1.08668, 1.08709, 1.08661, 1.08693, 1.08682, 1.08724, 1.08677, 1.08708],
          [243, 1588204905.0, 1.08637, 1.08671, 1.08624, 1.08669, 1.08651, 1.08686, 1.08639, 1.08683],
          [232, 1588204890.0, 1.08614, 1.08656, 1.08609, 1.08636, 1.08628, 1.0867, 1.08626, 1.0865],
          [349, 1588204875.0, 1.086, 1.08623, 1.08585, 1.08614, 1.08614, 1.08638, 1.08597, 1.0863]]
</code></pre>
</blockquote>
</aside>
<p>Pytorch will interpret <code>state</code> as a batch of 5 samples, each of which<br>
is a vector of 10 values.</p>
<aside class="quote no-group">
<blockquote>
<pre><code class="lang-auto">def predict(state, state_dim=5, action_dim=3, hidden_dim=32, lr=0.05):
    """ Compute Q values for all actions using the DQL. """
    criterion = torch.nn.MSELoss()
    model = torch.nn.Sequential(torch.nn.Linear(state_dim, hidden_dim),
                                     torch.nn.LeakyReLU(),
                                     torch.nn.Linear(hidden_dim, hidden_dim*2),
                                     torch.nn.LeakyReLU(),
                                     torch.nn.Linear(hidden_dim*2, action_dim))
</code></pre>
</blockquote>
</aside>
<p>But the first layer of your <code>Sequential</code> is a <code>Linear</code> that is expecting an<br>
input vector that consists of only 5 values.</p>
<p>Remember, pytorch models always work on <em>batches</em> of samples (even<br>
if the batch size is 1).  So the first dimension of <code>state</code> is, in fact, your<br>
batch size.</p>
<p>How to fix this depends on what you want your <code>state</code> tensor to mean.<br>
Is it supposed to be a batch of 5 length-10 vectors (as it is being treated<br>
now), or is it supposed to be a batch of 10 length-5 vectors?  Or<br>
something else?</p>
<p>Good luck.</p>
<p>K. Frank</p>

Masilive_Sifanele: <p>Hello Frank, thanks for your response. I want it to a batch of 5 length with 10 vectors.</p>
<p>Warm regards,</p>
<p>Masilive</p>

Masilive_Sifanele: <p>Thanks Frank.<br>
I changed my state_dim form 5 to 10</p>
<p><code>def predict(state_, state_dim=10, action_dim=3, hidden_dim=32, lr=0.05):</code></p>