提交 820885c0 编写于 作者: W wizardforcel

2021-01-16 19:50:48

上级 6a21e5cd
......@@ -114,7 +114,7 @@ random.set_seed(seed)
## 在 Linux 上安装 Python
1. Open your Terminal and type the following command:
1. 打开终端并输入以下命令:
```py
sudo apt-get install python3.7
......@@ -135,7 +135,7 @@ pip --version
如果您的计算机无法识别`pip`(或`pip3`)命令,请按照以下步骤进行安装:
1. 要安装`pip`,[请访问以下链接并下载`get-pip.py`文件](https://pip.pypa.io/en/stable/installing/)。
2. Then, on the Terminal or Command Prompt, use the following command to install it:
2. 然后,在终端或命令提示符上,使用以下命令进行安装:
```py
python get-pip.py
......@@ -161,7 +161,7 @@ pip --version
1. 打开终端/命令提示符。
2. 在“终端/命令提示符”中,转到您下载了该书的 GitHub 存储库的目录位置。
3. Open a Jupyter Notebook by typing in the following command:
3. 通过键入以下命令来打开 Jupyter 笔记本:
```py
jupyter notebook
......
......@@ -22,7 +22,7 @@ PyTorch 于 2017 年推出,其主要特点是它使用**图形处理单元**
尽管神经网络在几十年前就已经理论化了,但神经网络最近变得流行有两个主要原因:
* Neural networks require, and actually capitalize on, vast amounts of labeled data to achieve an optimal solution. This means that for the algorithm to create an outstanding model, it requires hundreds of thousands or even millions of entries, containing both the features and the target values. For instance, as regards the image recognition of cats, the more images you have, the more features the model is able to detect, which makes it better.
* 神经网络需要并实际上利用大量标记数据来实现最佳解决方案。 这意味着,要使用该算法创建出色的模型,就需要成千上万甚至上百万个条目,其中包含特征和目标值。 例如,关于猫的图像识别,您拥有的图像越多,该模型能够检测的功能就越多,这使其变得更好。
注意
......@@ -36,7 +36,7 @@ PyTorch 于 2017 年推出,其主要特点是它使用**图形处理单元**
如今,由于软件和硬件的进步使我们能够收集和处理这种粒度,这成为可能。
* Neural networks require considerable computing power to be able to process such large amounts of data without taking weeks (or even longer) to be trained. Because the process of achieving the best possible model is based on trial and error, it is necessary to be able to run the training process as efficiently as possible.
* 神经网络需要相当大的计算能力才能处理如此大量的数据,而无需花费数周(甚至更长)的时间来进行训练。 由于获得最佳模型的过程是基于反复试验的,因此有必要能够尽可能高效地运行训练过程。
今天,这可以通过使用 GPU 来实现,它可以将神经网络的训练时间从数周缩短至数小时。
......@@ -167,13 +167,13 @@ example_2 = torch.randint(low=0, high=2, \
对于本章中的练习和活动,您将需要安装 Python 3.7,Jupyter 6.0,Matplotlib 3.1 和 PyTorch 1.3+(最好是 PyTorch 1.4,有或没有 CUDA)(如“前言”中的说明) )。 它们将主要在 Jupyter 笔记本电脑中开发,建议您为不同的作业保留一个单独的笔记本电脑,除非建议不要这样做。
1. Import the PyTorch library called **torch**:
1. 导入名为`torch`的 PyTorch 库:
```py
import torch
```
2. Create tensors of the following ranks:`1`,`2`, and`3`.
2. 创建以下等级的张量:`1`,`2`和`3`。
使用`0`和`1`之间的值填充张量。 张量的大小可以根据您的需要进行定义,前提是正确创建了等级:
......@@ -194,7 +194,7 @@ example_2 = torch.randint(low=0, high=2, \
[[0.3,0.6],[0,1]]]).cuda()
```
3. Print the shape of each of the tensors using the **shape** property, just as you would do with NumPy arrays:
3. 就像使用 NumPy 数组一样,使用`shape`属性输出每个张量的形状:
```py
print(tensor_1.shape)
......@@ -328,7 +328,7 @@ loss_funct = nn.MSELoss()
在本练习中,我们将使用 PyTorch `nn`模块为单层神经网络定义模型,并定义损失函数以评估模型。 这将是起点,以便您能够构建更复杂的网络架构来解决实际数据问题。 执行以下步骤以完成本练习:
1. Import **torch** as well as the **nn** module from PyTorch:
1. PyTorch 导入`torch``nn`模块:
```py
import torch
......@@ -341,21 +341,21 @@ loss_funct = nn.MSELoss()
要了解有关 PyTorch 中种子的更多信息,请访问[这里](https://pytorch.org/docs/stable/notes/randomness.html)
2. Define the number of features of the input data as`10`(**input_units**) and the number of nodes of the output layer as`1`(**output_units**):
2. 定义输入数据的特征数为`10``input_units`),输出层的节点数为`1``output_units`)。
```py
input_units = 10
output_units = 1
```
3. Using the **Sequential()** container, define a single-layer network architecture and store it in a variable named **model**. Make sure to define one layer, followed by a **Sigmoid** activation function:
3. 使用`Sequential()`容器,定义单层网络体系结构并将其存储在名为`model`的变量中。 确保定义一层,然后定义`Sigmoid`激活函数:
```py
model = nn.Sequential(nn.Linear(input_units, output_units), \
                      nn.Sigmoid())
```
4. Print your model to verify that it was created accordingly:
4. 打印模型以验证是否已相应创建:
```py
print(model)
......@@ -370,13 +370,13 @@ loss_funct = nn.MSELoss()
)
```
5. Define the loss function as the MSE and store it in a variable named **loss_funct**:
5. 将损失函数定义为 MSE 并将其存储在名为`loss_funct`的变量中:
```py
loss_funct = nn.MSELoss()
```
6. Print your loss function to verify that it was created accordingly:
6. 打印损失函数以验证是否已相应创建:
```py
print(loss_funct)
......@@ -453,7 +453,7 @@ for i in range(100):
在本练习中,我们将使用 PyTorch 的`optim`包,学习如何从上一练习中训练单层网络。 考虑到我们将使用虚拟数据作为输入,训练网络不会解决数据问题,但是将其用于学习目的。 执行以下步骤以完成本练习:
1. Import **torch**, the **optim** package from PyTorch, and **matplotlib**:
1. 导入`torch`、PyTorch 的`optim`包和`matplotlib`。
```py
import torch
......@@ -461,20 +461,19 @@ for i in range(100):
import matplotlib.pyplot as plt
```
2. Create dummy input data (`x`) of random values and dummy target data (`y`) that only contains zeros and ones. Tensor`x`should have a size of (**20,10**), while the size of`y`should be (**20,1**):
2. 创建随机值的虚拟输入数据(`x`)和仅包含零和一的虚拟目标数据(`y`)。 张量`x`的大小应为`(20, 10)`,而`y`的大小应为`(20, 1)`:
```py
x = torch.randn(20,10)
y = torch.randint(0,2, (20,1)).type(torch.FloatTensor)
```
3. Define the optimization algorithm as the Adam optimizer. Set the learning rate equal to`0.01`:
3. 将优化算法定义为 Adam 优化器。 将学习率设置为等于 0.01:
```py
optimizer = optim.Adam(model.parameters(), lr=0.01)
```
4. Run the optimization for 20 iterations, saving the value of the loss in a variable. Every five iterations, print the loss value:
4. 运行优化 20 次迭代,将损失值保存在变量中。 每五次迭代,输出损失值:
```py
losses = []
......@@ -500,7 +499,7 @@ for i in range(100):
前面的输出显示纪元号以及损失函数的值,可以看出,该函数正在减小。 这意味着训练过程使损失函数最小化,这意味着模型能够理解输入特征和目标之间的关系。
5. Make a line plot to display the value of the loss function in each epoch:
5. 绘制线图以显示每个时期的损失函数的值:
```py
plt.plot(range(0,20), losses)
......@@ -534,13 +533,13 @@ for i in range(100):
用于此活动的数据集来自 UC Irvine 机器学习存储库,[该存储库可使用以下 URL 从**数据文件夹**超链接中下载](https://archive.ics.uci.edu/ml/datasets/Somerville+Happiness+Survey)。 [也可以在本书的 GitHub 存储库中找到它](https://packt.live/38gzpr5)。
1. 导入所需的库,包括用于读取 CSV 文件的熊猫。
2. Read the CSV file containing the dataset.
2. 读取包含数据集的 CSV 文件。
注意
建议使用熊猫的`read_csv`函数加载 CSV 文件。 要了解有关此功能的更多信息,请访问[这里](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html)。
3. Separate the input features from the target. Note that the target is located in the first column of the CSV file. Next, convert the values into tensors, making sure the values are converted into floats.
3. 将输入特征与目标分开。 请注意,目标位于 CSV 文件的第一列中。 接下来,将值转换为张量,确保将值转换为浮点数。
注意
......@@ -550,7 +549,7 @@ for i in range(100):
5. 定义要使用的损失函数。 在这种情况下,请使用 MSE 损失功能。
6. 定义模型的优化器。 在这种情况下,请使用 Adam 优化器,并将学习率设为`0.01`。
7. 对 100 次迭代运行优化,保存每次迭代的损失值。 每 10 次迭代打印一次损失值。
8. Make a line plot to display the loss value for each iteration step.
8. 绘制线图以显示每个迭代步骤的损耗值。
注意
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册