学习来源:李沐老师
线性回归
- 线性模型
- 单层神经网络
- 神经网络源于神经科学
- 衡量预测质量
- 损失函数
- 训练数据
- 参数学习
- 显示解
- 线性回归是对n维输入的加权,外加偏差
- 使用平方损失来衡量预测值和真实值的差异
- 线性回归有显示解
- 线性回归可以看做是单层神经网络
基础优化方法
- 梯度下降
- 选择学习率
- 小批量随机梯度下降
- 选择批量大小
- 梯度下降通过不断沿着反梯度方向更新参数求解
- 小批量随机梯度下降是深度学习默认的求解算法
- 两个重要的超参数是批量大小和学习率
线性回归的从零开始实现
%matplotlib inline
import random
import torch
from d2l import torch as d2l
根据带有噪声的线性模型构造一个人造数据集
def synthetic_data(w, b, num_examples):
"""生成y=Xw+b+噪音"""
X = torch.normal(0, 1, (num_examples, len(w)))
y = torch.matmul(X, w) + b
y += torch.normal(0, 0.01, y.shape)
return X, y.reshape((-1, 1))
true_w = torch.tensor([2, -3.4])
true_b = 4.2
features, labels = synthetic_data(true_w, true_b, 1000)
print('features:', features[0], '\nlabel:', labels[0])
features: tensor([1.2000, 0.0916])
label: tensor([6.2911])
d2l.set_figsize()
d2l.plt.scatter(features[:, (1)].detach().numpy()