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

In pytorch linear function got a problem while matmul. I think input tensor size are not matched... here is the error code.

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))

and here is result of that code.

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 <module>
      3 print("Linear_in size : {}, Linear_in dim : {}".format(linear_in.size(), linear_in.dim()))
      4 
----> 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):
-> 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) -> Tensor:
--> 103         return F.linear(input, self.weight, self.bias)
    104 
    105     def extra_repr(self) -> 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)
-> 1848     return torch._C._nn.linear(input, weight, bias)
   1849 
   1850 

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

I don't know how change tensor size fit in for linear function. plz give me any solutions thank you.



Solution 1:[1]

Your dimensions don't match. Maybe you should give another size of output_from_third as dimension of lin. Try this:

lin = nn.Linear(output_from_third.shape[1], 4)

instead of

lin = nn.Linear(len(output_from_third), 4)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1