TypeError: 'float' object cannot be interpreted as an integer

For https://github.com/promach/pytorch-pruning/blob/master/prune.py#L97 , how could I solve the following error ?

Note: this line of code does not use divide operations unlike in others code

[phung@archlinux pytorch-pruning]$ python finetune.py --prune
/usr/lib/python3.7/site-packages/torchvision/transforms/transforms.py:187: UserWarning: The use of the transforms.Scale transform is deprecated, please use transforms.Resize instead.
warnings.warn("The use of the transforms.Scale transform is deprecated, " +
/usr/lib/python3.7/site-packages/torchvision/transforms/transforms.py:562: UserWarning: The use of the transforms.RandomSizedCrop transform is deprecated, please use transforms.RandomResizedCrop instead.
warnings.warn("The use of the transforms.RandomSizedCrop transform is deprecated, " +
Accuracy: 0.6483
Number of prunning iterations to reduce 67% filters 5
Ranking filters…
Layers that will be prunned {10: 25, 26: 58, 21: 52, 24: 51, 28: 122, 17: 77, 12: 25, 14: 19, 19: 61, 7: 13, 5: 4, 0: 1, 2: 4}
Prunning filters…
Traceback (most recent call last):
File “finetune.py”, line 270, in
fine_tuner.prune()
File “finetune.py”, line 228, in prune
model = prune_vgg16_conv_layer(model, layer_index, filter_index)
File “/home/phung/Documents/Grive/Personal/Coursera/Machine_Learning/pruning/pytorch-pruning/prune.py”, line 97, in prune_vgg16_conv_layer
old_linear_layer.out_features)
File “/usr/lib/python3.7/site-packages/torch/nn/modules/linear.py”, line 48, in init
self.weight = Parameter(torch.Tensor(out_features, in_features))
TypeError: ‘float’ object cannot be interpreted as an integer
[phung@archlinux pytorch-pruning]$

A quick fix is to do

int(old_linear_layer.out_features)

I think there is an issue during model initialization.
In https://github.com/pytorch/pytorch/blob/master/torch/nn/modules/linear.py#L47 we directly assing out_features as it is instead of typecasting to int and that creates this problem.

Will follow up further with this issue.

It seems that explicit typecast to int does not work as shown below:

[phung@archlinux pytorch-pruning]$ python finetune.py --prune
/usr/lib/python3.7/site-packages/torchvision/transforms/transforms.py:187: UserWarning: The use of the transforms.Scale transform is deprecated, please use transforms.Resize instead.
warnings.warn("The use of the transforms.Scale transform is deprecated, " +
/usr/lib/python3.7/site-packages/torchvision/transforms/transforms.py:562: UserWarning: The use of the transforms.RandomSizedCrop transform is deprecated, please use transforms.RandomResizedCrop instead.
warnings.warn("The use of the transforms.RandomSizedCrop transform is deprecated, " +
Accuracy: 0.6483
Number of prunning iterations to reduce 67% filters 5
Ranking filters…
Layers that will be prunned {28: 130, 17: 64, 10: 27, 24: 67, 26: 63, 19: 64, 21: 51, 5: 4, 12: 17, 14: 17, 0: 2, 7: 3, 2: 3}
Prunning filters…
Traceback (most recent call last):
File “finetune.py”, line 270, in
fine_tuner.prune()
File “finetune.py”, line 228, in prune
model = prune_vgg16_conv_layer(model, layer_index, filter_index)
File “/home/phung/Documents/Grive/Personal/Coursera/Machine_Learning/pruning/pytorch-pruning/prune.py”, line 97, in prune_vgg16_conv_layer
int(old_linear_layer.out_features))
File “/usr/lib/python3.7/site-packages/torch/nn/modules/linear.py”, line 48, in init
self.weight = Parameter(torch.Tensor(out_features, in_features))
TypeError: ‘float’ object cannot be interpreted as an integer
[phung@archlinux pytorch-pruning]$

Did you type cast both the inputs to the nn.Linear() module?

1 Like