我有一段代码(来自这里),用于分类MINST数字。代码运行正常。他们使用了CrossEntropyLoss
和Adam
优化器。
模型代码如下
class CNN(nn.Module): def __init__(self): super(CNN, self).__init__() self.conv1 = nn.Sequential( nn.Conv2d( in_channels=1, out_channels=16, kernel_size=5, stride=1, padding=2, ), nn.ReLU(), nn.MaxPool2d(kernel_size=2), ) self.conv2 = nn.Sequential( nn.Conv2d(16, 32, 5, 1, 2), nn.ReLU(), nn.MaxPool2d(2), ) # fully connected layer, output 10 classes self.out = nn.Linear(32 * 7 * 7, 10) # self.softmax = torch.nn.Softmax(dim=1) def forward(self, x): x = self.conv1(x) x = self.conv2(x) # flatten the output of conv2 to (batch_size, 32 * 7 * 7) x = x.view(x.size(0), -1) output = self.out(x) # output = self.softmax(output) return output, x # return x for visualizationThe shape of the `b_x` and `b_y` is
torch.Size([100, 1, 28, 28]) torch.Size([100])
现在,我希望从输出层获得连续值。比如,我希望输出类似于1.0、0.9、8.6、7.0等。如果输出层的值是1.0且标签是1,那就意味着预测是完美的。否则,就是不完美的。更简单地说,我想把MNIST数字视为一个回归问题。
因此,我将损失函数改成了MSELoss
,优化器改成了SGD
(代码的其余部分与网站上的保持一致)。但现在,我遇到了一个错误
/home/Opps_0/.local/lib/python3.8/site-packages/torch/nn/modules/loss.py:528: UserWarning: Using a target size (torch.Size([100])) that is different to the input size (torch.Size([100, 10])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size. return F.mse_loss(input, target, reduction=self.reduction)Traceback (most recent call last): File "/usr/lib/python3.8/runpy.py", line 194, in _run_module_as_main return _run_code(code, main_globals, None, File "/usr/lib/python3.8/runpy.py", line 87, in _run_code exec(code, run_globals) File "/home/Opps_0/Desktop/MNIST/src/train.py", line 60, in <module> train(NB_EPOCS, model, loaders) File "/home/Opps_0/Desktop/MNIST/src/train.py", line 45, in train loss = criterion(output, b_y) File "/home/Opps_0/.local/lib/python3.8/site-packages/torch/nn/modules/module.py", line 889, in _call_impl result = self.forward(*input, **kwargs) File "/home/Opps_0/.local/lib/python3.8/site-packages/torch/nn/modules/loss.py", line 528, in forward return F.mse_loss(input, target, reduction=self.reduction) File "/home/Opps_0/.local/lib/python3.8/site-packages/torch/nn/functional.py", line 2925, in mse_loss expanded_input, expanded_target = torch.broadcast_tensors(input, target) File "/home/Opps_0/.local/lib/python3.8/site-packages/torch/functional.py", line 74, in broadcast_tensors return _VF.broadcast_tensors(tensors) # type: ignoreRuntimeError: The size of tensor a (10) must match the size of tensor b (100) at non-singleton dimension 1
你能告诉我如何修改才能从输出层获得连续值吗?
回答:
假设你的目标形状为(batch_size,)
,类似于以下内容:
>>> model = CNN() >>> criterion = nn.MSELoss() >>> output, _ = model(torch.rand(2, 1, 28, 28)) >>> b_y = torch.randint(0, 10, (2,)) tensor([1, 2, 6, 5, 7])
使用MSELoss
计算损失会导致:
>>> loss = criterion(output, b_y)
RuntimeError: The size of tensor
a
(10)
must match the size of tensorb
(2)
at non-singleton dimension1
.
这意味着你的目标b_y
的形状不正确,它需要匹配output
的形状,即它需要是一个二维张量。
由于你正在用回归损失优化这个任务,你可以将你的目标编码为稀疏向量,也称为独热编码。你可以使用内置的torch.nn.functional.one_hot
轻松做到这一点:
>>> ohe_target = torch.nn.functional.one_hot(b_y, num_classes=10)tensor([[0, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1, 0, 0]])
现在你可以正确地计算损失:
>>> criterion(output, ohe_target)tensor(0.1169, grad_fn=<MseLossBackward>)