Hello Valerio!



 Valerio_Biscione:

Basically I create a new linear layer, self.my_fc, deep-copying another layer, self.fc1. I change the input size of this layer to 3200 or whatever. However, I apply a tensor to both self.my_fc and self.fc1. I expect my new layer to fail, as the number of input doesn’t much the expected ones. Instead, they both work.
        self.fc1 = nn.Linear(16 * 20 * 20, 120) 

        self.my_fc = copy.deepcopy(self.fc1)
        self.my_fc.in_features = 3200



I think that self.my_fc.in_features = 3200 is, in effect, lying to
your Linear layer.  You already constructed it with an input
dimension of 6400.
Here’s an example script:
import torch
import copy

torch.__version__

fc1 = torch.nn.Linear (10, 2)
my_fc = copy.deepcopy (fc1)
my_fc.in_features = 5

fc1.in_features
fc1.weight
my_fc.in_features
my_fc.weight

Here is the output:
>>> import torch
>>> import copy
>>>
>>> torch.__version__
'0.3.0b0+591e73e'
>>>
>>> fc1 = torch.nn.Linear (10, 2)
>>> my_fc = copy.deepcopy (fc1)
>>> my_fc.in_features = 5
>>>
>>> fc1.in_features
10
>>> fc1.weight
Parameter containing:
 0.0767  0.1974  0.0051  0.1165  0.0270 -0.2915 -0.0038 -0.2985  0.2314  0.2616
 0.0099 -0.0060  0.1231  0.1886  0.2331  0.0675 -0.1929 -0.1226 -0.1118 -0.2078
[torch.FloatTensor of size 2x10]

>>> my_fc.in_features
5
>>> my_fc.weight
Parameter containing:
 0.0767  0.1974  0.0051  0.1165  0.0270 -0.2915 -0.0038 -0.2985  0.2314  0.2616
 0.0099 -0.0060  0.1231  0.1886  0.2331  0.0675 -0.1929 -0.1226 -0.1118 -0.2078
[torch.FloatTensor of size 2x10]

As you can see, setting my_fc.in_features doesn’t actually change
the weight tensor, so (presumably) doesn’t actually change the
expected shape of the input tensor.
You could argue that this is a bug in that it shouldn’t be possible
to create such an “inconsistent” Linear for which in_features
and weight.shape don’t match.


I also found that, sometime, by changing the input feature to THE SAME input feature as the deep-copied layer, the operation DOESN’T WORK and it actually complain about mismatch!


I would guess that this is the same issue.  You’re trying to change
in_features to match the shape of your input tensor, but this
doesn’t actually change the shape of the weight tensor, so you
do get a mismatch.
Good luck.
K. Frank