nn.Linear(16,32,3) 3D returns RuntimeError: size mismatch at /opt/conda/conda-bld/pytorch_1518243271935/work/torch/lib/THC/generic/THCTensorMathBlas.cu:247


l have the following input x=dim(16,16,3) that l would like to pass tonn.Linear()

cl = nn.Linear(16,32,3)

when l make x=cl(x) l get the following error :
***** RuntimeError: size mismatch at /opt/conda/conda-bld/pytorch_1518243271935/work/torch/lib/THC/generic/THCTensorMathBlas.cu:247**

But when it comes to 2D input such as


It works well.

How can l adapt nn.Linear to (16,32,3) ?

From the docs :
“Input: (N,∗,in_features) where * means any number of additional dimensions.
Output: (N,∗,out_features) where all but the last dimension are the same shape as the input.”

So with nn.Linear, you can apply a transformation on the last dimension of you input.

Thanks it works l did the following :

 cl = nn.Linear(16, 32,3)
 x = cl(x)

but according to documentation Output: (N,∗,out_features) is equivalent to cl = nn.Linear(16, 3,32) (in this case it doesn’t work. l permuted dim 2 and dim 1 like this and it works cl = nn.Linear(16, 32,3)

nn.Linear(in_features, out_features, bias=True)… I don’t know what your 3 should do.

If your import is of size (16,16,3)
then your Linear layer should be nn.Linear(3, output_features)
and will return output of size (16,16,output_features).

And if you feed it input of size (16,32,3)
then it will produce output of size (16,32,output_features)

1 Like