我在试着训练我的模特。我的模型输出一个4,2张量,其中4是批大小,2是二进制分类。在收到输出后,我找到了每个row.so的最大元素的索引,现在形状是4,1,标签的形状也是4,1,我不明白为什么我仍然得到这个error.Could --请有人帮我解决它-- out.Also,我使用的优化器是SGD,损失标准是交叉熵。
for epoch in range(2): # loop over the dataset multiple times
running_loss = 0.0
for i, data in enumerate(dataloader, 0):
# get the inputs; data is a list of [inputs, labels]
#inputs, labels = data
inputs, labels = \
data['image'], data['Status']
# zero the parameter gradients
optimizer.zero_grad()
outputs = net(inputs.float())
a=torch.max(outputs,1).indices
a=a.reshape(4,1)
a=a.float()
labels=labels.float()
print(a.shape,labels.shape)
loss = criterion(a, labels)
loss.backward()
optimizer.step()
# print statistics
running_loss += loss.item()
if i % 2000 == 1999: # print every 2000 mini-batches
print('[%d, %5d] loss: %.3f' %
(epoch + 1, i + 1, running_loss / 2000))
running_loss = 0.0
print('Finished Training')torch.Size([4, 1]) torch.Size([4, 1])
RuntimeError Traceback (most recent call last)
<ipython-input-83-72f63a4db63e> in <module>()
22 labels=labels.float()
23 print(a.shape,labels.shape)
---> 24 loss = criterion(a, labels)
25 loss.backward()
26 optimizer.step()
2 frames
/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction)
2822 if size_average is not None or reduce is not None:
2823 reduction = _Reduction.legacy_get_string(size_average, reduce)
-> 2824 return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
2825
2826
RuntimeError: 1D target tensor expected, multi-target not supported此外,模式是:
import torch
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
# 1 input image channel, 16 output channels, 5x5 square convolution
# kernel
self.conv1 = nn.Conv2d(1, 16, 5)
self.conv2 = nn.Conv2d(16, 32, 7)
self.dropout1 = nn.Dropout2d(0.25)
self.dropout2 = nn.Dropout2d(0.5)
self.fc1 = nn.Linear(4608,128)
self.fc2 = nn.Linear(128,16)
self.fc3 = nn.Linear(16, 2)
def forward(self, x):
# Max pooling over a (2, 2) window
x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
# If the size is a square, you can specify with a single number
x = F.max_pool2d(F.relu(self.conv2(x)), 2)
x = self.dropout1(x)
x = torch.flatten(x, 1) # flatten all dimensions except the batch dimension
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.dropout2(x)
x =self.fc3(x)
return x
net = Net()
net = net.float()
print(net)发布于 2021-08-29 18:18:23
如果这是一个二进制分类问题,那么您的模型只需要预测一个输出--一个介于0到1之间的值。接近0的预测值表示输入很可能属于您的第一类,而接近1的预测值表示可能属于第二类的输入。
然后,可以使用损失函数(如nn.BCELoss(prediction, target)或nn.BCEWithLogitsLoss(prediction, target) )对模型进行优化。这将避免当前出现的错误,因为您不会处理预测的多个输出值。
https://datascience.stackexchange.com/questions/100601
复制相似问题