Pytorch implements handwritten digit recognition

as title shows,datasat is MINST. but i meet the problem that the output of model is null.
the code as follows:

class Model(torch.nn.Module):
    def _init_(self):
        super(Model,self)._init_()
        self.conv1=torch.nn.Sequential(
            torch.nn.Conv2d(1,64,kernel_size=3,stride=1,padding=1),
            torch.nn.ReLU(),
            torch.nn.Conv2d(64,128,kernel_size=3,stride=1,padding=1),
            torch.nn.ReLU(),
            torch.nn.MaxPool2d(stride=2,kernel_size=2))
        self.dense=torch.nn.Sequential(
            torch.nn.Linear(14*14*128,1024),
            torch.nn.ReLU(),
            torch.nn.Dropout(p=0.5),
            torch.nn.Linear(1024,10))
    def forward(self,x):
            x=self.conv1(x)
            x=x.view(-1,14*14*128)
            x=self.dense(x)
            return x
model=Model()
print(model)

the output of model is :
Model()

that’s all. I try to solve it for many times.but no solution. I’m newcomer study on pytorch. thanks for your attention.

That’s strange. I jused exactly your code and with

print(model) I get the result as expected:

  (conv1): Sequential(
    (0): Conv2d(1, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (1): ReLU()
    (2): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (3): ReLU()
    (4): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
  )
  (dense): Sequential(
    (0): Linear(in_features=25088, out_features=1024, bias=True)
    (1): ReLU()
    (2): Dropout(p=0.5)
    (3): Linear(in_features=1024, out_features=10, bias=True)
  )
)

Can you post your code formatted? Maybe there are some indents wrong…

You can format your code using three backticks (`) before and after the code. Also make sure, the code you posted is indented the same way as the code you executed, because otherwise we won’t be aple to reproduce this.

you’re right.I will upload code that meet the format. but the problem not result in format. thanks for your attention.:grinning:

I guess it’s because you are missing two underscores in __init__.
Currently your code just uses one underscore on each side.

You are right, I totally missed this, and corrected it without noticing, since I couldn’t copy the code but had to retype it by myself :smiley:

1 Like

Automatic error detection :wink:

2 Likes

thanks, I have solved it according to your clue. ‘-’-‘init’-’-’.