pytorch-summary icon indicating copy to clipboard operation
pytorch-summary copied to clipboard

Error 'int' object has no attribute 'numpy'

Open uro1012 opened this issue 5 years ago • 2 comments

Hello,

I made some auto-encoder for a project for the university. It works well for my encoder, decoder and classifier, but not the layers before the classifier.

In the initialization function, I got the following:

        self.avgpool = nn.Sequential(
            # input size: 8x8x256
            nn.Dropout(0.25),
            nn.AvgPool2d(kernel_size=3, stride=1),
            # output: 6x6x256
        )

        # class prediction
        self.pred = nn.Sequential(
            # input size: 6x6x256
            nn.Linear(6*6*256, 2048),
            # output: 2048
            nn.Dropout(),
            nn.ReLU(inplace=True),
            nn.Linear(2048, n_class),
            # output: n_class
        )

When trying to get the summary of the avgpool layers, I get an error.

summary(ModelAE(200).avgpool.to(device), (256, 8, 8))

'int' object has no attribute 'numpy'

The error comes from the line 102 total_params_size = abs(total_params.numpy() * 4. / (1024 ** 2.)) of the torchsummary.py file.

uro1012 avatar May 19 '20 15:05 uro1012

getting similar error when trying to use latest torchsummary release (1.5.1) with latest pytorch release (1.5.0) on a custom nn.Module-derived net. nothing fancy - using some nn.Sequential with Linear layers and ReLU.

I believe culprit is here: https://github.com/sksq96/pytorch-summary/blob/master/torchsummary/torchsummary.py#L40

In particular, in the case where neither weight or bias properties are defined for a custom nn.Module-derived class, none of the if statements would trigger and we'd be left with the default (python int scalar) params = 0.0, which we later try and call .numpy() on , assuming it is indeed a torch Tensor.

A simple solution could be to instantiate it instead to be a Tensor perhaps? e.g.:

params = torch.tensor([0])

adamhadani avatar May 21 '20 20:05 adamhadani

See #124

TylerYep avatar Jun 06 '20 23:06 TylerYep