说明此时需要float型数据,但识别到long型数据,此时需要对入参和出参做一下类型转换
output=output.to(torch.float32)
target=target.to(torch.float32)
举例
output =net(input)
target = variable(t.arange(0,10))
#the point
output=output.to(torch.float32)
target=target.to(torch.float32)
criterion = nn.MSELoss()
loss = criterion(output,target)
net.zero_grad()
print("反向传播之前conv1.bias的梯度")
print(net.conv1.bias.grad)
loss.backward() #此处疑难杂症 先跳过
print("反向传播之后conv1.bias的梯度")
print(net.conv1.bias.grad)
RuntimeError: Found dtype Long but expected Float - 简书以上成功解决我的bug
还有一种
PyTorch出现错误“RuntimeError: Found dtype Double but expected Float”_墨门-CSDN博客
torch_tensor = torch_tensor.float()