提交 6ed202b1 编写于 作者: W wizardforcel

2021-01-16 21:44:52

上级 6d5c0933
......@@ -387,7 +387,7 @@
y_test_torch =火炬张量(y_test.values)
6. Build a custom module class for defining the layers of the network. Include a forward function that specifies the activation functions that will be applied to the output of each layer. Use **ReLU** for all the layers, except for the output, where you should use **log_softmax**:
6. 构建一个自定义模块类,用于定义网络的层。包括一个前向函数,指定将应用于每层输出的激活函数。对所有层都使用 **ReLU**,除了输出,你应该使用`log_softmax`
类 Classifier(nn.Module):
......@@ -415,7 +415,7 @@
返回
7. Instantiate the model and define all the variables required to train the model. Set the number of epochs to`50`and the batch size to`128`. Use a learning rate of`0.001`:
7. 实例化模型并定义训练模型所需的所有变量。设置纪元数为`50`,批次大小为`128`。使用`0.001`的学习率。
模型=分类器(X_train.shape [1])
......@@ -427,7 +427,7 @@
batch_size = 128
8. Train the network using the training sets' data. Use the validation sets to measure performance. To do this, save the loss and the accuracy for both the training and validation sets in each epoch:
8. 使用训练集的数据来训练网络。使用验证集来衡量性能。要做到这一点,请保存每个时代的训练集和验证集的损失和准确性。
train_losses,dev_losses,
......@@ -509,7 +509,7 @@
“验证准确率:{:. 3f}”。format(acc))
9. Plot the loss of both sets:
9. 绘出两组的损失。
图 = plt.figure(figsize =(15,5))
......@@ -527,7 +527,7 @@
图 3.15:显示训练和验证损失的图
10. Plot the accuracy of both sets:
10. 绘制两组的精度。
无花果= plt.figure(figsize =(15,5))
......@@ -555,7 +555,7 @@
解:
1. Import the same libraries that you used in the previous activity:
1. 导入你在上一个活动中使用的相同的库。
将熊猫作为 pd 导入
......@@ -577,7 +577,7 @@
torch.manual_seed(0)
2. Load the data and split the features from the target. Next, split the data into the three subsets (training, validation, and testing) using a 60:20:20 split ratio. Finally, convert the validation and testing sets into PyTorch tensors, just as you did in the previous activity:
2. 加载数据并从目标中拆分特征。接下来,使用 60:20:20 的分割比例将数据分割成三个子集(训练、验证和测试)。最后,将验证和测试集转换为 PyTorch 张量,就像您在上一个活动中所做的那样。
数据= pd.read_csv(“ dccc_prepared.csv”)
......@@ -609,7 +609,7 @@
y_test_torch =火炬张量(y_test.values)
3. Considering that the model is suffering from high bias, the focus should be on increasing the number of epochs or increasing the size of the network by adding additional layers or units to each layer. The aim should be to approximate the accuracy over the validation set to 80%.
3. 考虑到该模型存在较高的偏差,重点应放在增加纪元的数量上,或通过在每层中增加额外的层或单位来增加网络的规模。目标应该是将验证集的准确度近似到80%。
之后,将显示性能最佳的模型,该模型是在几次微调尝试之后实现的。 首先,定义模型架构和正向传递,如以下代码片段所示:
......@@ -751,7 +751,7 @@
可以在以前共享的 GitHub 存储库中找到此活动随附的 Jupyter Notebook。 在那里,您会发现对模型进行微调的各种尝试及其结果。 性能最佳的型号可以在笔记本电脑的末尾找到。
4. Plot the loss and accuracy for both sets of data:
4. 绘制两组数据的损失和准确性。
注意
......@@ -793,7 +793,7 @@
图 3.18:显示集合精度的图
5. Using the best-performing model, perform a prediction over the testing set (which should not have been used during the fine-tuning process). Compare the prediction with the ground truth by calculating the accuracy of the model over this set:
5. 使用性能最好的模型,对测试集(在微调过程中不应该使用)进行预测。通过计算模型在该集上的准确度,将预测结果与基本事实进行比较。
model.eval()
......@@ -819,8 +819,8 @@
### 解决方案
1. 打开用于上一个活动的 Jupyter Notebook
2. Copy the class containing the architecture of your best-performing model and save it in a Python file. Make sure that you import PyTorch's required libraries and modules. Name it **final_model.py**.
1. 打开用于上一个活动的 Jupyter 笔记本
2. 复制包含最佳性能模型架构的类,并将其保存在 Python 文件中。确保导入了 PyTorch 所需的库和模块,并将其命名为`final_model.py`。将其命名为`final_model.py`
该文件应如下所示:
......@@ -828,7 +828,7 @@
图 3.19:final_model.py 的屏幕截图
3. In the Jupyter Notebook, save the best-performing model. Make sure to save the information pertaining to the input units, along with the parameters of the model. Name it **checkpoint.pth**:
3. 在Jupyter 笔记本中,保存表现最好的模型。请务必保存与输入单位相关的信息,以及模型的参数。将其命名为`checkpoint.pth`
检查点= {“输入”:X_train.shape [1],\
......@@ -837,13 +837,13 @@
torch.save(检查点,“ checkpoint.pth”)
4. 打开一个新的 Jupyter Notebook。
5. Import PyTorch, as well as the Python file we created in *Step 2*:
5. 导入PyTorch,以及我们在“步骤 2”中创建的Python文件。
进口火炬
导入 final_model
6. Create a function that loads the model:
6. 创建一个加载模型的函数。
def load_model_checkpoint(path):
......@@ -857,7 +857,7 @@
模型= load_model_checkpoint(“ checkpoint.pth”)
7. Perform a prediction by inputting the following tensor into your model:
7. 通过将以下张量输入到你的模型中进行预测。
例子= torch.tensor([[0.0606,0.5000,0.3333,0.4828,\
......@@ -879,13 +879,13 @@
通过打印`top_class_test`,我们可以获得模型的预测,在这种情况下,该预测等于`1`(是)。
8. Convert the model using the JIT module:
8. 使用JIT模块转换模型。
traced_script = torch.jit.trace(模型,例如\
check_trace = False)
9. Perform a prediction by inputting the same tensor as in *Step 7* to the traced script of your model:
9. 通过输入“步骤 7”的相同张量到模型的跟踪脚本中进行预测。
预测= traced_script(示例)
......@@ -895,7 +895,7 @@
通过打印`top_class_test_2`,我们从模型的跟踪脚本表示中获得了预测,该预测再次等于`1`(是)。
10. Open a new Jupyter Notebook and import the required libraries to create an API using Flask, as well as the libraries to load the saved model:
10. 打开一个新的Jupyter 笔记本,并导入所需的库来使用Flask创建一个API,以及加载保存的模型的库。
进口烧瓶
......@@ -905,13 +905,13 @@
导入 final_model
11. Initialize the Flask app:
11. 初始化Flask应用。
app = flask.Flask(__ name__)
app.config [“ DEBUG”] = True
12. Define a function that loads the saved model and then instantiate the model:
12. 定义一个函数,加载保存的模型,然后实例化模型。
def load_model_checkpoint(path):
......@@ -925,7 +925,7 @@
模型= load_model_checkpoint(“ checkpoint.pth”)
13. Define the route of the API to **/prediction** and set the method to **POST**. Then, define the function that will receive the **POST** data and feed it to the model to perform a prediction:
13. 定义API的路由为`/prediction`,并将方法设置为`POST`。然后,定义接收`POST`数据的函数,并将其反馈给模型进行预测。
@ app.route('/ prediction',methods = ['POST'])
......@@ -945,7 +945,7 @@
return {“ status”:“ ok”,“ result”:int(top_class_test [0] [0])}
14. Run the Flask app:
14. 运行Flask应用。
app.run(debug = True,use_reloader = False)
......@@ -987,7 +987,7 @@
导入 matplotlib.pyplot 作为 plt
2. Set the transformations to be performed on the data, which will be converting the data into tensors and normalizing the pixel values:
2. 设置要对数据进行的变换,将数据转换为张量并对像素值进行归一化。
变换= \
......@@ -997,7 +997,7 @@
(0.5, 0.5, 0.5))])
3. Set a batch size of 100 images and download both the training and testing data from the **CIFAR10** dataset:
3. 设置批量大小为100张图像,并从 **CIFAR10** 数据集下载训练和测试数据。
batch_size = 100
......@@ -1015,7 +1015,7 @@
前面的代码将下载可通过 PyTorch 的`Torchvision`软件包获得的训练和测试数据集。 根据上一步中定义的转换对数据集进行转换。
4. Using a validation size of 20%, define the training and validation sampler that will be used to divide the dataset into those two sets:
4. 使用 20% 的验证大小,定义训练和验证采样器,用于将数据集划分为这两组。
dev_size = 0.2
......@@ -1033,7 +1033,7 @@
为了将训练集分为两组(训练和验证),为每个组定义了一个索引列表,然后可以使用`SubsetRandomSampler`函数对其进行随机采样。
5. Use the **DataLoader()** function to define the batches of each set of data to be used:
5. 使用`DataLoader()`函数来定义要使用的每一组数据的批次。
train_loader = \
......@@ -1059,7 +1059,7 @@
PyTorch 的`DataLoader`函数用于创建批量,这些批量将在开发过程的训练,验证和测试阶段馈送到模型中。
6. Define the architecture of your network. Use the following information to do so:
6. 定义你的网络架构。使用以下信息进行定义。
Conv1:卷积层,将彩色图像作为输入,并将其通过大小为 3 的 10 个滤镜。应将 padding 和 stride 都设置为 1。
......@@ -1119,7 +1119,7 @@
前面的代码段包含一个定义了网络架构的类(`__init__`方法),以及在信息正向传递过程中所遵循的步骤(`forward`方法)。
7. Define all of the parameters that are required to train your model. Set the number of epochs to`50`:
7. 定义训练模型所需的所有参数。设置周期数为`50`
型号= CNN()
......@@ -1135,7 +1135,7 @@
型号= CNN()。to(“ cuda”)
8. Train your network and be sure to save the values for the loss and accuracy of both the training and validation sets:
8. 训练你的网络,并确保保存训练集和验证集的损失和准确性的值。
train_losses,dev_losses,train_acc,dev_acc = [],[],[],[]
......@@ -1381,7 +1381,7 @@
.format(dev_accs / iter_2))
9. Plot the loss and accuracy of both sets. To plot the loss, use the following code:
9. 绘制这两组数据的损失和精度。要绘制损失,请使用以下代码。
plt.plot(x_axis,train_losses,label ='训练损失')
......@@ -1415,7 +1415,7 @@
可以看出,在第 15 个时期之后,过拟合开始影响模型。
10. Check the model's accuracy on the testing set:
10. 在测试集上检查模型的准确性。
model.eval()
......@@ -1477,11 +1477,11 @@
### 解决方案
1. Duplicate the notebook from the previous activity.
1. 复制之前活动中的笔记本。
为了完成此活动,按照以下步骤,除了修改`tranforms`值之外,不会更改任何代码。
2. Change the definition of the **transform** variable so that it includes, in addition to normalizing and converting the data into tensors, the following transformations:
2. 修改`transform`变量的定义,使其除了对数据进行归一化和转换为张量外,还包括以下转换:
对于训练/验证集,请使用`RandomHorizo​​ntalFlip`函数,其概率为 50% (`0.5`),并使用`RandomGrayscale`函数,其概率为 10% (`0.1`)。
......@@ -1509,7 +1509,7 @@
(0.5, 0.5, 0.5))])}
3. Train the model for 100 epochs.
3. 训练模型100个纪元。
如果您的计算机具有可用的 GPU,请确保使用代码的 GPU 版本来训练模型。
......@@ -1525,7 +1525,7 @@
通过添加数据扩充,可以改善模型的性能,并减少发生的过拟合。
4. Calculate the accuracy of the resulting model on the testing set.
4. 计算所得模型在测试集上的精度。
该模型在测试设备上的性能提高了约 75%。
......@@ -1541,11 +1541,11 @@
### 解决方案
1. Duplicate the notebook from the previous activity.
1. 复制之前活动中的笔记本。
要完成此活动,按照以下步骤,除了在网络架构中添加一些层之外,不会更改任何代码。
2. Add batch normalization to each convolutional layer, as well as to the first fully connected layer.
2. 将批量归一化添加到每个卷积层,以及第一个完全连接层。
网络的最终架构应如下:
......@@ -1597,7 +1597,7 @@
返回 x
3. Train the model for 100 epochs.
3. 训练模型100个纪元。
如果您的计算机具有可用的 GPU,请确保使用代码的 GPU 版本来训练模型。 训练和验证集的损失和准确率的结果图应类似于此处所示:
......@@ -1615,7 +1615,7 @@
尽管本章未对此进行探讨,但理想的步骤是为网络架构添加辍学以减少高方差。 随意尝试一下,看看您是否能够进一步提高性能。
4. Calculate the accuracy of the resulting model on the testing set.
4. 计算所得模型在测试集上的精度。
该模型在测试设备上的性能已提高了约 78%。
......@@ -1651,7 +1651,7 @@
device = "cuda"
2. Specify the transformations to be performed over the input images. Be sure to resize them to the same size, convert them into tensors, and normalize them:
2. 指定要对输入图像进行的变换。请确保将它们调整为相同的大小,将它们转换为张力,并将它们标准化。
尺寸= 224
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册