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**:
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`:
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:
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:
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%.
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:
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**.
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**:
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:
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:
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: