你好,我用pytorch写了一个vgg16网络结构的代码,但是运行会报错:name 'self' is not defined。能帮我看看哪错了吗,原始代码如下:import torch import torchvision import torch.nn as nn class VGG16(nn.Module): def __init__(in_channels = 3,out_channels = 1000,num_hidden = 50288): super(VGG16, self).__init__() self.features = nn.Sequential( nn.Conv2d(in_channels,64,3,1,1), nn.ReLU(inplace=True), nn.Conv2d(64,64,3,1,1), nn.ReLU(inplace=True), nn.MaxPool2d(2,2), nn.Conv2d(64,128,3,1,1), nn.ReLU(inplace=True), nn.Conv2d(128,128,3,1,1), nn.ReLU(inplace=True), nn.MaxPool2d(2,2), nn.Conv2d(128, 256, 3, 1, 1), nn.ReLU(inplace=True), nn.Conv2d(256, 256, 3, 1, 1), nn.ReLU(inplace=True), nn.Conv2d(256, 256, 3, 1, 1), nn.ReLU(inplace=True), nn.MaxPool2d(2, 2), nn.Conv2d(256, 512, 3, 1, 1), nn.ReLU(inplace=True), nn.Conv2d(512, 512, 3, 1, 1), nn.ReLU(inplace=True), nn.Conv2d(512, 512, 3, 1, 1), nn.ReLU(inplace=True), nn.MaxPool2d(2, 2), nn.Conv2d(256, 512, 3, 1, 1), nn.ReLU(inplace=True), nn.Conv2d(512, 512, 3, 1, 1), nn.ReLU(inplace=True), nn.Conv2d(512, 512, 3, 1, 1), nn.ReLU(inplace=True), nn.MaxPool2d(2, 2), ) self.avgpool = nn.AdaptiveAvgPool2d(output_size=(7,7)) self.classifier = nn.Sequential( nn.Linear(num_hidden,4096), nn.ReLU(), nn.Dropout(), nn.Linear(4096, 4096), nn.ReLU(), nn.Dropout(), nn.Linear(4096,out_channels) ) def forward(self,x): x = self.features(x) x = self.avgpool(x) x = torch.flatten(x,1) x = self.classifer(x) return x vgg = VGG16() print(vgg(3,1000,50288))
时间: 2023-03-09 16:03:31 浏览: 129
看起来应该是在类VGG16的__init__函数中定义self的时候出错了,可能是因为super(VGG16, self).__init__()前面多了一个空格,应该是super(VGG16,self).__init__(),应该把空格去掉就可以了。
阅读全文