Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Double counts parameters if same later called twice. #201

Open
sjamthe opened this issue Feb 5, 2024 · 0 comments
Open

Double counts parameters if same later called twice. #201

sjamthe opened this issue Feb 5, 2024 · 0 comments

Comments

@sjamthe
Copy link

sjamthe commented Feb 5, 2024

In the SimpleConv example given in README it show model has 20 parameters but in reality it only has 10 trainable parameters.
As the self.features is called twice it is double counting the parameters.

Try the following:
print(model)
SimpleConv(
(features): Sequential(
(0): Conv2d(1, 1, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(1): ReLU()
)
)
print(model.features[0].weight.numel(), model.features[0].bias.numel())
9 1

model

`import torch
import torch.nn as nn
from torchsummary import summary

class SimpleConv(nn.Module):
def init(self):
super(SimpleConv, self).init()
self.features = nn.Sequential(
nn.Conv2d(1, 1, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
)

def forward(self, x, y):
    x1 = self.features(x)
    x2 = self.features(y)
    return x1, x2

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = SimpleConv().to(device)

summary(model, [(1, 16, 16), (1, 28, 28)])`

`----------------------------------------------------------------
Layer (type) Output Shape Param #

        Conv2d-1            [-1, 1, 16, 16]              10
          ReLU-2            [-1, 1, 16, 16]               0
        Conv2d-3            [-1, 1, 28, 28]              10
          ReLU-4            [-1, 1, 28, 28]               0

================================================================
Total params: 20
Trainable params: 20
Non-trainable params: 0

Input size (MB): 0.77
Forward/backward pass size (MB): 0.02
Params size (MB): 0.00
Estimated Total Size (MB): 0.78
----------------------------------------------------------------`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant