深度学习基础pytorch:线性回归,softmax与分类模型,多层感知机
深度学习基础pytorch:线性回归,多层感知机,softmax与分类模型
-什么是线性回归
线性回归是利用数理统计中回归分析,来确定两种或两种以上变量间相互依赖的定量关系的一种统计分析方法,运用十分广泛。其表达形式为y = w’x+e,e为误差服从均值为0的正态分布。
-线性回归的基本要素
- 模型:
首先我们建立一个模型,这个模型房价预测模型,为了简单介绍线性回归,我们将房价预测存在的问题简单化,只用房龄与面积来预测房价,然后我们假设,和房价是线性关系,接着由于线性回归的定义,我们可以得到一个等式:
- 数据集:
对于预测,我们需要数据集来训练我们建立的模型,但是收集数据集有几个条件,对于该问题,我们需要多栋房屋的真实售出价格和它们对应的面积和房龄,然后我们希望利用这个数据集,获得我们几个特殊的值,和,对于机器学习来说,数据集被称为训练数据集(training data set)或训练集(training set),一栋房屋被称为一个样本(sample),其真实售出价格叫作标签(label),用来预测标签的两个因素叫作特征(feature)。特征用来表征样本的特点。
- 损失函数:
损失函数(loss function)或代价函数(cost function)是将随机事件或其有关随机变量的取值映射为非负实数以表示该随机事件的“风险”或“损失”的函数。在应用中,损失函数通常作为学习准则与优化问题相联系,即通过最小化损失函数求解和评估模型。
对于这个模型,我们主要是采用了平方函数作为损失函数,一下为平方函数的式子:
至于为什么用平方函数:
假设数据满足高斯分布的情况下
将y = wx+b 中的y和期望带入高斯分布函数,取对数化简后为 常数-(平方损失函数/方差),是近似平方损失的函数且方差随数据变化是个定值。
固定X使得W为参数时的似然估计最优解得到的概率,等同于真实W下的概率。
所以最大化似然函数值转换为最小化平方损失函数。
所以线性回归实质是寻找一组最贴切的权值,也就是最大化似然函数值。平方损失函数,是最大化函数值的一个简便的式子。
- 小批量随机梯度下降(mini-batch stochastic gradient descent):
MBGD的思想是通过每一次迭代使用的batch_size个样本对参数进行更新
MBGD的伪代码是这样的:
在这里我们假设,样本数
repeat{
for i=1,11,21,31,…,991{
(for j =0,1)
}
}
对于MBGD的优点就显而易见了每次使用一个batch可以大大减小收敛所需要的迭代次数,同时可以使收敛到的结果更加接近梯度下降的效果。
以下是数学表达式:
- 线性回归模型从零开始实现:
%matplotlib inline
import torch
from IPython import display
from matplotlib import pyplot as plt
import numpy as np
import random
print(torch.version)
在这里我们主要是调用我们需要的package和需要的module
print(torch.version)输出的1.3.0是torch的版本号
- 生成数据集
#set input feature number
num_inputs = 2
#set example number
num_examples = 1000
#set true weight and bias in order to generate corresponded label
true_w = [2, -3.4]
true_b = 4.2
features = torch.randn(num_examples, num_inputs,
dtype=torch.float32)
labels = true_w[0] * features[:, 0] + true_w[1] * features[:, 1] + true_b
labels += torch.tensor(np.random.normal(0, 0.01, size=labels.size()),
dtype=torch.float32)
我们用线性模型生成一个num_examples = 1000的数据集,输入成num_inputs为2
然后我们用plt的scatter方法来把这1000个数据集的散点图做出来
- 读取数据集
在训练模型的时候,我们需要遍历数据集并不断读取小批量数据样本。
在这里我们定义一个函数;它每次返回batch_size个随机样本的特征和标签。
def data_iter(batch_size, features, labels):
num_examples = len(features)
indices = list(range(num_examples))
random.shuffle(indices) # random read 10 samples
for i in range(0, num_examples, batch_size):
j = torch.LongTensor(indices[i: min(i + batch_size, num_examples)]) # the last time may be not enough for a whole batch
yield features.index_select(0, j), labels.index_select(0, j)
在这里,我们读取第一个小批量数据样本并且打印。每个批量的特征形状为(10,2),分别对应批量的大小和输入个数,标签形状为批量大小
batch_size = 10
for X, y in data_iter(batch_size, features, labels):
print(X, ‘\n’, y)
break
- 初始化模型参数
我们把权重初始化成均值为0、标准差为0.01的正态随机数,偏差则初始化成0。
w = torch.tensor(np.random.normal(0, 0.01, (num_inputs, 1)), dtype=torch.float32)
b = torch.zeros(1, dtype=torch.float32)
之后的模型训练中,需要对这些参数求梯度来迭代参数的值,因此我们需要创建它们的梯度
w.requires_grad_(requires_grad=True)
b.requires_grad_(requires_grad=True)
- 定义模型
下面是线性回归的矢量计算表达式的实现。我们使用torch.mm做矩阵乘法
def linreg(X, w, b):
return torch.mm(X, w) + b
- 定义损失函数
我们使用平方损失来定义线性回归的损失函数。
def squared_loss(y_hat, y):
return (y_hat - y.view(y_hat.size())) ** 2 / 2
- 定义优化函数
def sgd(params, lr, batch_size):
for param in params:
param.data -= lr * param.grad / batch_size # ues .data to operate param without gradient track
- 开始训练
#super parameters init
lr = 0.03
num_epochs = 5
net = linreg
loss = squared_loss
#training
for epoch in range(num_epochs): # training repeats num_epochs times
# in each epoch, all the samples in dataset will be used once
# X is the feature and y is the label of a batch sample
for X, y in data_iter(batch_size, features, labels):
l = loss(net(X, w, b), y).sum()
# calculate the gradient of batch sample loss
l.backward()
# using small batch random gradient descent to iter model parameters
sgd([w, b], lr, batch_size)
# reset parameter gradient
w.grad.data.zero_()
b.grad.data.zero_()
train_l = loss(net(features, w, b), labels)
print(‘epoch %d, loss %f’ % (epoch + 1, train_l.mean().item()))
训练结束后,我们比较以下,学到的参数和用来生成的训练集的真实参数。
w, true_w, b, true_b
- softmax与分类模型
- 分类问题
分类需要得到离散的预测输出,一个简单的办法是将输出值当作预测类别是的置信度,并将值最大的输出所对应的类作为预测输出,即输出
- softmax的基本概念
softmax回归是一个单层神经网络。softmax回归的输出层是一个全连接层
- softmax运算
softmax预算可以解决了两个问题:
- 输出层的输出值的范围不稳定,我们难以直观上判断这些值的意义。
- 真实标签是离散值,这些离散值与不确定范围的输出值之间的误差难以衡量
通过下式将输出值变换成值为正且和为1的概率分布:
其中:
由于
所以softmax运算不改变预测类别输出
代码中softmax操作:
def softmax(X):
X_exp = X.exp()
partition = X_exp.sum(dim=1, keepdim=True)
# print("X size is ", X_exp.size())
# print("partition size is ", partition, partition.size())
return X_exp / partition # 这里应用了广播机制
- softmax回归模型
def net(X):
return softmax(torch.mm(X.view((-1, num_inputs)), W) + b)
- 交叉熵损失函数
- 交叉熵
交叉熵是一个适合衡量两个概率分布差异的测量函数的方法
- 交叉熵损失函数
假设训练数据集的样本数为n,交叉熵损失函数定义为
- 定义损失函数
y_hat = torch.tensor([[0.1, 0.3, 0.6], [0.3, 0.2, 0.5]])
y = torch.LongTensor([0, 2])
y_hat.gather(1, y.view(-1, 1))
def cross_entropy(y_hat, y):
return - torch.log(y_hat.gather(1, y.view(-1, 1)))
- 多层感知机
- 多层感知机的基本知识
- 隐藏层
单隐藏层的多层感知机
也就是将隐藏层的输出直接作为输出层的输入。如果将以上两个式子联立起来,可以得到
以上设计只能与仅含输出层的单层神经网络等价
定义网络
def net(X):
X = X.view((-1, num_inputs))
H = relu(torch.matmul(X, W1) + b1)
return torch.matmul(H, W2) + b2
- Relu函数
定义:
x = torch.arange(-8.0, 8.0, 0.1, requires_grad=True)
y = x.relu()
xyplot(x, y, ‘relu’)
y.sum().backward()
xyplot(x, x.grad, ‘grad of relu’)
-Sigmoid函数
定义:
y = x.sigmoid()
xyplot(x, y, ‘sigmoid’)
x.grad.zero_()
y.sum().backward()
xyplot(x, x.grad, ‘grad of sigmoid’)
- tanh函数
定义:
y = x.tanh()
xyplot(x, y, ‘tanh’)
x.grad.zero_()
y.sum().backward()
xyplot(x, x.grad, ‘grad of tanh’)
在神经网络层数较多的时候,最好使用ReLu函数,ReLu函数比较简单计算量少,而sigmoid和tanh函数计算量大很多。
-多层感知机
定义模型参数
num_inputs, num_outputs, num_hiddens = 784, 10, 256
W1 = torch.tensor(np.random.normal(0, 0.01, (num_inputs, num_hiddens)), dtype=torch.float)
b1 = torch.zeros(num_hiddens, dtype=torch.float)
W2 = torch.tensor(np.random.normal(0, 0.01, (num_hiddens, num_outputs)), dtype=torch.float)
b2 = torch.zeros(num_outputs, dtype=torch.float)
params = [W1, b1, W2, b2]
for param in params:
param.requires_grad_(requires_grad=True)
我们用CrossEntropyLoss方法定义损失函数
loss = torch.nn.CrossEntropyLoss()
在学习这三个内容的时候,由于我先修了mxnet版本的代码,理解pytorch版本的代码并不难,神经网络的学习得先理解概念,然后多写一些代码,然后才能熟练。