RuntimeError: mat1 and mat2 shapes cannot be multiplied (951x10 and 951x4)

insung3511: <p>Hi I got a error on linear0 function while matrix multiple. Here is the code…</p>
<pre><code class="lang-python">lin = nn.Linear(len(output_from_third), 4)
linear_in = torch.tensor(output_from_third)
print("Linear_in size : {}, Linear_in dim : {}".format(linear_in.size(), linear_in.dim()))

print(lin(linear_in))
</code></pre>
<p>and this is output.</p>
<pre><code class="lang-auto">Linear_in size : torch.Size([951, 10]), Linear_in dim : 2
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
/var/folders/_8/q_cwh5hn0s1dxsrzq2d040p80000gn/T/ipykernel_44889/2866187022.py in &lt;module&gt;
      3 print("Linear_in size : {}, Linear_in dim : {}".format(linear_in.size(), linear_in.dim()))
      4 
----&gt; 5 print(lin(linear_in))

~/miniforge3/lib/python3.9/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   1100         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1101                 or _global_forward_hooks or _global_forward_pre_hooks):
-&gt; 1102             return forward_call(*input, **kwargs)
   1103         # Do not call functions when jit is used
   1104         full_backward_hooks, non_full_backward_hooks = [], []

~/miniforge3/lib/python3.9/site-packages/torch/nn/modules/linear.py in forward(self, input)
    101 
    102     def forward(self, input: Tensor) -&gt; Tensor:
--&gt; 103         return F.linear(input, self.weight, self.bias)
    104 
    105     def extra_repr(self) -&gt; str:

~/miniforge3/lib/python3.9/site-packages/torch/nn/functional.py in linear(input, weight, bias)
   1846     if has_torch_function_variadic(input, weight, bias):
   1847         return handle_torch_function(linear, (input, weight, bias), input, weight, bias=bias)
-&gt; 1848     return torch._C._nn.linear(input, weight, bias)
   1849 
   1850 

RuntimeError: mat1 and mat2 shapes cannot be multiplied (951x10 and 951x4)
</code></pre>
<p>How do I changed tensor sizes? plz give me some solutions thanks :slight_smile <img src="https://discuss.pytorch.org/images/emoji/apple/smile.png?v=12" title=":smile:" class="emoji" alt=":smile:" loading="lazy" width="20" height="20"></p>

Matias_Vasquez: <p>You need to transpose <code>linear_in</code> in order to be compatible with your <code>nn.Linear</code> layer.</p>
<pre><code class="lang-python"># Example

linear_in = torch.rand(951, 10)
lin = torch.nn.Linear(951, 4)

# This causes an error
lin(linear_in)


# This should work fine
lin(linear_in.T)

</code></pre>
<p>As a rule of thumb, the last dimension of your input must match the first dimension of your linear layer.</p>
<p>So if you did something like this, it should also work because the last dim of <code>linear_in</code> is the same as the first of <code>lin</code>.</p>
<pre><code class="lang-python">linear_in = torch.rand(10, 20, 30, 951)
lin = torch.nn.Linear(951, 4)

lin(linear_in)
</code></pre>
<p>Hope this helps <img src="https://discuss.pytorch.org/images/emoji/apple/smile.png?v=12" title=":smile:" class="emoji" alt=":smile:" loading="lazy" width="20" height="20"></p>

is_Juncheng: <p>When your two matrices A1 (m1, n1) and A2 (m2, n2) need to be multiplied, you need to ensure that n1 ==m2!</p>

insung3511: <p>damn, thank you for advise. It’s works. Btw what meaning of <code>.T</code>?</p>

Matias_Vasquez: <p><code>.T</code> transposes dimensions 0 and 1 for a 2d tensor.</p>
<p>For a higher dimension tensor you should use <code>torch.transpose(x, dim0, dim1)</code> to specify which dimensions you want to swap. (only changing two dims at the time)</p>
<p>Or if you want to change the order of even more dimensions at the same time, you can use <code>.permute</code>.</p>
<p>Hooe this helps <img src="https://discuss.pytorch.org/images/emoji/apple/smile.png?v=12" title=":smile:" class="emoji" alt=":smile:" loading="lazy" width="20" height="20"></p>