1
0
This commit is contained in:
2024-05-23 20:43:52 +08:00
parent e6da567437
commit 9a33bd6f49
11 changed files with 292 additions and 1 deletions

15
autograd.py Normal file
View File

@@ -0,0 +1,15 @@
import torch
from torch import autograd
device = torch.device('mps')
x = torch.tensor(1.)
a = torch.tensor(2., requires_grad=True)
b = torch.tensor(2., requires_grad=True)
c = torch.tensor(3., requires_grad=True)
y = a ** 2 * x + b * x + c ** 3
print('before:', a.grad, b.grad, c.grad)
grads = autograd.grad(y, [a, b, c])
print('after:', grads[0], grads[1], grads[2])