梯度下降法求解最优解
import torch
def f01():
W = torch.tensor(10,requires_grad=True,dtype=torch.float)
# 学习率
lr = 0.001
for i in range(5000):
# 前向传播
y = W ** 2 + 20
# 防止梯度累加
if W.grad is not None:
W.grad.zero_()
# 反向传播
y.backward()
# 梯度下降法公式
W.data = W.data - lr * W.grad
print(f"第{i}轮,w为{W}",(i,W))
if __name__ == '__main__':
f01()
# 将带有梯度的张量,转换成numpy
def f02():
w = torch.tensor([10.,10.],requires_grad=True,dtype=torch.float)
w1 = w.detach()
print(w.requires_grad) # True
print(w1.requires_grad) #Flase
print(w.data)
print(w1.data)
print(id(w.data))
print(id(w1.data))
w2 = w1.numpy()
print(id(w2),w2,type(w2))
自动微分模块应用
def f03():
x = torch.ones(2,5)
y = torch.zeros(2,3)
# 创建权重w和偏置b
w = torch.randn(5,3,requires_grad=True)
b = torch.randn(3,requires_grad=True)
# 网络输出
z = torch.matmul(x,w) + b
loss = torch.nn.MSELoss()
loss = loss(z,y)
print(loss.item())
loss.backward()
print("w的梯度为:",w.grad)
print("b的梯度为:",b.grad)