Expected object of scalar type Float but got scalar type Long for argument #2 'target'

# 损失函数 
# nn.MSELoss 来计算均方误差
# nn.CrossEntropyLoss 来计算交叉熵损失
output = net(input)
target = Variable(t.arange(0, 10))
# target = target.float()
criterion = nn.MSELoss()
loss = criterion(output, target)
loss

在上面注释代码里加上一行即可

target = target.float()

得到答案

tensor(28.3906, grad_fn=)

发布了182 篇原创文章 · 获赞 101 · 访问量 20万+

猜你喜欢

转载自blog.csdn.net/lancecrazy/article/details/99879360
今日推荐