更新中~
PyTorch版《动手学深度学习》中提供的代码在最新版本pytorch上会报错。这里记录一下debug。
第三章代码报错记录
3.2节
1、RuntimeError: Expected object of scalar type Double but got scalar type Float for argument #2 'mat2'
解决:
def linreg(X, w, b): # 本函数已保存在d2lzh_pytorch包中⽅便以后使用
X = torch.tensor(X, dtype=torch.float32)
return torch.mm(X, w) + b
2、UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
解决:
#w = torch.tensor(np.random.normal(0, 0.01, (num_inputs, 1)), dtype=torch.float32)
w = torch.as_tensor(np.random.normal(0, 0.01, (num_inputs, 1)), dtype=torch.float32)
b = torch.zeros(1, dtype=torch.floa