线性回归
线性关系
以x1 和x2 估计y值,故训练 ω \omega ω和b三个参数
损失函数 loss function
均值表示模型质量
优化算法
求出适合的参数,尽可能降低损失函数
小批量随机梯度下降
神经网络图
此为单层神经网络
此输出层中的神经元与输入层中各个输入完全连接,所以此输出层叫全连接层或稠密层
矢量计算表达式
两个向量的加法中,矢量相加比各个元素单独相加快很多,故
转为矢量计算
其中
损失函数写为
小批量随机梯度下降的迭代步骤写为
梯度写为
线性回归程序编写
from IPython import display
from matplotlib import pyplot as plt
from mxnet import autograd, nd
import random
num_inputs = 2
num_examples = 1000
true_w = [2, -3.4]
true_b = 4.2
features = nd.random.normal(scale=1, shape=(num_examples, num_inputs)) # 1000行长度为2的向量,x1和x2
labels = true_w[0] * features[:, 0] + true_w[1] * features[:, 1] + true_b # 1000行长度为1的向量,y
labels += nd.random.normal(scale=0.01, shape=labels.shape) # 给y值添加噪声
def use_svg_display():
# 用矢量图显示
display.set_matplotlib_formats('svg')
def set_figsize(figsize=(3.5, 2.5)):
use_svg_display()
# 设置图的尺寸
plt.rcParams['figure.figsize'] = figsize
set_figsize()
plt.scatter(features[:, 1].asnumpy(), labels.asnumpy(), 1); # 加分号只显示图
def data_iter(batch_size, features, labels):
num_examples = len(features)
indices = list(range(num_examples))
random.shuffle(indices) # 样本的读取顺序是随机的 把序列indices打乱
for i in range(0, num_examples, batch_size):
j = nd.array(indices[i: min(i + batch_size, num_examples)])
yield features.take(j), labels.take(j) # take函数根据索引返回对应元素 每次返回10个,共返回100次,返回全部值
w = nd.random.normal(scale=0.01, shape=(num_inputs, 1))
b = nd.zeros(shape=(1,))
w.attach_grad() # 申请存储梯度所需要的内存
b.attach_grad()
def linreg(X, w, b): # 本函数已保存在d2lzh包中方便以后使用
return nd.dot(X, w) + b
def squared_loss(y_hat, y): # 本函数已保存在d2lzh包中方便以后使用
return (y_hat - y.reshape(y_hat.shape)) ** 2 / 2
def sgd(params, lr, batch_size): # 本函数已保存在d2lzh包中方便以后使用
for param in params:
param[:] = param - lr * param.grad / batch_size
lr = 0.03
num_epochs = 1
net = linreg
loss = squared_loss
batch_size=10
for epoch in range(num_epochs): # 训练模型一共需要num_epochs个迭代周期
# 在每一个迭代周期中,会使用训练数据集中所有样本一次(假设样本数能够被批量大小整除)。X
# 和y分别是小批量样本的特征和标签
for X, y in data_iter(batch_size, features, labels):
with autograd.record():
l = loss(net(X, w, b), y) # l是有关小批量X和y的损失
l.backward() # 小批量的损失对模型参数求梯度
sgd([w, b], lr, batch_size) # 使用小批量随机梯度下降迭代模型参数
train_l = loss(net(features, w, b), labels)
print('epoch %d, loss %f' % (epoch + 1, train_l.mean().asnumpy()))