报错:Expected object of scalar type Float but got scalar type Long for argument #2 'target'
程序员文章站
2022-06-15 13:42:28
...
import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self):
#找到Net的父类,将其转换为父类,再调用自己的 __init__()
super(Net,self).__init__()
# in_channels,out_channels,kernel_size
self.conv1 = nn.Conv2d(1,6,5)
self.conv2 = nn.Conv2d(6,16,5)
#全连接层,y = wx + b
self.fc1 = nn.Linear(16*5*5,120)
self.fc2 = nn.Linear(120,84)
self.fc3 = nn.Linear(84,10)
# 定义好forward函数,backwa会被自动实现
def forward(self, x):
#最大池化 ,步幅是2*2
x = F.max_pool2d(F.relu(self.conv1(x)),(2,2))
#大小为正方形,则只能指定一个数字
x = F.max_pool2d(F.relu(self.conv2(x)),2)
x = x.view(x.size(0),16*5*5) #改变x的大小
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
def num_flat_features(self,x):
size = x.size()[:1]
num_features = 1
for s in size:
num_features *=s
return num_features
net = Net()
input = Variable(torch.randn(1,1,32,32))
output = net(input)
target = Variable(torch.arange(1,11))#假设的target :1,2,3,4,5,6,7,8,9,10
criterion = nn.MSELoss()
loss = criterion(output,target)
print(loss)
运行上述代码时报错:
Expected object of scalar type Float but got scalar type Long for argument #2 ‘target’
解决:将target类型转换为float
target = target.float()
即可
运行结果:
推荐阅读
-
报错:Expected object of scalar type Float but got scalar type Long for argument #2 'target'
-
RuntimeError: Expected tensor for argument #1 'indices' to have scalar type Long; but got CUDAType
-
RuntimeError: Expected object of scalar type Long but got scalar type Int for argument #2 'target'
-
RuntimeError: Expected tensor for argument #1 ‘indices‘ to have scalar type Long; but got CUDAFloatT
-
RuntimeError: Expected tensor for argument #1 ‘indices‘ to have scalar type Long; but got torch.IntT
-
Expected tensor for argument #1 ‘indices‘ to have scalar type Long; but got torch.cuda.FloatTensor i
-
Expected object of scalar type Long but got scalar type Int for argument #2 'target'
-
RuntimeError: Expected object of scalar type Long but got scalar type Float for argument #2 'target'
-
Expected object of scalar type Long but got scalar type Double for argument #2 'target'
-
Expected object of scalar type Float but got scalar type Long for argument #2 'target'