diff --git a/content/learning-paths/cross-platform/pytorch-digit-classification-architecture/_index.md b/content/learning-paths/cross-platform/pytorch-digit-classification-architecture/_index.md index c6a079fd32..359acf07ec 100644 --- a/content/learning-paths/cross-platform/pytorch-digit-classification-architecture/_index.md +++ b/content/learning-paths/cross-platform/pytorch-digit-classification-architecture/_index.md @@ -1,8 +1,5 @@ --- title: Create a PyTorch model for digit classification -draft: true -cascade: - draft: true minutes_to_complete: 40 diff --git a/content/learning-paths/cross-platform/pytorch-digit-classification-architecture/_next-steps.md b/content/learning-paths/cross-platform/pytorch-digit-classification-architecture/_next-steps.md index 4afb181fd1..6ad3878391 100644 --- a/content/learning-paths/cross-platform/pytorch-digit-classification-architecture/_next-steps.md +++ b/content/learning-paths/cross-platform/pytorch-digit-classification-architecture/_next-steps.md @@ -4,11 +4,11 @@ # ================================================================================ next_step_guidance: > - Proceed to Get Started with Arm Performance Studio for mobile to continue learning about Android performance analysis. + Continue to learn how to train the model with PyTorch and use it for inference. # 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. -recommended_path: "/learning-paths/smartphones-and-mobile/ams/" +recommended_path: "/learning-paths/cross-platform/pytorch-digit-classification-training/" # Link to the next learning path being recommended(For example this could be /learning-paths/servers-and-cloud-computing/mongodb). diff --git a/content/learning-paths/cross-platform/pytorch-digit-classification-training/_index.md b/content/learning-paths/cross-platform/pytorch-digit-classification-training/_index.md index e252a2c34b..6a54885136 100644 --- a/content/learning-paths/cross-platform/pytorch-digit-classification-training/_index.md +++ b/content/learning-paths/cross-platform/pytorch-digit-classification-training/_index.md @@ -1,32 +1,40 @@ --- -title: Learn how to train the PyTorch model for digit classification +title: Train a PyTorch model for digit classification minutes_to_complete: 40 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. learning_objectives: - - Download and prepare the dataset. + - Download and prepare the MNIST dataset. - Train a neural network using PyTorch. prerequisites: - - A x86_64 or Apple development machine with Code Editor (we recommend Visual Studio Code). + - Any computer which can run Python3 and Visual Studio Code, this can be Windows, Linux, or macOS. + - You should complete [Create a PyTorch model for digit classification](/learning-paths/cross-platform/pytorch-digit-classification-architecture/) before starting this Learning Path. author_primary: Dawid Borycki ### Tags skilllevels: Introductory -subjects: Neural Networks +subjects: ML armips: - Cortex-A - Cortex-X + - Neoverse operatingsystems: - Windows - Linux - - MacOS + - macOS tools_software_languages: - - Android Studio - Coding +shared_path: true +shared_between: + - servers-and-cloud-computing + - laptops-and-desktops + - smartphones-and-mobile + + ### FIXED, DO NOT MODIFY # ================================================================================ weight: 1 # _index.md always has weight of 1 to order correctly diff --git a/content/learning-paths/cross-platform/pytorch-digit-classification-training/_next-steps.md b/content/learning-paths/cross-platform/pytorch-digit-classification-training/_next-steps.md index 4afb181fd1..f8ad807964 100644 --- a/content/learning-paths/cross-platform/pytorch-digit-classification-training/_next-steps.md +++ b/content/learning-paths/cross-platform/pytorch-digit-classification-training/_next-steps.md @@ -4,11 +4,11 @@ # ================================================================================ next_step_guidance: > - Proceed to Get Started with Arm Performance Studio for mobile to continue learning about Android performance analysis. + Proceed to Use Keras Core with TensorFlow, PyTorch, and JAX backends to continue exploring Machine Learning. # 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. -recommended_path: "/learning-paths/smartphones-and-mobile/ams/" +recommended_path: "/learning-paths/servers-and-cloud-computing/keras-core/" # Link to the next learning path being recommended(For example this could be /learning-paths/servers-and-cloud-computing/mongodb). diff --git a/content/learning-paths/cross-platform/pytorch-digit-classification-training/datasets-and-training.md b/content/learning-paths/cross-platform/pytorch-digit-classification-training/datasets-and-training.md index 577084b238..b414228182 100644 --- a/content/learning-paths/cross-platform/pytorch-digit-classification-training/datasets-and-training.md +++ b/content/learning-paths/cross-platform/pytorch-digit-classification-training/datasets-and-training.md @@ -7,11 +7,13 @@ weight: 3 layout: "learningpathall" --- -We start by downloading the MNIST dataset. Proceed as follows: -1. Open, the pytorch-digits.ipynb you created in this [Learning Path](learning-paths/cross-platform/pytorch-digit-classification-architecture). +Start by downloading the MNIST dataset. Proceed as follows: + +1. Open, the pytorch-digits.ipynb you created in [Create a PyTorch model for digit classification](/learning-paths/cross-platform/pytorch-digit-classification-architecture/). + 2. Add the following statements: -```Python +```python from torchvision import transforms, datasets from torch.utils.data import DataLoader @@ -38,31 +40,33 @@ train_dataloader = DataLoader(training_data, batch_size=batch_size) test_dataloader = DataLoader(test_data, batch_size=batch_size) ``` -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. +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. 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. This setup prepares the training and test datasets for use in a machine learning model, enabling efficient data handling and model training in PyTorch. To run the above code, you will need to install certifi package: + ```console pip install certifi ``` -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: +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: ```console /Applications/Python\ 3.x/Install\ Certificates.command ``` -Make sure to replace `x` by the number of Python version you have installed. +Make sure to replace `x` with the number of Python version you have installed. After running the code you will see the output that might look like shown below: ![image](Figures/01.png) -# Training -Now, we have all the tools needed to train the model. We first specify the loss function and the optimizer: +# Train the model + +To train the model, specify the loss function and the optimizer: ```Python learning_rate = 1e-3 @@ -71,9 +75,9 @@ loss_fn = nn.CrossEntropyLoss() optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate) ``` -We use CrossEntropyLoss as the loss function and the Adam optimizer for training. The learning rate is set to 1e-3. +Use CrossEntropyLoss as the loss function and the Adam optimizer for training. The learning rate is set to 1e-3. -Next, we define the methods for training and evaluating our feedforward neural network: +Next, define the methods for training and evaluating the feedforward neural network: ```Python def train_loop(dataloader, model, loss_fn, optimizer): @@ -105,9 +109,9 @@ def test_loop(dataloader, model, loss_fn): print(f"Accuracy: {(100*correct):>0.1f}%, Avg loss: {test_loss:>8f} \n") ``` -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. +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. -We can now invoke these methods to train and evaluate the model. Similarly to TensorFlow, we use 10 epochs. +You can now invoke these methods to train and evaluate the model using 10 epochs. ```Python epochs = 10 @@ -131,20 +135,21 @@ Accuracy: 95.4%, Avg loss: 1.507491 which shows the model achieved around 95% of accuracy. -# Saving the model -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: +# Save the model + +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: ```Python torch.save(model.state_dict(), "model_weights.pth"). ``` -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. +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. Therefore, when you later want to use the saved model for inference, you will need to provide the definition of the model class. 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. -Here, we use TorchScript and save the model using the following commands: +Use TorchScript to save the model using the following commands: ```Python # Set model to evaluation mode @@ -157,15 +162,16 @@ traced_model = torch.jit.trace(model, torch.rand(1, 1, 28, 28)) traced_model.save("model.pth") ``` -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. +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. Setting the model to evaluation mode before tracing is important for several reasons: + 1. Behavior of Layers like Dropout and BatchNorm: -* 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. -* BatchNorm. During training, Batch Normalization layers use batch statistics to normalize the input. During evaluation, they use running averages calculated during training. + * 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. + * BatchNorm. During training, Batch Normalization layers use batch statistics to normalize the input. During evaluation, they use running averages calculated during training. -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. +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. 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. -In the next step, we will use the saved model for inference. \ No newline at end of file +In the next step, you will use the saved model for inference. \ No newline at end of file diff --git a/content/learning-paths/cross-platform/pytorch-digit-classification-training/inference.md b/content/learning-paths/cross-platform/pytorch-digit-classification-training/inference.md index 30b51ba8ad..5a2224f7a8 100644 --- a/content/learning-paths/cross-platform/pytorch-digit-classification-training/inference.md +++ b/content/learning-paths/cross-platform/pytorch-digit-classification-training/inference.md @@ -8,23 +8,27 @@ layout: "learningpathall" --- The inference process involves using a trained model to make predictions on new, unseen data. It typically follows these steps: -1. Load the Trained Model. The model, along with its learned parameters (weights and biases), is loaded from a saved file. -2. Prepare the Input Data. The input data is preprocessed in the same way as during training (e.g., normalization, tensor conversion) to ensure compatibility with the model. -3. Make Predictions. The preprocessed data is fed into the model, which computes the output based on its trained parameters. The output is often a probability distribution over possible classes. -4. Interpret the Results. The predicted class is usually the one with the highest probability. The results can then be used for further analysis or decision-making. + +1. **Load the Trained Model**: the model, along with its learned parameters (weights and biases), is loaded from a saved file. +2. **Prepare the Input Data**: the input data is preprocessed in the same way as during training (e.g., normalization, tensor conversion) to ensure compatibility with the model. +3. **Make Predictions**: the preprocessed data is fed into the model, which computes the output based on its trained parameters. The output is often a probability distribution over possible classes. +4. **Interpret the Results**: the predicted class is usually the one with the highest probability. The results can then be used for further analysis or decision-making. This process allows the model to generalize its learned knowledge to make accurate predictions on new data. # Runing inference in PyTorch -We will now see how to run inference in PyTorch using previously saved model. To display results we will use matplotlib. So, start by installing matplotlib package: + +You can inference in PyTorch using the previously saved model. To display results you can use matplotlib. + +Start by installing matplotlib package: ```console pip install matplotlib ``` -Then, in Visual Studio Code create a new file, pytorch-digits-inference.ipynb, and modify that file as follows: +Then, in Visual Studio Code create a new file named `pytorch-digits-inference.ipynb` and modify the file to include the code below: -```Python +```python import torch from torchvision import datasets, transforms import matplotlib.pyplot as plt @@ -79,29 +83,30 @@ plt.tight_layout() plt.show() ``` -The above code performs inference on a saved PyTorch model using 16 randomly selected images from the MNIST test dataset and displays them along with their actual and predicted labels. +The above code performs inference on the saved PyTorch model using 16 randomly selected images from the MNIST test dataset and displays them along with their actual and predicted labels. -As before, we start by importing the necessary Python libraries: torch, datasets, transforms, matplotlib.pyplot, and random. torch is used for loading the model and performing tensor operations. datasets and transforms from torchvision are used for loading and transforming the MNIST dataset. We use matplotlib.pyplot for plotting and displaying images, and random is used for selecting random images from the dataset. +As before, start by importing the necessary Python libraries: torch, datasets, transforms, matplotlib.pyplot, and random. torch is used for loading the model and performing tensor operations. datasets and transforms from torchvision are used for loading and transforming the MNIST dataset. Use matplotlib.pyplot for plotting and displaying images, and random is used for selecting random images from the dataset. -Next, we load the MNIST test dataset using datasets.MNIST() with train=False to specify that it’s the test data. The dataset is automatically downloaded if it’s not available locally. +Next, load the MNIST test dataset using datasets.MNIST() with train=False to specify that it’s the test data. The dataset is automatically downloaded if it’s not available locally. -We then load the saved model using torch.jit.load("model.pth") and set the model to evaluation mode using model.eval(). This ensures that layers like dropout and batch normalization behave appropriately during inference. +Load the saved model using torch.jit.load("model.pth") and set the model to evaluation mode using model.eval(). This ensures that layers like dropout and batch normalization behave appropriately during inference. -Subsequently, we select 16 random images and create a 4x4 grid of subplots using plt.subplots(4, 4, figsize=(12, 12)) for displaying the images. +Subsequently, select 16 random images and create a 4x4 grid of subplots using plt.subplots(4, 4, figsize=(12, 12)) for displaying the images. -Afterward, we perform inference and display the images in a loop. Specifically, for each of the 16 selected images, the image and its label are retrieved from the dataset using the random index. The image tensor is expanded to include a batch dimension (image.unsqueeze(0)) because the model expects a batch of images. Inference is performed with model(image_batch) to get the prediction. The predicted label is determined using torch.argmax() to find the index of the maximum probability in the output. Each image is displayed in its respective subplot with the actual and predicted labels. We use plt.tight_layout() to ensure that the layout is adjusted nicely, and plt.show() to display the 16 images with their actual and predicted labels. +Afterward, perform inference and display the images in a loop. Specifically, for each of the 16 selected images, the image and its label are retrieved from the dataset using the random index. The image tensor is expanded to include a batch dimension (image.unsqueeze(0)) because the model expects a batch of images. Inference is performed with model(image_batch) to get the prediction. The predicted label is determined using torch.argmax() to find the index of the maximum probability in the output. Each image is displayed in its respective subplot with the actual and predicted labels. We use plt.tight_layout() to ensure that the layout is adjusted nicely, and plt.show() to display the 16 images with their actual and predicted labels. -This code effectively demonstrates how to use a saved PyTorch model for inference and visualization of predictions on a subset of the MNIST test dataset. +This code demonstrates how to use a saved PyTorch model for inference and visualization of predictions on a subset of the MNIST test dataset. After running the code, you should see results similar to the following figure: ![image](Figures/03.png) # Summary -In this exercise, we went through the complete process of training and using a PyTorch model for digit classification on the MNIST dataset. Using the training dataset, we optimized the model’s weights and biases over multiple epochs. We employed the CrossEntropyLoss function and the Adam optimizer to minimize prediction errors and improve accuracy. We periodically evaluated the model on the test dataset to monitor its performance, ensuring it was learning effectively without overfitting. -After training, we saved the model using TorchScript, which captures both the model’s architecture and its learned parameters. This made the model portable and independent of the original class definition, simplifying deployment. +In this exercise, you went through the complete process of training and using a PyTorch model for digit classification on the MNIST dataset. Using the training dataset, you optimized the model’s weights and biases over multiple epochs. You employed the CrossEntropyLoss function and the Adam optimizer to minimize prediction errors and improve accuracy. You periodically evaluated the model on the test dataset to monitor its performance, ensuring it was learning effectively without overfitting. + +After training, you saved the model using TorchScript, which captures both the model’s architecture and its learned parameters. This made the model portable and independent of the original class definition, simplifying deployment. -Next, we moved on to inference. We loaded the saved model and set it to evaluation mode to ensure that layers like dropout and batch normalization behaved correctly during inference. We randomly selected 16 images from the MNIST test dataset to evaluate the model’s performance on unseen data. For each selected image, we used the model to predict the digit, comparing the predicted labels with the actual ones. We displayed the images alongside their actual and predicted labels in a 4x4 grid, visually assessing the model’s accuracy and performance. +Next, you performed inference. You loaded the saved model and set it to evaluation mode to ensure that layers like dropout and batch normalization behaved correctly during inference. You randomly selected 16 images from the MNIST test dataset to evaluate the model’s performance on unseen data. For each selected image, you used the model to predict the digit, comparing the predicted labels with the actual ones. You displayed the images alongside their actual and predicted labels in a 4x4 grid, visually assessing the model’s accuracy and performance. -This comprehensive process—from model training and saving to inference and visualization—illustrates the end-to-end workflow for building and deploying a machine learning model in PyTorch. It demonstrates how to train a model, save it in a portable format, and then use it to make predictions on new data. \ No newline at end of file +This comprehensive process, from model training and saving to inference and visualization, illustrates the end-to-end workflow for building and deploying a machine learning model in PyTorch. It demonstrates how to train a model, save it in a portable format, and then use it to make predictions on new data. \ No newline at end of file diff --git a/content/learning-paths/cross-platform/pytorch-digit-classification-training/intro.md b/content/learning-paths/cross-platform/pytorch-digit-classification-training/intro.md index f383e1148a..eccd982ff1 100644 --- a/content/learning-paths/cross-platform/pytorch-digit-classification-training/intro.md +++ b/content/learning-paths/cross-platform/pytorch-digit-classification-training/intro.md @@ -1,6 +1,6 @@ --- # User change -title: "Background and Installation" +title: "PyTorch model training" weight: 2 @@ -8,12 +8,20 @@ layout: "learningpathall" --- ## Background -In the [learning path](learning-paths/cross-platform/pytorch-digit-classification-architecture), we created a feedforward neural network for digit classification using the MNIST dataset. This network, however, is currently untrained and lacks the ability to make accurate predictions. To enable the network to recognize handwritten digits effectively, we need to perform training. Training in PyTorch involves configuring the network's parameters—such as weights and biases—by exposing the model to labeled data and iteratively adjusting these parameters to minimize prediction errors. This process allows the model to learn the patterns in the data, enabling it to make accurate classifications on new, unseen inputs. -The typical approach to training a neural network in PyTorch involves several key steps. First, we obtain and preprocess the dataset, which usually includes normalizing the data and converting it into a format suitable for the model. Next, the dataset is split into training and testing subsets. Training data is used to update the model’s parameters, while testing data evaluates its performance. During training, we feed batches of input data through the network, calculate the prediction error or loss using a loss function (such as cross-entropy for classification tasks), and optimize the model’s weights and biases using backpropagation. Backpropagation involves computing the gradient of the loss with respect to each parameter and then updating the parameters using an optimizer, like Stochastic Gradient Descent (SGD) or Adam. This process is repeated for multiple epochs until the model achieves satisfactory performance, balancing accuracy and generalization. +In [Create a PyTorch model for digit classification](/learning-paths/cross-platform/pytorch-digit-classification-architecture/), you created a feedforward neural network for digit classification using the MNIST dataset. The network was left untrained and lacks the ability to make accurate predictions. Make sure to create that Learning Path before starting this one. + +To enable the network to recognize handwritten digits effectively, training is needed. Training in PyTorch involves configuring the network's parameters, such as weights and biases, by exposing the model to labeled data and iteratively adjusting these parameters to minimize prediction errors. This process allows the model to learn the patterns in the data, enabling it to make accurate classifications on new, unseen inputs. + +The typical approach to training a neural network in PyTorch involves several key steps. + +First, obtain and preprocess the dataset, which usually includes normalizing the data and converting it into a format suitable for the model. + +Next, the dataset is split into training and testing subsets. Training data is used to update the model’s parameters, while testing data evaluates its performance. During training, feed batches of input data through the network, calculate the prediction error or loss using a loss function (such as cross-entropy for classification tasks), and optimize the model’s weights and biases using backpropagation. Backpropagation involves computing the gradient of the loss with respect to each parameter and then updating the parameters using an optimizer, like Stochastic Gradient Descent (SGD) or Adam. This process is repeated for multiple epochs until the model achieves satisfactory performance, balancing accuracy and generalization. ### Loss, gradients, epoch and backpropagation -Loss is a measure of how well or poorly a model’s predictions match the true labels of the data. It quantifies the difference between the predicted output and the actual output. The lower the loss, the better the model’s performance. In classification tasks, a common loss function is Cross-Entropy Loss, while Mean Squared Error (MSE) is often used for regression tasks. The goal of training is to minimize the loss, which indicates that the model’s predictions are getting closer to the actual labels. + +Loss is a measure of how well a model’s predictions match the true labels of the data. It quantifies the difference between the predicted output and the actual output. The lower the loss, the better the model’s performance. In classification tasks, a common loss function is Cross-Entropy Loss, while Mean Squared Error (MSE) is often used for regression tasks. The goal of training is to minimize the loss, which indicates that the model’s predictions are getting closer to the actual labels. Gradients represent the rate of change of the loss with respect to each of the model’s parameters (weights and biases). They are used to update the model’s parameters in the direction that reduces the loss. Gradients are calculated during the backpropagation step, where the loss is propagated backward through the network to compute how each parameter contributes to the overall loss. Optimizers like SGD or Adam use these gradients to adjust the parameters, effectively “teaching” the model to improve its predictions. @@ -21,25 +29,27 @@ An epoch refers to one complete pass through the entire training dataset. During Backpropagation is a fundamental algorithm used in training neural networks to optimize their parameters—weights and biases—by minimizing the loss function. It works by propagating the error backward through the network, calculating the gradients of the loss function with respect to each parameter, and updating these parameters accordingly. -### Training a model in PyTorch - overview +### Training a model in PyTorch + To train a model in PyTorch, several essential components are required: -1. Dataset, which is the source of data that the model will learn from. It typically consists of input samples and their corresponding labels. PyTorch provides the torchvision.datasets module for easy access to popular datasets like MNIST, CIFAR-10, and ImageNet. You can also create custom datasets using the torch.utils.data.Dataset class. -2. DataLoader is used to efficiently load and batch the data during training. It handles data shuffling, batching, and parallel loading, making it easier to feed the data into the model in a structured manner. This is crucial for performance, especially when working with large datasets. +1. **Dataset**: the source of data that the model will learn from. It typically consists of input samples and their corresponding labels. PyTorch provides the `torchvision.datasets` module for easy access to popular datasets like MNIST, CIFAR-10, and ImageNet. You can also create custom datasets using the `torch.utils.data.Dataset` class. + +2. **DataLoader**: used to efficiently load and batch the data during training. It handles data shuffling, batching, and parallel loading, making it easier to feed the data into the model in a structured manner. This is crucial for performance, especially when working with large datasets. -3. Model (Neural Network Architecture). The model defines the structure of the neural network. We already learned that in PyTorch, models are typically created by subclassing torch.nn.Module and defining the network layers and forward pass. This includes specifying the input and output dimensions and the sequence of layers, such as linear layers, activation functions, and dropout. +3. **Model**: the Neural Network Architecture defines the structure of the neural network. You learned that in PyTorch, models are typically created by subclassing `torch.nn.Module` and defining the network layers and forward pass. This includes specifying the input and output dimensions and the sequence of layers, such as linear layers, activation functions, and dropout. -4. Loss Function, which measures how far the model’s predictions are from the actual targets. It guides the optimization process by providing a signal that tells the model how to adjust its parameters. Common loss functions include Cross-Entropy Loss for classification tasks and Mean Squared Error (MSE) Loss for regression tasks. You can select a predefined loss function from torch.nn or define your own. +4. **Loss Function**: measures how far the model’s predictions are from the actual targets. It guides the optimization process by providing a signal that tells the model how to adjust its parameters. Common loss functions include Cross-Entropy Loss for classification tasks and Mean Squared Error (MSE) Loss for regression tasks. You can select a predefined loss function from torch.nn or define your own. -5. Optimizer, which updates the model’s parameters based on the gradients computed during backpropagation. It determines how the model learns from the data. Popular optimizers include Stochastic Gradient Descent (SGD) and Adam, which are available in the torch.optim module. You need to specify the learning rate (a hyperparameter that controls how much to change the parameters in response to the gradient) and other hyperparameters when creating the optimizer. +5. **Optimizer**: updates the model’s parameters based on the gradients computed during backpropagation. It determines how the model learns from the data. Popular optimizers include Stochastic Gradient Descent (SGD) and Adam, which are available in the torch.optim module. You need to specify the learning rate (a hyperparameter that controls how much to change the parameters in response to the gradient) and other hyperparameters when creating the optimizer. -6. Training Loop, which is where the actual learning happens. In each iteration of the loop: -* A batch of data is fetched from the DataLoader. -* The model performs a forward pass to generate predictions. -* The loss is calculated using the predictions and the true labels. -* The gradients are computed via backpropagation. -* The optimizer updates the model’s parameters based on the gradients. +6. **Training Loop**: where the actual learning happens. For each iteration of the loop: + * A batch of data is fetched from the DataLoader. + * The model performs a forward pass to generate predictions. + * The loss is calculated using the predictions and the true labels. + * The gradients are computed via backpropagation. + * The optimizer updates the model’s parameters based on the gradients. This process is repeated for a specified number of epochs to gradually reduce the loss and improve the model’s performance. -In the next step we will see how to perform all this using the model, we created in this [Learning Path](learning-paths/cross-platform/pytorch-digit-classification-architecture). \ No newline at end of file +In the next step you will see how to perform model training. \ No newline at end of file