pyTorch求梯度实例
import torch x=torch.tensor(3.0, requires_grad=True) z=torch.tensor(4.0, requires_grad=True) y=x**2+z**3 y.backward() print('dy/dx = 2x:', x.grad) print('dy/dz = 3x^3:',z.grad)
import torch x=torch.tensor(3.0, requires_grad=True) z=torch.tensor(4.0, requires_grad=True) y=x**2+z**3 y.backward() print('dy/dx = 2x:', x.grad) print('dy/dz = 3x^3:',z.grad)