sksq96 / pytorch-summary

Model summary in PyTorch similar to `model.summary()` in Keras
MIT License
4.02k stars 414 forks source link

Crash on custom module with Parameter #78

Open jvanheugten opened 5 years ago

jvanheugten commented 5 years ago

I was trying to create a custom layer and check it with summary, however, it kept crashing. Here is a simple example for which the summary code crashes:

class TestMod(nn.Module):
    def __init__(self, input_size, attention_size, eps=0.0):
        super().__init__()
        self.weight = nn.Parameter(torch.Tensor(attention_size, input_size))
        nn.init.kaiming_uniform_(self.weight, a=math.sqrt(5))

    def forward(self, x):
        return self.weight
test = TestMod(2,3)
print(list(test.named_parameters()))
torchsummary.summary(test, [[2]])

Output:

[('weight', Parameter containing:
tensor([[-0.7039, -0.1902],
        [-0.3078, -0.1418],
        [-0.4269, -0.4219]], requires_grad=True))]
----------------------------------------------------------------
        Layer (type)               Output Shape         Param #
================================================================
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-161-ff76d1f01e72> in <module>
      9 test = TestMod(2,3)
     10 print(list(test.named_parameters()))
---> 11 torchsummary.summary(test, [[2]])

~/miniconda3/lib/python3.6/site-packages/torchsummary/torchsummary.py in summary(model, input_size, batch_size, device)
    100     total_input_size = abs(np.prod(input_size) * batch_size * 4. / (1024 ** 2.))
    101     total_output_size = abs(2. * total_output * 4. / (1024 ** 2.))  # x2 for gradients
--> 102     total_params_size = abs(total_params.numpy() * 4. / (1024 ** 2.))
    103     total_size = total_params_size + total_output_size + total_input_size
    104 

AttributeError: 'int' object has no attribute 'numpy'
BruceBinBoxing commented 5 years ago

I also met this problem!

vyouman commented 5 years ago

Me too here!

ucalyptus commented 5 years ago

Same problem , @sksq96

Naireen commented 4 years ago
class TestMod(nn.Module):
    def __init__(self, input_size, attention_size, eps=0.0):
        super().__init__()
        self.weight = nn.Parameter(torch.Tensor(attention_size, input_size))
        nn.init.kaiming_uniform_(self.weight, a=math.sqrt(5))

    def forward(self, x):
        return self.weight

test = TestMod(2,3)
# print(list(test.named_parameters()))

summary(test, ((1, 2)))

yeilds the following output:

----------------------------------------------------------------
        Layer (type)               Output Shape         Param #
================================================================
           TestMod-1                    [-1, 2]               6
================================================================
Total params: 6
Trainable params: 6
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.00
Forward/backward pass size (MB): 0.00
Params size (MB): 0.00
Estimated Total Size (MB): 0.00
----------------------------------------------------------------

The input size was passed in as ((1, 2)), instead of [[2]], as the package currently assumes that a channel size is passed in as well. I hope that helps! Is the output of 6 parameters what you were expecting?

bimsarapathiraja commented 3 years ago
class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.bias = nn.Parameter(torch.Tensor([0]))

    def forward(self, x):
        return x + self.bias

m = Model()

Model works for (10, 200, 200) input. But I can't get the summary using torchsummary. Could you help me?