mirror of
https://github.com/fendouai/PyTorchDocs.git
synced 2025-10-20 21:33:46 +08:00
14 lines
450 B
Python
14 lines
450 B
Python
from __future__ import print_function
|
|
import torch
|
|
|
|
|
|
x = torch.randn((3), dtype=torch.float32, requires_grad=True)
|
|
y = torch.randn((3), dtype=torch.float32, requires_grad=True)
|
|
z = torch.randn((3), dtype=torch.float32, requires_grad=True)
|
|
t = x + y
|
|
loss = t.dot(z) # 求向量的内积
|
|
|
|
loss.backward(retain_graph=True)
|
|
print(z, x.grad, y.grad) # 预期打印出的结果都一样
|
|
print(t, z.grad) # 预期打印出的结果都一样
|
|
print(t.grad) |