#Exercise: Build and Train a Simple Neural Network
Objective: Create a simple neural network using PyTorch, train it on a dataset, and evaluate its performance.
Instructions:
- Define a Neural Network Model: Create a neural network with one hidden layer.
- Load a Dataset: Use the MNIST dataset for training and evaluation.
- Define a Loss Function and Optimizer: Choose appropriate loss function and optimizer for the task.
- Train the Model: Implement a training loop to train the model on the MNIST dataset.
- Evaluate the Model: Test the model on a test set and print the accuracy.
Explanation:
- Define the Neural Network Model: The
SimpleNN
class has two fully connected layers. Theforward
method processes input data through these layers. - Load the MNIST Dataset: The dataset is transformed into tensors and normalized. DataLoaders are created for training and testing.
- Define Loss Function and Optimizer: CrossEntropyLoss is used for classification, and Adam optimizer updates the model's parameters.
- Train the Model: The training loop processes the dataset in batches, computes the loss, performs backpropagation, and updates the model's weights.
- Evaluate the Model: The model's performance is evaluated on the test dataset, and accuracy is printed.