You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Proceed to Get Started with Arm Performance Studio for mobile to continue learning about Android performance analysis.
7
+
Continue to learn how to train the model with PyTorch and use it for inference.
8
8
9
9
# 1-3 sentence recommendation outlining how the reader can generally keep learning about these topics, and a specific explanation of why the next step is being recommended.
Copy file name to clipboardExpand all lines: content/learning-paths/cross-platform/pytorch-digit-classification-training/_index.md
+14-6Lines changed: 14 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,32 +1,40 @@
1
1
---
2
-
title: Learn how to train the PyTorch model for digit classification
2
+
title: Train a PyTorch model for digit classification
3
3
minutes_to_complete: 40
4
4
5
5
who_is_this_for: This is an introductory topic for software developers interested in learning how to use PyTorch to train a feedforward neural network for digit classification.
6
6
7
7
learning_objectives:
8
-
- Download and prepare the dataset.
8
+
- Download and prepare the MNIST dataset.
9
9
- Train a neural network using PyTorch.
10
10
11
11
prerequisites:
12
-
- A x86_64 or Apple development machine with Code Editor (we recommend Visual Studio Code).
12
+
- Any computer which can run Python3 and Visual Studio Code, this can be Windows, Linux, or macOS.
13
+
- You should complete [Create a PyTorch model for digit classification](/learning-paths/cross-platform/pytorch-digit-classification-architecture/) before starting this Learning Path.
Proceed to Get Started with Arm Performance Studio for mobile to continue learning about Android performance analysis.
7
+
Proceed to Use Keras Core with TensorFlow, PyTorch, and JAX backends to continue exploring Machine Learning.
8
8
9
9
# 1-3 sentence recommendation outlining how the reader can generally keep learning about these topics, and a specific explanation of why the next step is being recommended.
Copy file name to clipboardExpand all lines: content/learning-paths/cross-platform/pytorch-digit-classification-training/datasets-and-training.md
+27-21Lines changed: 27 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,11 +7,13 @@ weight: 3
7
7
layout: "learningpathall"
8
8
---
9
9
10
-
We start by downloading the MNIST dataset. Proceed as follows:
11
-
1. Open, the pytorch-digits.ipynb you created in this [Learning Path](learning-paths/cross-platform/pytorch-digit-classification-architecture).
10
+
Start by downloading the MNIST dataset. Proceed as follows:
11
+
12
+
1. Open, the pytorch-digits.ipynb you created in [Create a PyTorch model for digit classification](/learning-paths/cross-platform/pytorch-digit-classification-architecture/).
The above code snippet downloads the MNIST dataset, transforms the images into tensors, and sets up data loaders for training and testing. Specifically, the datasets.MNIST function is used to download the MNIST dataset, with train=True indicating training data and train=False indicating test data. The transform=transforms.ToTensor() argument converts each image in the dataset into a PyTorch tensor, which is necessary for model training and evaluation.
43
+
The above code snippet downloads the MNIST dataset, transforms the images into tensors, and sets up data loaders for training and testing. Specifically, the `datasets.MNIST` function is used to download the MNIST dataset, with `train=True` indicating training data and `train=False` indicating test data. The `transform=transforms.ToTensor()` argument converts each image in the dataset into a PyTorch tensor, which is necessary for model training and evaluation.
42
44
43
45
The DataLoader wraps the datasets and allows efficient loading of data in batches. It handles data shuffling, batching, and parallel loading. Here, the train_dataloader and test_dataloader are created with a batch_size of 32, meaning they will load 32 images per batch during training and testing.
44
46
45
47
This setup prepares the training and test datasets for use in a machine learning model, enabling efficient data handling and model training in PyTorch.
46
48
47
49
To run the above code, you will need to install certifi package:
50
+
48
51
```console
49
52
pip install certifi
50
53
```
51
54
52
-
certifi is a Python package that provides the Mozilla’s root certificates, which are essential for ensuring the SSL connections are secure. If you’re using macOS, you may also need to install the certificates by running:
55
+
The certifi Python package provides the Mozilla root certificates, which are essential for ensuring the SSL connections are secure. If you’re using macOS, you may also need to install the certificates by running:
The first method, train_loop, uses the backpropagation algorithm to optimize the trainable parameters and minimize the prediction error of the neural network. The second method, test_loop, calculates the neural network error using the test images and displays the accuracy and loss values.
112
+
The first method, `train_loop`, uses the backpropagation algorithm to optimize the trainable parameters and minimize the prediction error of the neural network. The second method, `test_loop`, calculates the neural network error using the test images and displays the accuracy and loss values.
109
113
110
-
We can now invoke these methods to train and evaluate the model. Similarly to TensorFlow, we use 10 epochs.
114
+
You can now invoke these methods to train and evaluate the model using 10 epochs.
which shows the model achieved around 95% of accuracy.
133
137
134
-
# Saving the model
135
-
Once the model is trained, we can save it. There are various approaches for this. In PyTorch, you can save both the model’s structure and its weights to the same file using the torch.save() function. Alternatively, you can save only the weights (parameters) of the model, not the model architecture itself. This requires you to have the model’s architecture defined separately when loading. To save the model weights, you can use the following command:
138
+
# Save the model
139
+
140
+
Once the model is trained, you can save it. There are various approaches for this. In PyTorch, you can save both the model’s structure and its weights to the same file using the `torch.save()` function. Alternatively, you can save only the weights (parameters) of the model, not the model architecture itself. This requires you to have the model’s architecture defined separately when loading. To save the model weights, you can use the following command:
However, PyTorch does not save the definition of the class itself. When you load the model using torch.load(), PyTorch needs to know the class definition to recreate the model object.
146
+
However, PyTorch does not save the definition of the class itself. When you load the model using `torch.load()`, PyTorch needs to know the class definition to recreate the model object.
142
147
143
148
Therefore, when you later want to use the saved model for inference, you will need to provide the definition of the model class.
144
149
145
150
Alternatively, you can use TorchScript, which serializes both the architecture and weights into a single file that can be loaded without needing the original class definition. This is particularly useful for deploying models to production or sharing models without code dependencies.
146
151
147
-
Here, we use TorchScript and save the model using the following commands:
152
+
Use TorchScript to save the model using the following commands:
The above commands set the model to evaluation mode (model.eval()), then trace the model, and save it. Tracing is useful for converting models with static computation graphs to TorchScript, making them portable and independent of the original class definition.
165
+
The above commands set the model to evaluation mode, trace the model, and save it. Tracing is useful for converting models with static computation graphs to TorchScript, making them portable and independent of the original class definition.
161
166
162
167
Setting the model to evaluation mode before tracing is important for several reasons:
168
+
163
169
1. Behavior of Layers like Dropout and BatchNorm:
164
-
* Dropout. During training (model.train()), dropout randomly zeroes out some of the activations to prevent overfitting. During evaluation (model.eval()), dropout is turned off, and all activations are used.
165
-
* BatchNorm. During training, Batch Normalization layers use batch statistics to normalize the input. During evaluation, they use running averages calculated during training.
170
+
* Dropout. During training, dropout randomly zeroes out some of the activations to prevent overfitting. During evaluation dropout is turned off, and all activations are used.
171
+
* BatchNorm. During training, Batch Normalization layers use batch statistics to normalize the input. During evaluation, they use running averages calculated during training.
166
172
167
-
2. Consistent Inference Behavior. By setting the model to eval() mode, you ensure that the traced model will behave consistently during inference, as it will not use dropout or batch statistics that are inappropriate for inference.
173
+
2. Consistent Inference Behavior. By setting the model to eval mode, you ensure that the traced model will behave consistently during inference, as it will not use dropout or batch statistics that are inappropriate for inference.
168
174
169
175
3. Correct Tracing. Tracing captures the operations performed by the model using a given input. If the model is in training mode, the traced graph may include operations related to dropout and batch normalization updates. These operations can affect the correctness and performance of the model during inference.
170
176
171
-
In the next step, we will use the saved model for inference.
177
+
In the next step, you will use the saved model for inference.
0 commit comments