Why the output of my model is None?

here is my model:

class vgg16Net(nn.Module):
def init(self):
super(vgg16Net, self).init()
vgg = models.vgg16_bn(pretrained=True)
self.block1 = nn.Sequential(*list(vgg.features.children())[:7])
self.block2 = nn.Sequential(*list(vgg.features.children())[7:14])
self.block3 = nn.Sequential(*list(vgg.features.children())[14:24])
self.block4 = nn.Sequential(*list(vgg.features.children())[24:34])
self.block5 = nn.Sequential(*list(vgg.features.children())[34:],
nn.AdaptiveAvgPool2d(1))
self.classifier = nn.Sequential(nn.Linear(512, 10))

def forward(self, x):
    x = self.block1(x)
    x = self.block2(x)
    x = self.block3(x)
    x = self.block4(x)
    x = self.block5(x)
    x = x.view(x.shape[0],-1)
    x = self.classifier(x)

vggmodel = vgg16Net()
vggmodel = vgg16.cuda()
device = torch.device(“cuda:0”)
inputs = torch.randn((4,3,224,224))
inputs = inputs.to(device)
outputs = vggmodel(inputs) #get a None type object

It looks like you forgot to return x.

3 Likes

you need to return the output of forward

def forward(self, x):
    x = self.block1(x)
    x = self.block2(x)
    x = self.block3(x)
    x = self.block4(x)
    x = self.block5(x)
    x = x.view(x.shape[0],-1)
    x = self.classifier(x)
    return x

thanks, it work.
sorry for a so stupid problem, I am a newer to pytorch