RuntimeError: Found dtype Long but expected Float

说明此时需要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)