自动求导机制-代码实现

自动求导机制

import torch

# x = torch.randn(3,4,requires_grad=True) #requires_grad=True 这个的意思是指在训练的过程中可以对当前指定的x进行求导了。
# print(x)

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 = ", x)
print("x.requires_grad = ", x.requires_grad)
# print("w = ", w)
print("w.requires_grad = ", w.requires_grad)
# print("b = ", b)
print("b.requires_grad = ", b.requires_grad)
# print("y = ", y)
print("y.requires_grad = ", y.requires_grad)
# print("z = ", z)
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)

你可能感兴趣的:(深度学习,深度学习,人工智能,机器学习)