Pytorch学习之旅(1)——Hello Pytorch!

Pytorch学习之旅(1)——Hello Pytorch!

代码块:

import torch
from torch import autograd

x = torch.tensor(1.)
a = torch.tensor(1., requires_grad=True)
b = torch.tensor(2., requires_grad=True)
c = torch.tensor(3., requires_grad=True)

y = a**2*x + b*x + c
print('Before:', a.grad, b.grad, c.grad)

grads = autograd.grad(y, [a, b, c])
print('After:', grads[0], grads[1], grads[2])

运行结果:

Before: None None None
After: tensor(2.) tensor(1.) tensor(1.)
发布了27 篇原创文章 · 获赞 1 · 访问量 996

猜你喜欢

转载自blog.csdn.net/qq_41320782/article/details/103930104
今日推荐