如何从输出层获得连续值



我有一个代码(从这里)来分类MINST数字。代码运行正常。这里他们使用了CrossEntropyLossAdam优化器。

模型代码如下

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 visualization
The 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: ignore
RuntimeError: 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:张量a(10)的大小必须与张量b(2)在非单维1的大小相匹配。

这意味着你的目标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>)

最新更新