本文共 2238 字,大约阅读时间需要 7 分钟。
import torchx = torch.randn(3,4,requires_grad=True) #对指定的X进行求导b = torch.randn(3,4,requires_grad=True)t = x+by=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 torchx = torch.rand(1)b = torch.rand(1, requires_grad=True)w = torch.rand(1, requires_grad=True)y = w * xz = y + bprint(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 torchimport torch.nn as nnimport numpy as np#大多时候对图像等进行数据读入都为np.array 需要转换#先构建x、yx_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+1input_dim = 1output_dim = 1model = LinearRegressionModel(input_dim, output_dim)print(model)#指定好参数和损失函数进行训练epochs = 1000learning_rate = 0.01optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)#优化器SGDcriterion = 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'))
转载地址:http://vxtgn.baihongyu.com/