自动求导机制
import torch
x = torch.rand(1)
w = torch.rand(1, requires_grad=True)
b = torch.rand(1, requires_grad=True)
y = w * x
z = b + y
print("x.requires_grad = ", x.requires_grad)
print("w.requires_grad = ", w.requires_grad)
print("b.requires_grad = ", b.requires_grad)
print("y.requires_grad = ", y.requires_grad)
print("z.requires_grad = ", z.requires_grad)
z.backward(retain_graph=True)
print("w.grad=", w.grad)
print("b.grad", b.grad)
torch_stack
import numpy as np
import torch
a = torch.from_numpy(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]))
b = torch.from_numpy(np.array([[10, 20, 30], [40, 50, 60], [70, 80, 90]]))
c = torch.from_numpy(np.array([[11, 21, 31], [41, 51, 61], [71, 81, 91]]))
d = torch.stack((a, b, c), dim=2)
print("a={}\nb={}\nc={}\ndim_2={}\n".format(a, b, c, d))
dim_0 = torch.stack((a, b, c), dim=0)
print("dim_0=", dim_0)
dim_1 = torch.stack((a, b, c), dim=1)
print("dim_1=", dim_1)