提交 1a68295c 编写于 作者: W wizardforcel

2021-01-20 20:46:05

上级 b55a5440
......@@ -20,7 +20,7 @@ PyTorch 是基于 Python 的科学计算软件包,可实现两个广泛的目
注意
确保已安装[割炬](https://github.com/pytorch/pytorch)[割炬](https://github.com/pytorch/vision)软件包。
确保已安装[`torch`](https://github.com/pytorch/pytorch)[`torchvision`](https://github.com/pytorch/vision)软件包。
![../_img/tensor_illustration_flat.png](img/0c7a402331744a44f5e17575b1607904.png)
......@@ -28,7 +28,7 @@ PyTorch 是基于 Python 的科学计算软件包,可实现两个广泛的目
![../_img/autodiff.png](img/0a7a97c39d6dfc0e08d2701eb7a49231.png)
[torch.Autograd 的简要介绍](blitz/autograd_tutorial.html#sphx-glr-beginner-blitz-autograd-tutorial-py)
[`torch.autograd`的简要介绍](blitz/autograd_tutorial.html#sphx-glr-beginner-blitz-autograd-tutorial-py)
![../_img/mnist1.png](img/be60e8e1f4baa0de87cf9d37c5325525.png)
......
......@@ -155,7 +155,7 @@ tensor([[1., 0., 1., 1.],
```
**连接张量**可以使用`torch.cat`沿给定维度连接一系列张量。 另请参见[火炬堆栈](https://pytorch.org/docs/stable/generated/torch.stack.html),这是另一个与`torch.cat`稍有不同的张量连接操作。
**连接张量**可以使用`torch.cat`沿给定维度连接一系列张量。 另请参见[`torch.stack`](https://pytorch.org/docs/stable/generated/torch.stack.html),这是另一个与`torch.cat`稍有不同的张量连接操作。
```py
t1 = torch.cat([tensor, tensor, tensor], dim=1)
......
......@@ -281,7 +281,7 @@ optimizer.step() # Does the update
注意
观察如何使用`optimizer.zero_grad()`将梯度缓冲区手动设置为零。 这是因为如 [Backprop](#backprop) 部分中所述累积了梯度。
观察如何使用`optimizer.zero_grad()`将梯度缓冲区手动设置为零。 这是因为如[反向传播](#backprop)部分中所述累积了梯度。
**脚本的总运行时间**:(0 分钟 3.778 秒)
......
......@@ -218,7 +218,7 @@ torch.save(net.state_dict(), PATH)
```
有关保存 PyTorch 模型的更多详细信息,请参见此处的[](https://pytorch.org/docs/stable/notes/serialization.html)
有关保存 PyTorch 模型的更多详细信息,请参见[此处](https://pytorch.org/docs/stable/notes/serialization.html)
### 5.根据测试数据测试网络
......@@ -404,7 +404,7 @@ inputs, labels = data[0].to(device), data[1].to(device)
## 我下一步要去哪里?
* [训练神经网络玩视频游戏](../../intermediate/reinforcement_q_learning.html)
* [在 imagenet](https://github.com/pytorch/examples/tree/master/imagenet) 上训练最先进的 ResNet 网络
* [在 imagenet 上训练最先进的 ResNet 网络](https://github.com/pytorch/examples/tree/master/imagenet)
* [使用生成对抗网络训练人脸生成器](https://github.com/pytorch/examples/tree/master/dcgan)
* [使用递归 LSTM 网络训练单词级语言模型](https://github.com/pytorch/examples/tree/master/word_language_model)
* [更多示例](https://github.com/pytorch/examples)
......
......@@ -2,7 +2,7 @@
> 原文:<https://pytorch.org/tutorials/beginner/pytorch_with_examples.html>
**作者**[贾斯汀·约翰逊](https://github.com/jcjohnson/pytorch-examples)
**作者**[Justin Johnson](https://github.com/jcjohnson/pytorch-examples)
本教程通过独立的示例介绍 [PyTorch](https://github.com/pytorch/pytorch) 的基本概念。
......@@ -15,11 +15,11 @@ PyTorch 的核心是提供两个主要功能:
注意
您可以在本页的[端浏览各个示例。](#examples-download)
您可以在[本页](#examples-download)浏览各个示例。
## [张量](#id12)
## 张量
### [预热:numpy](#id13)
### 预热:numpy
在介绍 PyTorch 之前,我们将首先使用 numpy 实现网络。
......@@ -68,7 +68,7 @@ print(f'Result: y = {a} + {b} x + {c} x^2 + {d} x^3')
```
### [PyTorch:张量](#id14)
### PyTorch:张量
Numpy 是一个很棒的框架,但是它不能利用 GPU 来加速其数值计算。 对于现代深度神经网络,GPU 通常会提供 [50 倍或更高](https://github.com/jcjohnson/cnn-benchmarks)的加速,因此遗憾的是,numpy 不足以实现现代深度学习。
......@@ -125,13 +125,13 @@ print(f'Result: y = {a.item()} + {b.item()} x + {c.item()} x^2 + {d.item()} x^3'
```
## [Autograd](#id15)
## Autograd
### [PyTorch:张量和 autograd](#id16)
### PyTorch:张量和 Autograd
在上述示例中,我们必须手动实现神经网络的前向和后向传递。 对于小型的两层网络,手动实施反向传递并不是什么大问题,但是对于大型的复杂网络来说,可以很快变得非常麻烦。
幸运的是,我们可以使用[自动微分](https://en.wikipedia.org/wiki/Automatic_differentiation)来自动计算神经网络中的反向传递。 PyTorch 中的 **autograd** 软件包正是提供了此功能。 使用 Autograd 时,网络的正向传播将定义**计算图**; 图中的节点为张量,边为从输入张量产生输出张量的函数。 然后通过该图进行反向传播,可以轻松计算梯度。
幸运的是,我们可以使用[自动微分](https://en.wikipedia.org/wiki/Automatic_differentiation)来自动计算神经网络中的反向传递。 PyTorch 中的 **Autograd** 软件包正是提供了此功能。 使用 Autograd 时,网络的正向传播将定义**计算图**; 图中的节点为张量,边为从输入张量产生输出张量的函数。 然后通过该图进行反向传播,可以轻松计算梯度。
这听起来很复杂,在实践中非常简单。 每个张量代表计算图中的一个节点。 如果`x`是具有`x.requires_grad=True`的张量,则`x.grad`是另一个张量,其保持`x`相对于某个标量值的梯度。
......@@ -198,7 +198,7 @@ print(f'Result: y = {a.item()} + {b.item()} x + {c.item()} x^2 + {d.item()} x^3'
```
### [PyTorch:定义新的 Autograd 函数](#id17)
### PyTorch:定义新的 Autograd 函数
在幕后,每个原始的 Autograd 运算符实际上都是在张量上运行的两个函数。 **正向**函数从输入张量计算输出张量。 **反向**函数接收相对于某个标量值的输出张量的梯度,并计算相对于相同标量值的输入张量的梯度。
......@@ -293,9 +293,9 @@ print(f'Result: y = {a.item()} + {b.item()} * P3({c.item()} + {d.item()} x)')
```
## [nn 模块](#id18)
## `nn`模块
### [PyTorch:nn](#id19)
### PyTorch:`nn`
计算图和 Autograd 是定义复杂运算符并自动采用导数的非常强大的范例。 但是对于大型神经网络,原始的 Autograd 可能会太低级。
......@@ -380,7 +380,7 @@ print(f'Result: y = {linear_layer.bias.item()} + {linear_layer.weight[:, 0].item
```
### [PyTorch:优化](#id20)
### PyTorch:`optim`
到目前为止,我们已经通过使用`torch.no_grad()`手动更改持有可学习参数的张量来更新模型的权重。 对于像随机梯度下降这样的简单优化算法来说,这并不是一个巨大的负担,但是在实践中,我们经常使用更复杂的优化器(例如 AdaGrad,RMSProp,Adam 等)来训练神经网络。
......@@ -443,7 +443,7 @@ print(f'Result: y = {linear_layer.bias.item()} + {linear_layer.weight[:, 0].item
```
### [PyTorch:自定义 nn 模块](#id21)
### PyTorch:自定义`nn`模块
有时,您将需要指定比一系列现有模块更复杂的模型。 对于这些情况,您可以通过子类化`nn.Module`并定义一个`forward`来定义自己的模块,该模块使用其他模块或在 Tensors 上的其他自动转换操作来接收输入 Tensors 并生成输出 Tensors。
......@@ -510,7 +510,7 @@ print(f'Result: {model.string()}')
```
### [PyTorch:控制流+权重共享](#id22)
### PyTorch:控制流 + 权重共享
作为动态图和权重共享的示例,我们实现了一个非常奇怪的模型:一个三阶多项式,在每个前向传递中选择 3 到 5 之间的一个随机数,并使用该阶数,多次重复使用相同的权重进行计算 第四和第五阶。
......@@ -588,44 +588,44 @@ print(f'Result: {model.string()}')
```
## [示例](#id23)
## 示例
您可以在此处浏览以上示例。
### [张量](#id24)
### 张量
![../_img/sphx_glr_polynomial_numpy_thumb.png](img/ea0bddb69dfbd67215b823007544ab8f.png)
[热身:numpy](examples_tensor/polynomial_numpy.html#sphx-glr-beginner-examples-tensor-polynomial-numpy-py)
[热身:NumPy](examples_tensor/polynomial_numpy.html#sphx-glr-beginner-examples-tensor-polynomial-numpy-py)
![../_img/sphx_glr_polynomial_tensor_thumb.png](img/04ee335faf821b337dba0c4d7ccb0b67.png)
[PyTorch:张量](examples_tensor/polynomial_tensor.html#sphx-glr-beginner-examples-tensor-polynomial-tensor-py)
### [Autograd](#id25)
### Autograd
![../_img/sphx_glr_polynomial_autograd_thumb.png](img/ffad28c33f8a48d06521421f1aa441ed.png)
[PyTorch:张量和自定等级](examples_autograd/polynomial_autograd.html#sphx-glr-beginner-examples-autograd-polynomial-autograd-py)
[PyTorch:张量和 Autograd](examples_autograd/polynomial_autograd.html#sphx-glr-beginner-examples-autograd-polynomial-autograd-py)
![../_img/sphx_glr_polynomial_custom_function_thumb.png](img/a5c5d931ed12e34bf68476f4f157b780.png)
[PyTorch:定义新的 Autograd 函数](examples_autograd/polynomial_custom_function.html#sphx-glr-beginner-examples-autograd-polynomial-custom-function-py)
### [nn 模块](#id26)
### `nn`模块
![../_img/sphx_glr_polynomial_nn_thumb.png](img/335fb81e535f98bfda7cbdb3e50d8832.png)
[PyTorch:nn](examples_nn/polynomial_nn.html#sphx-glr-beginner-examples-nn-polynomial-nn-py)
[PyTorch:`nn`](examples_nn/polynomial_nn.html#sphx-glr-beginner-examples-nn-polynomial-nn-py)
![../_img/sphx_glr_polynomial_optim_thumb.png](img/87aa5017f5f0ba9a29d66e74ac6b3d1a.png)
[PyTorch:最佳](examples_nn/polynomial_optim.html#sphx-glr-beginner-examples-nn-polynomial-optim-py)
[PyTorch:`optim`](examples_nn/polynomial_optim.html#sphx-glr-beginner-examples-nn-polynomial-optim-py)
![../_img/sphx_glr_polynomial_module_thumb.png](img/b3f0b96ed8ba751fee4a5fc7ca878eb1.png)
[PyTorch:自定义 nn 模块](examples_nn/polynomial_module.html#sphx-glr-beginner-examples-nn-polynomial-module-py)
[PyTorch:自定义`nn`模块](examples_nn/polynomial_module.html#sphx-glr-beginner-examples-nn-polynomial-module-py)
![../_img/sphx_glr_dynamic_net_thumb.png](img/bf0b252ce2d39ba6da26c16bee984d39.png)
[PyTorch:控制流+权重共享](examples_nn/dynamic_net.html#sphx-glr-beginner-examples-nn-dynamic-net-py)
\ No newline at end of file
[PyTorch:控制流 + 权重共享](examples_nn/dynamic_net.html#sphx-glr-beginner-examples-nn-dynamic-net-py)
\ No newline at end of file
# PyTorch:控制流+权重共享
# PyTorch:控制流 + 权重共享
> 原文:<https://pytorch.org/tutorials/beginner/examples_nn/dynamic_net.html#sphx-glr-beginner-examples-nn-dynamic-net-py>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册