Skip to content

Commit

Permalink
2020-10-19 22:31:47
Browse files Browse the repository at this point in the history
  • Loading branch information
wizardforcel committed Oct 19, 2020
1 parent e92cc30 commit 60bea08
Show file tree
Hide file tree
Showing 21 changed files with 50 additions and 50 deletions.
6 changes: 3 additions & 3 deletions docs/pytorch/02.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

## 为什么用 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 吧.

![](img/20e2ebdf112e4aa3202e951e072c2dc2.png)

可见, 著名的 Facebook, twitter 等都在使用它, 这就说明 PyTorch 的确是好用的, 而且是值得推广.

而且如果你知道 [Numpy](https://www.pytorchtutorial.com/goto/http://www.numpy.org/), PyTorch 说他就是在神经网络领域可以用来替换 numpy 的模块.
而且如果你知道 [Numpy](http://www.numpy.org/), PyTorch 说他就是在神经网络领域可以用来替换 numpy 的模块.

## 神经网络在做什么

Expand All @@ -33,4 +33,4 @@
* 如果你是学生, 随便选一个学, 或者稍稍偏向 PyTorch, 因为写代码的时候应该更好理解. 懂了一个模块, 转换 Tensorflow 或者其他的模块都好说.
* 如果是上班了, 跟着你公司来, 公司用什么, 你就用什么, 不要脱群.

文章来源:[莫烦](https://www.pytorchtutorial.com/goto/https://morvanzhou.github.io/)
文章来源:[莫烦](https://morvanzhou.github.io/)
6 changes: 3 additions & 3 deletions docs/pytorch/03.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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/)
文章来源:[莫烦](https://morvanzhou.github.io/)
4 changes: 2 additions & 2 deletions docs/pytorch/05.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 绝对值计算
Expand Down Expand Up @@ -71,4 +71,4 @@ print(
)
```

文章来源:[莫烦](https://www.pytorchtutorial.com/goto/https://morvanzhou.github.io/)
文章来源:[莫烦](https://morvanzhou.github.io/)
2 changes: 1 addition & 1 deletion docs/pytorch/06.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,4 @@ print(variable.data.numpy()) # numpy 形式
"""
```

文章来源:[莫烦](https://www.pytorchtutorial.com/goto/https://morvanzhou.github.io/)
文章来源:[莫烦](https://morvanzhou.github.io/)
2 changes: 1 addition & 1 deletion docs/pytorch/07.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ plt.legend(loc=\'best\')
plt.show()
```

文章来源:[莫烦](https://www.pytorchtutorial.com/goto/https://morvanzhou.github.io/)
文章来源:[莫烦](https://morvanzhou.github.io/)
4 changes: 2 additions & 2 deletions docs/pytorch/09.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
文章来源:[莫烦](https://morvanzhou.github.io/)
4 changes: 2 additions & 2 deletions docs/pytorch/10.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
文章来源:[莫烦](https://morvanzhou.github.io/)
4 changes: 2 additions & 2 deletions docs/pytorch/11.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
文章来源:[莫烦](https://morvanzhou.github.io/)
4 changes: 2 additions & 2 deletions docs/pytorch/12.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
文章来源:[莫烦](https://morvanzhou.github.io/)
4 changes: 2 additions & 2 deletions docs/pytorch/13.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
文章来源:[莫烦](https://morvanzhou.github.io/)
4 changes: 2 additions & 2 deletions docs/pytorch/14.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
文章来源:[莫烦](https://morvanzhou.github.io/)
4 changes: 2 additions & 2 deletions docs/pytorch/16.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
文章来源:[莫烦](https://morvanzhou.github.io/)
4 changes: 2 additions & 2 deletions docs/pytorch/17.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
文章来源:[莫烦](https://morvanzhou.github.io/)
4 changes: 2 additions & 2 deletions docs/pytorch/18.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
文章来源:[莫烦](https://morvanzhou.github.io/)
4 changes: 2 additions & 2 deletions docs/pytorch/19.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
文章来源:[莫烦](https://morvanzhou.github.io/)
6 changes: 3 additions & 3 deletions docs/pytorch/20.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 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>

Expand Down Expand Up @@ -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/)
文章来源:[莫烦](https://morvanzhou.github.io/)
8 changes: 4 additions & 4 deletions docs/pytorch/21.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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/)
文章来源:[莫烦](https://morvanzhou.github.io/)
6 changes: 3 additions & 3 deletions docs/pytorch/23.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
..
Expand Down Expand Up @@ -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/)
文章来源:[莫烦](https://morvanzhou.github.io/)
Loading

0 comments on commit 60bea08

Please sign in to comment.