【发布时间】:2020-04-30 07:38:35
【问题描述】:
我正在使用models.vgg16(pretrained=True) 模型进行图像分类,其中类数 = 3。
批量大小为 12 trainloader = torch.utils.data.DataLoader(train_data, batch_size=12, shuffle=True),因为错误为 Target size (torch.Size([12])) must be the same as input size (torch.Size([12, 1000]))
我已经更改了最后一个 fc 层参数,并将最后一个 FC 层设置为 Linear(in_features=1000, out_features=3, bias=True)
损失函数为BCEWithLogitsLoss()
criterion = nn.BCEWithLogitsLoss()
optimizer = optim.SGD(vgg16.parameters(), lr=0.001, momentum=0.9)
训练代码是
# zero the parameter gradients
optimizer.zero_grad()
outputs = vgg16(inputs) #----> forward pass
loss = criterion(outputs, labels) #----> compute loss #error occurs here
loss.backward() #----> backward pass
optimizer.step() #----> weights update
在计算损失时,我收到此错误Target size (torch.Size([12])) must be the same as input size (torch.Size([12, 1000]))
代码位于:code
【问题讨论】:
-
请不要包含图片,而是直接将相关代码复制到您的问题中。
标签: deep-learning pytorch conv-neural-network vgg-net