import torch
x = torch.randn(3,4,requires_grad=True) #对指定的X进行求导
b = torch.randn(3,4,requires_grad=True)
t = x+b
y=t.sum()
print(y)
y.backward() #执行反向传播
print(b.grad)
print(x.requires_grad, b.requires_grad, t.requires_grad) #自动计算t 自动指定为true
for example:
z=b+y
y=x*w
(求偏导逐层来做 链式求导)
import torch
x = torch.rand(1)
b = torch.rand(1, requires_grad=True)
w = torch.rand(1, requires_grad=True)
y = w * x
z = y + b
print(x.requires_grad, b.requires_grad, w.requires_grad, y.requires_grad)
# dw/dx 要先通过对y求导 故y也需要
print(x.is_leaf, w.is_leaf, b.is_leaf, y.is_leaf, z.is_leaf)
#检测是否为leaf节点 True True True False False
#反向传播的计算
z.backward(retain_graph=True) #如果对梯度不清零 会累加 实际训练模型时一般不需要累加
print(w.grad)
print(b.grad)
反向传播操作全部已经封装在函数内。
import torch
import torch.nn as nn
import numpy as np
#大多时候对图像等进行数据读入都为np.array 需要转换
#先构建x、y
x_values = [i for i in range(11)]
x_train = np.array(x_values, dtype=np.float32)
x_train = x_train.reshape(-1, 1) #转为矩阵
print(x_train.shape)
y_values = [2*i + 1 for i in x_values]
y_train = np.array(y_values, dtype=np.float32)
y_train = y_train.reshape(-1, 1)
print(y_train.shape)
#其实线性回归就是一个不加激活函数的全连接层
class LinearRegressionModel(nn.Module):
def __init__(self, input_dim, output_dim):
super(LinearRegressionModel, self).__init__()
self.linear = nn.Linear(input_dim, output_dim)
def forward(self, x):
out = self.linear(x)
return out
#y=2x+1
input_dim = 1
output_dim = 1
model = LinearRegressionModel(input_dim, output_dim)
print(model)
#指定好参数和损失函数进行训练
epochs = 1000
learning_rate = 0.01
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
#优化器SGD
criterion = nn.MSELoss()
#定义损失函数 回归函数->MSE
#训练模型
for epoch in range(epochs):
epoch += 1
# 注意转行成tensor
inputs = torch.from_numpy(x_train)
labels = torch.from_numpy(y_train)
# 梯度要清零每一次迭代
optimizer.zero_grad()
# 前向传播
outputs = model(inputs)
# 计算损失
loss = criterion(outputs, labels)
# 返向传播
loss.backward()
# 更新权重参数
optimizer.step()
if epoch % 5 == 0:
print('epoch {}, loss {}'.format(epoch, loss.item()))
#测试模型预测结果
predicted = model(torch.from_numpy(x_train).requires_grad_()).data.numpy()
print(predicted)
#模型的保存与读取
torch.save(model.state_dict(), 'model.pkl')
model.load_state_dict(torch.load('model.pkl'))
因篇幅问题不能全部显示,请点此查看更多更全内容