蓝灯123

导航

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)

 

posted on 2022-01-20 22:29  蓝灯123  阅读(37)  评论(0编辑  收藏  举报