pytorch学习笔记(二)
Logistic Regression分类问题
1、torchvision提供了常用的数据集
import torchvision
train_set = torchvision.datasets.MNIST(root='../dataset/mnist',train=True,download=True)
// train是否是训练集,download是否需要下载
test_set = torchvision.datasets.MNIST(root='../dataset/mnist',train=False,download=True)
- CIFAR-10 dataset:32*32的图片,分为十类
2、some functions
-
Logistic Function : 属于饱和函数。R→[0,1]
σ ( x ) = 1 1 + e − x \sigma(x) = \frac{1}{1+e^{-x}}\, σ(x)=1+e−x1 -
Sigmoid functions:满足饱和函数、有极限、单调增函数,常说的Sigmoid function通常指Logistic Function。
-
cross-entropy:交叉熵,表示两个分布之间差异性的大小
-
二分类用的损失函数(BCE):
l o s s = − ( y l o g y ^ + ( 1 − y ) l o g ( 1 − y ^ ) ) loss = -(y \, log\widehat{y} +(1-y)\,log(1-\widehat{y})) loss=−(ylogy +(1−y)log(1−y ))
(加负号使得loss越小越好)
处理多维特征输入
如果输入八维数据,要求输出一维数据,则需要做以下变换:
[
z
(
1
)
⋮
z
(
N
)
]
=
[
x
1
(
1
)
…
x
8
(
1
)
⋮
x
1
(
N
)
…
x
8
(
N
)
]
[
w
1
⋮
w
8
]
+
[
b
1
⋮
b
8
]
\begin{bmatrix} z^{(1)} \\ \vdots \\ z^{(N)}\end{bmatrix} = \begin{bmatrix} x_1^{(1)}&\dots&x_8^{(1)} \\ \vdots \\ x_1^{(N)}&\dots&x_8^{(N)}\end{bmatrix} \begin{bmatrix} w_1 \\ \vdots \\ w_8\end{bmatrix}+\begin{bmatrix} b_1 \\ \vdots \\ b_8\end{bmatrix}
⎣⎢⎡z(1)⋮z(N)⎦⎥⎤=⎣⎢⎢⎡x1(1)⋮x1(N)……x8(1)x8(N)⎦⎥⎥⎤⎣⎢⎡w1⋮w8⎦⎥⎤+⎣⎢⎡b1⋮b8⎦⎥⎤
self.linear = torch.nn.Linear(8,1)
加载数据集
-
Dataset:构造数据集。抽象类,不能实例化
DataLoader:拿出Mini-Batch供快速使用。可以实例化 -
利用一个样本——解决鞍点问题,耗费时间长
利用全部Batch——计算速度快,性能不是很好 -
Epoch:利用所有样本的次数
Batch-Siza:一次前馈/反馈运算所需的样本个数
Iteration:用batch size迭代的次数 -
shuffle是否打乱顺序
多分类问题
- Softmax Classifier:保证p>=0且总概率之和为1。先做指数运算,再求sum、divide,得到一个离散的分布。
- Loss Function:Loss(y_pred,y)= -ylogy_pred
CrossEntropyLoss <==> LogSoftmax + NLLLoss
输入的图像先转换成tensor,再调用Normalize使其分布在0~1上,以供神经网络训练。