错误一:
Mismatch in shape: grad_output[0] has a shape of torch.Size([2]) and output[0] has a shape of torch.Size([2, 2])
错误二
错误原因expected sequence of length 2 at dim 1 (got 1)
在进行 张量 梯度求解时,传入数据维度不对
错误代码示例import torch # 第一步:创建 tensor x = torch.ones(2,2,requires_grad=True) print(x) # 第二步:对 tensor 做处理 y = x**2 print(y) # 第三步:求梯度 y.backward(torch.tensor([[3,2],[2]],dtype=float))# 这里加了一个类型 print(x.grad)正确代码示例
import torch # 第一步:创建 tensor x = torch.ones(2,2,requires_grad=True) print(x) # 第二步:对 tensor 做处理 y = x**2 print(y) # 第三步:求梯度 y.backward(torch.tensor([[3,2],[2,2]],dtype=float))# 这里加了一个类型 print(x.grad)运行结果



