提交 60bea082 编写于 作者: W wizardforcel

2020-10-19 22:31:47

上级 e92cc304
......@@ -2,7 +2,7 @@
## 为什么用 PyTorch
[PyTorch](https://www.pytorchtutorial.com/goto/http://pytorch.org/) 是 [Torch](https://www.pytorchtutorial.com/goto/http://torch.ch/) 在 Python 上的衍生. 因为 [Torch](https://www.pytorchtutorial.com/goto/http://torch.ch/) 是一个使用 [Lua](https://www.pytorchtutorial.com/goto/https://www.lua.org/) 语言的神经网络库, Torch 很好用, 但是 Lua 又不是特别流行, 所有开发团队将 Lua 的 Torch 移植到了更流行的语言 Python 上. 是的 PyTorch 一出生就引来了剧烈的反响. 为什么呢?
[PyTorch](http://pytorch.org/) 是 [Torch](http://torch.ch/) 在 Python 上的衍生. 因为 [Torch](http://torch.ch/) 是一个使用 [Lua](https://www.lua.org/) 语言的神经网络库, Torch 很好用, 但是 Lua 又不是特别流行, 所有开发团队将 Lua 的 Torch 移植到了更流行的语言 Python 上. 是的 PyTorch 一出生就引来了剧烈的反响. 为什么呢?
很简单, 我们就看看有谁在用 PyTorch 吧.
......@@ -10,7 +10,7 @@
可见, 著名的 Facebook, twitter 等都在使用它, 这就说明 PyTorch 的确是好用的, 而且是值得推广.
而且如果你知道 [Numpy](https://www.pytorchtutorial.com/goto/http://www.numpy.org/), PyTorch 说他就是在神经网络领域可以用来替换 numpy 的模块.
而且如果你知道 [Numpy](http://www.numpy.org/), PyTorch 说他就是在神经网络领域可以用来替换 numpy 的模块.
## 神经网络在做什么
......@@ -33,4 +33,4 @@
* 如果你是学生, 随便选一个学, 或者稍稍偏向 PyTorch, 因为写代码的时候应该更好理解. 懂了一个模块, 转换 Tensorflow 或者其他的模块都好说.
* 如果是上班了, 跟着你公司来, 公司用什么, 你就用什么, 不要脱群.
文章来源:[莫烦](https://www.pytorchtutorial.com/goto/https://morvanzhou.github.io/)
\ No newline at end of file
文章来源:[莫烦](https://morvanzhou.github.io/)
\ No newline at end of file
......@@ -6,7 +6,7 @@ PyTorch 暂时只支持 MacOS, Linux. 暂不支持 Windows! (可怜的 Windows
## 安装
PyTorch 安装起来很简单, [它自家网页](https://www.pytorchtutorial.com/goto/http://pytorch.org/)上就有很方便的选择方式 (网页升级改版后可能和下图有点不同):
PyTorch 安装起来很简单, [它自家网页](http://pytorch.org/)上就有很方便的选择方式 (网页升级改版后可能和下图有点不同):
![](img/b708f231f544bd7bcefa1d55c82653dd.png)
......@@ -19,6 +19,6 @@ $ pip install http://download.pytorch.org/whl/torch-0.1.11.post5-cp35-cp35m-maco
$ pip install torchvision
```
注意, 我安装的是0.1.11版本的 torch, 你需要去他们网站上看是否有新版本的. 安装 PyTorch 会安装两个模块, 一个是 torch, 一个 torchvision, torch 是主模块, 用来搭建神经网络的, torchvision 是辅模块, 有数据库, 还有一些已经训练好的神经网络等着你直接用, 比如 ([VGG, AlexNet, ResNet](https://www.pytorchtutorial.com/goto/http://pytorch.org/docs/torchvision/models.html)).
注意, 我安装的是0.1.11版本的 torch, 你需要去他们网站上看是否有新版本的. 安装 PyTorch 会安装两个模块, 一个是 torch, 一个 torchvision, torch 是主模块, 用来搭建神经网络的, torchvision 是辅模块, 有数据库, 还有一些已经训练好的神经网络等着你直接用, 比如 ([VGG, AlexNet, ResNet](http://pytorch.org/docs/torchvision/models.html)).
文章来源:[莫烦](https://www.pytorchtutorial.com/goto/https://morvanzhou.github.io/)
\ No newline at end of file
文章来源:[莫烦](https://morvanzhou.github.io/)
\ No newline at end of file
......@@ -22,7 +22,7 @@ print(
## Torch 中的数学运算
其实 torch 中 tensor 的运算和 numpy array 的如出一辙, 我们就以对比的形式来看. 如果想了解 torch 中其它更多有用的运算符, [API就是你要去的地方](https://www.pytorchtutorial.com/goto/http://pytorch.org/docs/torch.html#math-operations).
其实 torch 中 tensor 的运算和 numpy array 的如出一辙, 我们就以对比的形式来看. 如果想了解 torch 中其它更多有用的运算符, [API就是你要去的地方](http://pytorch.org/docs/torch.html#math-operations).
```py
# abs 绝对值计算
......@@ -71,4 +71,4 @@ print(
)
```
文章来源:[莫烦](https://www.pytorchtutorial.com/goto/https://morvanzhou.github.io/)
\ No newline at end of file
文章来源:[莫烦](https://morvanzhou.github.io/)
\ No newline at end of file
......@@ -87,4 +87,4 @@ print(variable.data.numpy()) # numpy 形式
"""
```
文章来源:[莫烦](https://www.pytorchtutorial.com/goto/https://morvanzhou.github.io/)
\ No newline at end of file
文章来源:[莫烦](https://morvanzhou.github.io/)
\ No newline at end of file
......@@ -62,4 +62,4 @@ plt.legend(loc=\'best\')
plt.show()
```
文章来源:[莫烦](https://www.pytorchtutorial.com/goto/https://morvanzhou.github.io/)
\ No newline at end of file
文章来源:[莫烦](https://morvanzhou.github.io/)
\ No newline at end of file
......@@ -103,6 +103,6 @@ for t in range(100):
![](img/13e0473ef73a9de2569a81c62e30d054.png)
所以这也就是在我 [github 代码](https://www.pytorchtutorial.com/goto/https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/301_regression.py) 中的每一步的意义啦.
所以这也就是在我 [github 代码](https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/301_regression.py) 中的每一步的意义啦.
文章来源:[莫烦](https://www.pytorchtutorial.com/goto/https://morvanzhou.github.io/)
\ No newline at end of file
文章来源:[莫烦](https://morvanzhou.github.io/)
\ No newline at end of file
......@@ -121,6 +121,6 @@ plt.show()
![](img/bce7313d5ac6f2600b62a4962a6daf3a.png)
所以这也就是在我 [github 代码](https://www.pytorchtutorial.com/goto/https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/302_classification.py) 中的每一步的意义啦.
所以这也就是在我 [github 代码](https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/302_classification.py) 中的每一步的意义啦.
文章来源:[莫烦](https://www.pytorchtutorial.com/goto/https://morvanzhou.github.io/)
\ No newline at end of file
文章来源:[莫烦](https://morvanzhou.github.io/)
\ No newline at end of file
......@@ -53,6 +53,6 @@ Sequential (
我们会发现 net2  多显示了一些内容, 这是为什么呢? 原来他把激励函数也一同纳入进去了, 但是 net1  中, 激励函数实际上是在 forward()  功能中才被调用的. 这也就说明了, 相比 net2 , net1  的好处就是, 你可以根据你的个人需要更加个性化你自己的前向传播过程, 比如(RNN). 不过如果你不需要七七八八的过程, 相信 net2  这种形式更适合你.
所以这也就是在我 [github 代码](https://www.pytorchtutorial.com/goto/https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/303_build_nn_quickly.py) 中的每一步的意义啦.
所以这也就是在我 [github 代码](https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/303_build_nn_quickly.py) 中的每一步的意义啦.
文章来源:[莫烦](https://www.pytorchtutorial.com/goto/https://morvanzhou.github.io/)
\ No newline at end of file
文章来源:[莫烦](https://morvanzhou.github.io/)
\ No newline at end of file
......@@ -77,6 +77,6 @@ def restore_params():
这样我们就能看出三个网络完全一模一样啦.
所以这也就是在我 [github 代码](https://www.pytorchtutorial.com/goto/https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/304_save_reload.py) 中的每一步的意义啦.
所以这也就是在我 [github 代码](https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/304_save_reload.py) 中的每一步的意义啦.
文章来源:[莫烦](https://www.pytorchtutorial.com/goto/https://morvanzhou.github.io/)
\ No newline at end of file
文章来源:[莫烦](https://morvanzhou.github.io/)
\ No newline at end of file
......@@ -67,6 +67,6 @@ Epoch: 2 | Step: 1 | batch x: [ 1\. 5.] | batch y: [ 10\. 6.]
这时, 在 step=1  就只给你返回这个 epoch 中剩下的数据就好了.
所以这也就是在我 [github 代码](https://www.pytorchtutorial.com/goto/https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/305_batch_train.py) 中的每一步的意义啦.
所以这也就是在我 [github 代码](https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/305_batch_train.py) 中的每一步的意义啦.
文章来源:[莫烦](https://www.pytorchtutorial.com/goto/https://morvanzhou.github.io/)
\ No newline at end of file
文章来源:[莫烦](https://morvanzhou.github.io/)
\ No newline at end of file
......@@ -104,6 +104,6 @@ for epoch in range(EPOCH):
SGD  是最普通的优化器, 也可以说没有加速效果, 而 Momentum  是 SGD  的改良版, 它加入了动量原则. 后面的 RMSprop  又是 Momentum  的升级版. 而 Adam  又是 RMSprop  的升级版. 不过从这个结果中我们看到, Adam  的效果似乎比 RMSprop  要差一点. 所以说并不是越先进的优化器, 结果越佳. 我们在自己的试验中可以尝试不同的优化器, 找到那个最适合你数据/网络的优化器.
所以这也就是在我 [github 代码](https://www.pytorchtutorial.com/goto/https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/306_optimizer.py) 中的每一步的意义啦.
所以这也就是在我 [github 代码](https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/306_optimizer.py) 中的每一步的意义啦.
文章来源:[莫烦](https://www.pytorchtutorial.com/goto/https://morvanzhou.github.io/)
\ No newline at end of file
文章来源:[莫烦](https://morvanzhou.github.io/)
\ No newline at end of file
......@@ -154,6 +154,6 @@ print(test_y[:10].numpy(), \\'real number\\')
![](img/388ca39bf710c8f053f533ad10872cd7.png)
所以这也就是在我 [github 代码](https://www.pytorchtutorial.com/goto/https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/401_CNN.py) 中的每一步的意义啦.
所以这也就是在我 [github 代码](https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/401_CNN.py) 中的每一步的意义啦.
文章来源:[莫烦](https://www.pytorchtutorial.com/goto/https://morvanzhou.github.io/)
\ No newline at end of file
文章来源:[莫烦](https://morvanzhou.github.io/)
\ No newline at end of file
......@@ -140,6 +140,6 @@ print(test_y[:10], \\'real number\\')
"""
```
所以这也就是在我 [github 代码](https://www.pytorchtutorial.com/goto/https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/402_RNN_classifier.py) 中的每一步的意义啦.
所以这也就是在我 [github 代码](https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/402_RNN_classifier.py) 中的每一步的意义啦.
文章来源:[莫烦](https://www.pytorchtutorial.com/goto/https://morvanzhou.github.io/)
\ No newline at end of file
文章来源:[莫烦](https://morvanzhou.github.io/)
\ No newline at end of file
......@@ -112,6 +112,6 @@ for step in range(60):
![](img/04a6040ad4f06a69a92f440ea17dde44.png)
所以这也就是在我 [github 代码](https://www.pytorchtutorial.com/goto/https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/403_RNN_regressor.py) 中的每一步的意义啦.
所以这也就是在我 [github 代码](https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/403_RNN_regressor.py) 中的每一步的意义啦.
文章来源:[莫烦](https://www.pytorchtutorial.com/goto/https://morvanzhou.github.io/)
\ No newline at end of file
文章来源:[莫烦](https://morvanzhou.github.io/)
\ No newline at end of file
......@@ -133,6 +133,6 @@ ax.set_zlim(Z.min(), Z.max())
plt.show()
```
所以这也就是在我 [github 代码](https://www.pytorchtutorial.com/goto/https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/404_autoencoder.py) 中的每一步的意义啦.
所以这也就是在我 [github 代码](https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/404_autoencoder.py) 中的每一步的意义啦.
文章来源:[莫烦](https://www.pytorchtutorial.com/goto/https://morvanzhou.github.io/)
\ No newline at end of file
文章来源:[莫烦](https://morvanzhou.github.io/)
\ No newline at end of file
# 4.5 – DQN 强化学习 (Reinforcement Learning)
Torch 是神经网络库, 那么也可以拿来做强化学习, 之前我用另一个强大神经网络库 Tensorflow来制作了这一个 从浅入深强化学习教程, 你同样也可以用 PyTorch 来实现, 这次我们就举 DQN 的例子, 我对比了我的 Tensorflow DQN 的代码, 发现 PyTorch 写的要简单很多. 如果对 DQN 或者强化学习还没有太多概念, 强烈推荐我的这个DQN动画短片(如下), 让你秒懂DQN. 还有强推这套花了我几个月来制作的[强化学习教程](https://www.pytorchtutorial.com/goto/https://morvanzhou.github.io/tutorials/machine-learning/reinforcement-learning/)!
Torch 是神经网络库, 那么也可以拿来做强化学习, 之前我用另一个强大神经网络库 Tensorflow来制作了这一个 从浅入深强化学习教程, 你同样也可以用 PyTorch 来实现, 这次我们就举 DQN 的例子, 我对比了我的 Tensorflow DQN 的代码, 发现 PyTorch 写的要简单很多. 如果对 DQN 或者强化学习还没有太多概念, 强烈推荐我的这个DQN动画短片(如下), 让你秒懂DQN. 还有强推这套花了我几个月来制作的[强化学习教程](https://morvanzhou.github.io/tutorials/machine-learning/reinforcement-learning/)!
<video class="wp-video-shortcode" id="video-135-1" width="760" height="427" preload="metadata" controls="controls"><source type="video/mp4" src="https://www.pytorchtutorial.com/wp-content/uploads/2017/08/cartpole-dqn.mp4?_=1">[https://www.pytorchtutorial.com/wp-content/uploads/2017/08/cartpole-dqn.mp4](https://www.pytorchtutorial.com/wp-content/uploads/2017/08/cartpole-dqn.mp4)</video>
......@@ -160,6 +160,6 @@ for i_episode in range(400):
s = s_
```
所以这也就是在我 [github 代码](https://www.pytorchtutorial.com/goto/https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/405_DQN_Reinforcement_learning.py) 中的每一步的意义啦.
所以这也就是在我 [github 代码](https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/405_DQN_Reinforcement_learning.py) 中的每一步的意义啦.
文章来源:[莫烦](https://www.pytorchtutorial.com/goto/https://morvanzhou.github.io/)
\ No newline at end of file
文章来源:[莫烦](https://morvanzhou.github.io/)
\ No newline at end of file
......@@ -82,7 +82,7 @@ for step in range(10000):
prob_artist1 = D(G_paintings) # D try to reduce this prob
```
然后计算有多少来之画家的画猜对了, 有多少来自 G 的画猜对了, 我们想最大化这些猜对的次数. 这也就是 log(D(x)) log(1-D(G(z))  在[论文](https://www.pytorchtutorial.com/goto/https://arxiv.org/abs/1406.2661)中的形式. 而因为 torch 中提升参数的形式是最小化误差, 那我们把最大化 score 转换成最小化 loss, 在两个 score 的合的地方加一个符号就好. 而 G 的提升就是要减小 D 猜测 G 生成数据的正确率, 也就是减小 D_score1.
然后计算有多少来之画家的画猜对了, 有多少来自 G 的画猜对了, 我们想最大化这些猜对的次数. 这也就是 log(D(x)) log(1-D(G(z))  在[论文](https://arxiv.org/abs/1406.2661)中的形式. 而因为 torch 中提升参数的形式是最小化误差, 那我们把最大化 score 转换成最小化 loss, 在两个 score 的合的地方加一个符号就好. 而 G 的提升就是要减小 D 猜测 G 生成数据的正确率, 也就是减小 D_score1.
```py
D_loss = - torch.mean(torch.log(prob_artist0) torch.log(1\. - prob_artist1))
......@@ -101,12 +101,12 @@ for step in range(10000):
opt_G.step()
```
上面的全部代码内容在我的 [github](https://www.pytorchtutorial.com/goto/https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/406_GAN.py).
上面的全部代码内容在我的 [github](https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/406_GAN.py).
## 可视化训练过程
可视化的代码很简单, 在这里就不会意义叙说了, 大家直接看[代码](https://www.pytorchtutorial.com/goto/https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/406_GAN.py) 吧. 在本节的最上面就是这次的动图效果, 最后达到收敛时, 下过如下, G 能成功的根据自己的”灵感”, 产生出一条很像 artist画出的曲线, 而 D 再也没有能力猜出这到底是 G 的画作还是 artist 的画作, 他只能一半时间猜是 G 的, 一半时间猜是 artist的.
可视化的代码很简单, 在这里就不会意义叙说了, 大家直接看[代码](https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/406_GAN.py) 吧. 在本节的最上面就是这次的动图效果, 最后达到收敛时, 下过如下, G 能成功的根据自己的”灵感”, 产生出一条很像 artist画出的曲线, 而 D 再也没有能力猜出这到底是 G 的画作还是 artist 的画作, 他只能一半时间猜是 G 的, 一半时间猜是 artist的.
![](img/7eca2f8318f254b17ca0bc215ec4f5a0.png)
文章来源:[莫烦](https://www.pytorchtutorial.com/goto/https://morvanzhou.github.io/)
\ No newline at end of file
文章来源:[莫烦](https://morvanzhou.github.io/)
\ No newline at end of file
......@@ -10,7 +10,7 @@
## 动态RNN
我们拿 [这一节内容的 RNN](https://www.pytorchtutorial.com/4-3-rnn-for-regression/) 来解释动态计算图. 那节内容的[代码在这](https://www.pytorchtutorial.com/goto/https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/11_RNN_regressor.py).
我们拿 [这一节内容的 RNN](https://www.pytorchtutorial.com/4-3-rnn-for-regression/) 来解释动态计算图. 那节内容的[代码在这](https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/11_RNN_regressor.py).
```py
..
......@@ -56,6 +56,6 @@ for i in range(60):
经过这样的折腾, torch 还能 handle 住, 已经很不容易啦. 所以当你想要处理这些动态计算图的时候, Torch 还是你首选的神经网络模块.
所以这也就是在我 [github 代码](https://www.pytorchtutorial.com/goto/https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/501_why_torch_dynamic_graph.py) 中的每一步的意义啦.
所以这也就是在我 [github 代码](https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/501_why_torch_dynamic_graph.py) 中的每一步的意义啦.
文章来源:[莫烦](https://www.pytorchtutorial.com/goto/https://morvanzhou.github.io/)
\ No newline at end of file
文章来源:[莫烦](https://morvanzhou.github.io/)
\ No newline at end of file
......@@ -2,12 +2,12 @@
在 GPU 训练可以大幅提升运算速度. 而且 Torch 也有一套很好的 GPU 运算体系. 但是要强调的是:
* 你的电脑里有合适的 GPU 显卡(NVIDIA), 且支持 CUDA 模块. [请在NVIDIA官网查询](https://www.pytorchtutorial.com/goto/https://developer.nvidia.com/cuda-gpus)
* 你的电脑里有合适的 GPU 显卡(NVIDIA), 且支持 CUDA 模块. [请在NVIDIA官网查询](https://developer.nvidia.com/cuda-gpus)
* 必须安装 GPU 版的 Torch, [点击这里查看如何安装](https://www.pytorchtutorial.com/1-2-install-pytorch/)
## 用 GPU 训练 CNN
这份 GPU 的代码是依据[之前这份CNN](https://www.pytorchtutorial.com/goto/https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/401_CNN.py)的代码修改的. 大概修改的地方包括将数据的形式变成 GPU 能读的形式, 然后将 CNN 也变成 GPU 能读的形式. 做法就是在后面加上 .cuda() , 很简单.
这份 GPU 的代码是依据[之前这份CNN](https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/401_CNN.py)的代码修改的. 大概修改的地方包括将数据的形式变成 GPU 能读的形式, 然后将 CNN 也变成 GPU 能读的形式. 做法就是在后面加上 .cuda() , 很简单.
```py
...
......@@ -61,6 +61,6 @@ print(test_y[:10], \'real number\')
大功告成~
所以这也就是在我 [github 代码](https://www.pytorchtutorial.com/goto/https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/502_GPU.py) 中的每一步的意义啦.
所以这也就是在我 [github 代码](https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/502_GPU.py) 中的每一步的意义啦.
文章来源:[莫烦](https://www.pytorchtutorial.com/goto/https://morvanzhou.github.io/)
\ No newline at end of file
文章来源:[莫烦](https://morvanzhou.github.io/)
\ No newline at end of file
......@@ -116,6 +116,6 @@ for t in range(500):
![](img/c2914d88b6f17b84982e162cf6930a88.png)
所以这也就是在我 [github 代码](https://www.pytorchtutorial.com/goto/https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/503_dropout.py) 中的每一步的意义啦.
所以这也就是在我 [github 代码](https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/503_dropout.py) 中的每一步的意义啦.
文章来源:[莫烦](https://www.pytorchtutorial.com/goto/https://morvanzhou.github.io/)
\ No newline at end of file
文章来源:[莫烦](https://morvanzhou.github.io/)
\ No newline at end of file
......@@ -128,7 +128,7 @@ for epoch in range(EPOCH):
## 画图
这个教程有几张图要画, 首先我们画训练时的动态图. 我单独定义了一个画动图的功能 plot_histogram() , 因为不是重点, 所以代码的具体细节请看我的 [github](https://www.pytorchtutorial.com/goto/https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/504_batch_normalization.py),
这个教程有几张图要画, 首先我们画训练时的动态图. 我单独定义了一个画动图的功能 plot_histogram() , 因为不是重点, 所以代码的具体细节请看我的 [github](https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/504_batch_normalization.py),
![](img/cb2138c3f800c7ca4b5ae38076d09429.png)
......@@ -156,7 +156,7 @@ for epoch in range(EPOCH):
...
```
后面还有两张图, 一张是预测曲线, 一张是误差变化曲线, 具体代码不在这里呈现, 想知道如何画图的朋友, 请参考我的 [github](https://www.pytorchtutorial.com/goto/https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/504_batch_normalization.py)
后面还有两张图, 一张是预测曲线, 一张是误差变化曲线, 具体代码不在这里呈现, 想知道如何画图的朋友, 请参考我的 [github](https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/504_batch_normalization.py)
## 对比结果
......@@ -182,6 +182,6 @@ for epoch in range(EPOCH):
上面结果是使用 tanh  作为激励函数的结果, 可以看出, 不好的初始化, 让输入数据在激活前分散得非常离散, 而有了 BN, 数据都被收拢了. 收拢的数据再放入激励函数就能很好地利用激励函数的非线性. 而且可以看出没有 BN 的数据让激活后的结果都分布在 tanh  的两端, 而这两端的梯度又非常的小, 是的后面的误差都不能往前传, 导致神经网络死掉了.
所以这也就是在我 [github 代码](https://www.pytorchtutorial.com/goto/https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/504_batch_normalization.py) 中的每一步的意义啦.
所以这也就是在我 [github 代码](https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents/504_batch_normalization.py) 中的每一步的意义啦.
文章来源:[莫烦](https://www.pytorchtutorial.com/goto/https://morvanzhou.github.io/)
\ No newline at end of file
文章来源:[莫烦](https://morvanzhou.github.io/)
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册