Skip to content

Commit 49bdd53

Browse files
committed
第5章笔记,第8章部分代码
1 parent 8f12a39 commit 49bdd53

File tree

5 files changed

+180
-0
lines changed

5 files changed

+180
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
2+
3+
## 5.1 机器学习的基本任务
4+
5+
- 监督学习
6+
- 分类
7+
- 回归
8+
- 目标检测
9+
- 识别
10+
- 无监督学习
11+
- 聚类
12+
- 降维
13+
- 半监督学习
14+
- 自编码
15+
- 推荐
16+
- 生成式对抗
17+
- 强化学习
18+
- 分类
19+
- 回归
20+
21+
## 5.2 机器学习的一般流程
22+
23+
0. 定义问题
24+
1. 明确目标
25+
2. 收集数据
26+
3. 数据探索与预处理
27+
4. 选择模型及损失函数
28+
5. 评估及优化模型
29+
30+
## 5.3 过拟合与欠拟合
31+
32+
1. 权重正则化
33+
2. Dropout正则化
34+
3. Batch Normalization
35+
4. 权重初始化
36+
37+
## 5.4 选择合适的**激活函数**
38+
39+
- sigmoid
40+
表达式:$f(x)=\frac {1} {1+e^{-x}}$
41+
导数:
42+
- tanh
43+
$f(x)=\frac {1-e^{-2x}} {1+e^{-2x}}$
44+
>【注意】书中89页下边tanh公式写少写了个负号。
45+
- relu
46+
- LeakyReLU
47+
- softmax
48+
49+
## 5.5 选择合适的**损失函数**
50+
训练模型的过程实际就是优化损失函数的过程。
51+
52+
1. 分类问题——交叉熵(Cross Entropy)
53+
交叉熵反应的是两个概率分布之间的距离。
54+
交叉熵损失又称对数似然损失。
55+
56+
57+
2. 回归问题——均方误差(Mean Sequared error, Mse)
58+
59+
## 5.6 选择合适优化器
60+
61+
### 5.6.1 传统梯度优化的不足
62+
* 对超参数学习率比较敏感。
63+
* 容易卡在鞍点上。
64+
65+
### 5.6.2 动量算法
66+
动量(Momentum)算法:每下降一步由前面下降方向的一个累积和当前点的梯度方向 组合而成。
67+
NAG算法:按照前面一小步位置 的超前梯度来做梯度合并。
68+
69+
### 5.6.3 AdaGrad 算法
70+
能自动调整模型参数的学习率。
71+
72+
### 5.6.4 RMSProp 算法
73+
超参数\rho$ 用来控制移动平均的长度范围。
74+
75+
### 5.6.5 Adam 算法
76+
带有动量项的 RMSProp 算法
77+
78+
79+
## 5.7 GPU加速
80+
### 5.7.1 单GPU加速
81+
* torch.cuda.is_available() GPU是否可用。
82+
* torch.cuda.device_count() 获得能够使用的GPU的数量
83+
* to(device) .cuda() 从内存转到GPU
84+
85+
86+
### 5.7.2 多GPU加速
87+
- 单机多GPU DataParallel
88+
- 多机多GPU DistributedParallel
89+
90+
### 5.7.3 使用GPU注意事项
91+
1. GPU数量尽量为偶数
92+
2. 数据量小时多GPU未必更快。
93+
3. 内存不够大,可设置pin_memory为false。
94+
95+
96+
97+
# 参考
98+
《Python深度学习基于Pytorch》
99+

pytorch-08/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
data/

pytorch-08/load_data.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
import os
3+
import torch
4+
import torch.nn as nn
5+
import torch.nn.functional as F
6+
7+
import torchvision
8+
from torchvision import transforms
9+
from torchvision.utils import save_image
10+
11+
12+
image_size = 784
13+
h_dim = 400
14+
z_dim = 20
15+
num_epochs = 30
16+
batch_size = 128
17+
learning_rate = 0.001
18+
19+
dataset = torchvision.datasets.MNIST(root='data', train=True, transform=transforms.ToTensor(), download=True)
20+
data_loader = torch.utils.data.DataLoader(dataset=dataset, batch_size=batch_size, shuffle=True)
21+

pytorch-08/net_vae.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
3+
import torch.nn as nn
4+
import torch.nn.functional as F
5+
6+
7+
class VAE(nn.Module):
8+
def __init__(self, image_size=784, h_dim=400, z_dim=20):
9+
super(VAE, self).__init__()
10+
self.fc1 = nn.Linear(image_size, h_dim)
11+
self.fc2 = nn.Linear(h_dim, z_dim)
12+
self.fc3 = nn.Linear(h_dim, z_dim)
13+
self.fc4 = nn.Linear(z_dim, h_dim)
14+
self.fc5 = nn.Linear(h_dim, image_size)
15+
16+
def encode(self, x):
17+
h = F.relu(self.fc1(x))
18+
return self.fc2(h), self.fc3(h)
19+
20+
def reparameterize(self, mu, log_var):
21+
std = torch.exp(log_var/2)
22+
eps = torch.randn_like(std)
23+
return mu + eps * std
24+
25+
def decode(self, z):
26+
h = F.relu(self.fc4(z))
27+
return F.sigmoid(self.fc5(h))
28+
29+
def forward(self, x):
30+
mu, log_var = self.encode(x)
31+
z = self.reparameterize(mu, log_var)
32+
x_reconst = self.decode(z)
33+
return x_reconst, mu, log_var
34+
35+
36+
if __name__ == "__main__":
37+
net = VAE()
38+
print(net)
39+
40+
41+

pytorch-08/train_vae.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
import torch
3+
4+
from load_data import *
5+
from net_vae import VAE
6+
7+
8+
# torch.cuda.set_device(0)
9+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
10+
model = VAE().to(device)
11+
12+
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
13+
14+
15+
16+
17+
18+

0 commit comments

Comments
 (0)