Chapter 25
01. PyTorch Workflow Exercise Template
NotebookPython 321 cells
01. PyTorch Workflow Exercise Template
The following is a template for the PyTorch workflow exercises.
It's only starter code and it's your job to fill in the blanks.
Because of the flexibility of PyTorch, there may be more than one way to answer the question.
Don't worry about trying to be right just try writing code that suffices the question.
You can see one form of solutions on GitHub (but try the exercises below yourself first!).
In [ ]python · cell 3
python
# Import necessary librariesIn [ ]python · cell 4
python
# Setup device-agnostic code1. Create a straight line dataset using the linear regression formula (weight * X + bias).
- Set
weight=0.3andbias=0.9there should be at least 100 datapoints total. - Split the data into 80% training, 20% testing.
- Plot the training and testing data so it becomes visual.
Your output of the below cell should look something like:
code
Number of X samples: 100
Number of y samples: 100
First 10 X & y samples:
X: tensor([0.0000, 0.0100, 0.0200, 0.0300, 0.0400, 0.0500, 0.0600, 0.0700, 0.0800,
0.0900])
y: tensor([0.9000, 0.9030, 0.9060, 0.9090, 0.9120, 0.9150, 0.9180, 0.9210, 0.9240,
0.9270])Of course the numbers in X and y may be different but ideally they're created using the linear regression formula.
In [ ]python · cell 6
python
# Create the data parameters
# Make X and y using linear regression feature
print(f"Number of X samples: {len(X)}")
print(f"Number of y samples: {len(y)}")
print(f"First 10 X & y samples:\nX: {X[:10]}\ny: {y[:10]}")In [ ]python · cell 7
python
# Split the data into training and testingIn [ ]python · cell 8
python
# Plot the training and testing data 2. Build a PyTorch model by subclassing nn.Module.
- Inside should be a randomly initialized
nn.Parameter()withrequires_grad=True, one forweightsand one forbias. - Implement the
forward()method to compute the linear regression function you used to create the dataset in 1. - Once you've constructed the model, make an instance of it and check its
state_dict(). - Note: If you'd like to use
nn.Linear()instead ofnn.Parameter()you can.
In [ ]python · cell 10
python
# Create PyTorch linear regression model by subclassing nn.ModuleIn [ ]python · cell 11
python
# Instantiate the model and put it to the target device3. Create a loss function and optimizer using nn.L1Loss() and torch.optim.SGD(params, lr) respectively.
- Set the learning rate of the optimizer to be 0.01 and the parameters to optimize should be the model parameters from the model you created in 2.
- Write a training loop to perform the appropriate training steps for 300 epochs.
- The training loop should test the model on the test dataset every 20 epochs.
In [ ]python · cell 13
python
# Create the loss function and optimizerIn [ ]python · cell 14
python
# Training loop
# Train model for 300 epochs
# Send data to target device
for epoch in range(epochs):
### Training
# Put model in train mode
# 1. Forward pass
# 2. Calculate loss
# 3. Zero gradients
# 4. Backpropagation
# 5. Step the optimizer
### Perform testing every 20 epochs
if epoch % 20 == 0:
# Put model in evaluation mode and setup inference context
# 1. Forward pass
# 2. Calculate test loss
# Print out what's happening
print(f"Epoch: {epoch} | Train loss: {loss:.3f} | Test loss: {test_loss:.3f}")4. Make predictions with the trained model on the test data.
- Visualize these predictions against the original training and testing data (note: you may need to make sure the predictions are not on the GPU if you want to use non-CUDA-enabled libraries such as matplotlib to plot).
In [ ]python · cell 16
python
# Make predictions with the modelIn [ ]python · cell 17
python
# Plot the predictions (these may need to be on a specific device)5. Save your trained model's state_dict() to file.
- Create a new instance of your model class you made in 2. and load in the
state_dict()you just saved to it. - Perform predictions on your test data with the loaded model and confirm they match the original model predictions from 4.
In [ ]python · cell 19
python
from pathlib import Path
# 1. Create models directory
# 2. Create model save path
# 3. Save the model state dictIn [ ]python · cell 20
python
# Create new instance of model and load saved state dict (make sure to put it on the target device)In [ ]python · cell 21
python
# Make predictions with loaded model and compare them to the previous