From: https://github.com/L1aoXingyu/code-of-learn-deep-learning-with-pytorch
线性回归是监督学习里面一个非常简单的模型,同时梯度下降也是深度学习中应用最广的优化算法,我们将从这里开始我们的深度学习之旅
生成数据
x_train = np.array([[3.3], [4.4], [5.5], [6.71], [6.93], [4.168],
[9.779], [6.182], [7.59], [2.167], [7.042],
[10.791], [5.313], [7.997], [3.1]], dtype=np.float32)
y_train = np.array([[1.7], [2.76], [2.09], [3.19], [1.694], [1.573],
[3.366], [2.596], [2.53], [1.221], [2.827],
[3.465], [1.65], [2.904], [1.3]], dtype=np.float32)
plt.plot(x_train, y_train, 'bo')
# 模型训练数据
x_train = torch.from_numpy(x_train)
y_train = torch.from_numpy(y_train)
w = Variable(torch.randn(1), requires_grad=True)
b = Variable(torch.zeros(1), requires_grad=True)
print(w)
tensor([1.1085], requires_grad=True)
模型
x_train = Variable(x_train)
y_train = Variable(y_train)
def linear_model(x):
return x * w + b
预测
y_predict = linear_model(x_train)
def show_predict(x_train, y_train, y_predict):
plt.plot(x_train.data.numpy(), y_train.data.numpy(), 'bo', label='real')
plt.plot(x_train.data.numpy(), y_predict.data.numpy(), 'ro', label='estimated')
plt.legend()
show_predict(x_train, y_train, y_predict)
计算损失
def get_loss(y, y_predict):
return torch.mean((y - y_predict)**2)
loss = get_loss(y_train, y_predict)
print(loss)
tensor(23.5047, grad_fn=<MeanBackward1>)
自动求导
loss.backward()
print(w.grad)
print(b.grad)
tensor([62.7573])
tensor([8.7960])
更新参数, 重新拟合
w.data = w.data - 1e-2 * w.grad.data
b.data = b.data - 1e-2 * b.grad.data
y_predict = linear_model(x_train)
show_predict(x_train, y_train, y_predict)
迭代多次训练
for i in range(20):
y_predict = linear_model(x_train)
loss = get_loss(y_train, y_predict)
# 归零梯度
w.grad.zero_()
b.grad.zero_()
# 自动求导
loss.backward()
w.data = w.data - 1e-2 * w.grad.data
b.data = b.data - 1e-2 * b.grad.data
print('epoch: {}, loss:{}'.format(i, loss.data))
y_predict = linear_model(x_train)
show_predict(x_train, y_train, y_predict)
epoch: 0, loss:0.24172908067703247
epoch: 1, loss:0.2413555085659027
epoch: 2, loss:0.24098394811153412
epoch: 3, loss:0.24061423540115356
epoch: 4, loss:0.24024644494056702
epoch: 5, loss:0.2398805171251297
epoch: 6, loss:0.239516481757164
epoch: 7, loss:0.23915432393550873
epoch: 8, loss:0.2387939989566803
epoch: 9, loss:0.2384355068206787
epoch: 10, loss:0.23807893693447113
epoch: 11, loss:0.23772411048412323
epoch: 12, loss:0.23737111687660217
epoch: 13, loss:0.23702001571655273
epoch: 14, loss:0.2366706281900406
epoch: 15, loss:0.2363230437040329
epoch: 16, loss:0.23597723245620728
epoch: 17, loss:0.23563320934772491
epoch: 18, loss:0.235290989279747
epoch: 19, loss:0.2349504828453064
多项式回归
w_target = np.array([0.5, 3, 2.4])
b_target = np.array([0.9])
# y = 0.90 + 0.50 * x + 3.00 * x^2 + 2.40 * x^3
x_sample = np.arange(-3, 3.1, 0.1)
y_sample = b_target[0] + w_target[0] * x_sample + w_target[1] * x_sample**2 + w_target[2] * x_sample**3
plt.plot(x_sample, y_sample)
plt.show()
x_train = torch.from_numpy(np.stack([x_sample**i for i in range(1, 4)], axis=1)).float()
y_train = torch.from_numpy(y_sample).float().unsqueeze(1)
x_train = Variable(x_train)
y_train = Variable(y_train)
def multi_linear(x):
return torch.mm(x, w) + b
w = Variable(torch.randn(3, 1), requires_grad=True)
b = Variable(torch.ones(1), requires_grad=True)
for i in range(100):
y_predict = multi_linear(x_train)
loss = get_loss(y_train, y_predict)
# 归零梯度
w.grad.zero_()
b.grad.zero_()
# 自动求导
loss.backward()
w.data = w.data - 0.001 * w.grad.data
b.data = b.data - 0.001 * b.grad.data
if i//10 ==0:
print('epoch: {}, loss:{}'.format(i, loss.data))
y_predict = multi_linear(x_train)
plt.plot(x_train.data.numpy()[:,0], y_train.data.numpy(), label='real', c='r')
plt.plot(x_train.data.numpy()[:,0], y_predict.data.numpy(), label='predict', c='b')
plt.legend()
epoch: 0, loss:1213.7315673828125
epoch: 1, loss:797.805419921875
epoch: 2, loss:548.1760864257812
epoch: 3, loss:396.3459777832031
epoch: 4, loss:302.1776428222656
epoch: 5, loss:242.14144897460938
epoch: 6, loss:202.434326171875
epoch: 7, loss:174.94998168945312
epoch: 8, loss:154.91954040527344
epoch: 9, loss:139.5298614501953