如何从输出层获取连续值
How to get continuous value from output layer
我有一个代码(来自 here)来对 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 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: The size of tensor a
(10)
must match the size of tensor b
(2)
at non-singleton dimension 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>)
我有一个代码(来自 here)来对 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 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: 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>)