【发布时间】:2020-10-04 14:12:22
【问题描述】:
问题定义:
我必须使用MSELoss 函数来定义分类问题的损失。因此它一直在说关于张量形状的错误信息。
整个错误信息:
torch.Size([32, 10]) torch.Size([32]) -------------------------------------------------- ------------------------- RuntimeError Traceback(最近调用 最后)在 53 输出 = model.forward(图像) 54 打印(输出.形状,标签.形状) ---> 55 损失 = 标准(输出,标签) 56 损失.backward() 57 优化器.step()
/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py 调用(self, *input, **kwargs) 530 结果 = self._slow_forward(*输入,**kwargs) 531 其他: --> 532 结果 = self.forward(*input, **kwargs) 533 for hook in self._forward_hooks.values(): 534 hook_result = hook(自我,输入,结果)
/opt/conda/lib/python3.7/site-packages/torch/nn/modules/loss.py 在 前进(自我,输入,目标) 429 430 def forward(自我,输入,目标): --> 431 返回 F.mse_loss(输入,目标,reduction=self.reduction) 432 第433章
/opt/conda/lib/python3.7/site-packages/torch/nn/functional.py 在 mse_loss(输入,目标,size_average,减少,减少)2213
ret = torch.mean(ret) if reduction == 'mean' else torch.sum(ret)
2214 其他: -> 2215 扩展输入,扩展目标 = torch.broadcast_tensors(输入,目标)2216 ret = torch._C._nn.mse_loss(expanded_input,expanded_target, _Reduction.get_enum(reduction)) 2217 返回ret/opt/conda/lib/python3.7/site-packages/torch/functional.py 在 广播张量(*张量) 50 [0, 1, 2]]) 51 """ ---> 52 返回 torch._C._VariableFunctions.broadcast_tensors(tensors) 53 54
> RuntimeError: 张量 a (10) 的大小必须与张量的大小匹配 b (32) 在非单一维度 1
如何重塑张量,我应该改变哪个张量(输出或标签)来计算损失?
完整的代码附在下面。
import numpy as np
import torch
# Loading the Fashion-MNIST dataset
from torchvision import datasets, transforms
# Get GPU Device
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
transform = transforms.Compose([transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,))])
# Download and load the training data
trainset = datasets.FashionMNIST('MNIST_data/', download = True, train = True, transform = transform)
testset = datasets.FashionMNIST('MNIST_data/', download = True, train = False, transform = transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size = 32, shuffle = True, num_workers=4)
testloader = torch.utils.data.DataLoader(testset, batch_size = 32, shuffle = True, num_workers=4)
# Examine a sample
dataiter = iter(trainloader)
images, labels = dataiter.next()
# Define the network architecture
from torch import nn, optim
import torch.nn.functional as F
model = nn.Sequential(nn.Linear(784, 128),
nn.ReLU(),
nn.Linear(128, 10),
nn.LogSoftmax(dim = 1))
model.to(device)
# Define the loss
criterion = nn.MSELoss()
# Define the optimizer
optimizer = optim.Adam(model.parameters(), lr = 0.001)
# Define the epochs
epochs = 5
train_losses, test_losses = [], []
for e in range(epochs):
running_loss = 0
for images, labels in trainloader:
# Flatten Fashion-MNIST images into a 784 long vector
images = images.to(device)
labels = labels.to(device)
images = images.view(images.shape[0], -1)
# Training pass
optimizer.zero_grad()
output = model.forward(images)
print(output.shape, labels.shape)
loss = criterion(output, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
else:
test_loss = 0
accuracy = 0
# Turn off gradients for validation, saves memory and computation
with torch.no_grad():
# Set the model to evaluation mode
model.eval()
# Validation pass
for images, labels in testloader:
images = images.to(device)
labels = labels.to(device)
images = images.view(images.shape[0], -1)
ps = model(images)
test_loss += criterion(ps, labels)
top_p, top_class = ps.topk(1, dim = 1)
equals = top_class == labels.view(*top_class.shape)
accuracy += torch.mean(equals.type(torch.FloatTensor))
model.train()
print("Epoch: {}/{}..".format(e+1, epochs),
"Training loss: {:.3f}..".format(running_loss/len(trainloader)),
"Test loss: {:.3f}..".format(test_loss/len(testloader)),
"Test Accuracy: {:.3f}".format(accuracy/len(testloader)))
【问题讨论】:
-
把所有的错误日志都放到最后一行
-
@NatthaphonHongcharoen 感谢您的评论。我附上了错误消息的整行。