Note
•
Packages to import
# Things to import first
import torch # PyTorch
import numpy as np # NumPy
import torch.nn as nn # neural network models
from torch.utils.data import TensorDataset # dataset
from torch.utils.data import DataLoader # dataloader
import torch.nn.functional as F # functions
Python
복사
•
Dataset and DataLoader: here
•
can add ? before the nn to view a documentation (ex. ?nn.Linear)
•
Composed of …
◦
Import: nn, TensorDataset, DataLoader, F
◦
Define and load data: TensorDataset, DataLoader
◦
Define model: nn.
◦
Define loss function: F.
◦
Define optimizer: torch.optim.
◦
Build training function: here
1.
Generate predictions
2.
Calculate loss
3.
Compute gradients
4.
Update parameters using gradients
5.
Reset the gradients to zero
◦
Train model
Initial Import
# Import Built-in Neural Networks
import torch.nn as nn
Python
복사
Inputs and Targets Arrays
# Input (temp, rainfall, humidity)
inputs = np.array([[73, 67, 43],
[91, 88, 64],
[87, 134, 58],
[102, 43, 37],
[69, 96, 70],
[74, 66, 43],
[91, 87, 65],
[88, 134, 59],
[101, 44, 37],
[68, 96, 71],
[73, 66, 44],
[92, 87, 64],
[87, 135, 57],
[103, 43, 36],
[68, 97, 70]],
dtype='float32')
# Targets (apples, oranges)
targets = np.array([[56, 70],
[81, 101],
[119, 133],
[22, 37],
[103, 119],
[57, 69],
[80, 102],
[118, 132],
[21, 38],
[104, 118],
[57, 69],
[82, 100],
[118, 134],
[20, 38],
[102, 120]],
dtype='float32')
inputs = torch.from_numpy(inputs)
targets = torch.from_numpy(targets)
Python
복사
Dataset and DataLoader
# Import Utility package
from torch.utils.data import TensorDataset #(1)
from torch.utils.data import DataLoader #(2)
# Define dataset
train_ds = TensorDataset(inputs, targets)
train_ds[0:3] #(3)
# Define data loader
batch_size = 5
train_dl = DataLoader(train_ds, batch_size, shuffle=True)
# Sample for loop
for inputBatch, targetBatch in train_dl:
Python
복사
#(1) TensorDataset allows access to rows from inputs and targets as tuples, and provides standard APIs for working with many different types of datasets in PyTorch
#(2) DataLoader allows to split the data into batches of a predefined size while training with other utilities such as shuffling and random sampling.
#(3) TensorDataset allows to access a small section of the training data using indexing notation, the first returned tensor is the inputs whereas second returned tensor is the targets
nn.Linear
# Define Model
model = nn.Linear(3,2) #(1)
# Access weights and biases
model.weigth
model.bias
# Parameters
list(model.parameters())
# Generate predictions
preds = model(inputs)
Python
복사
#(1) By creating model using nn.Linear(input, output), it generates weights and biases automatically.
Loss Function
# Import nn.functional
import torch.nn.functional as F # functions
# Define loss function
loss_fn = F.mse_loss
# Compute loss
loss = loss_fn(model(inputs), targets) #(1)
Python
복사
#(1) loss_fn(prediction, targets)
Optimizer
# Define Optimizer
opt = torch.optim.SGD(model.parameters(), lr=1e-5) #(1)
Python
복사
#(1) SGD stands for “Stochastic Gradient Descent,” meaning the samples are selected in random batches instead of as a single group. By specifying lr, we can control the rate of which the parameters are modified.
Train the Model
# Utility function to train the model
def fit(num_epochs, model, loss_fn, opt, train_dl):
# Repeat for given number of epochs
for epoch in range(num_epochs):
# Train with batches of data
for inputBatch, targetBatch in train_dl:
# 1. Generate predictions
pred = model(inputBatch)
# 2. Calculate loss
loss = loss_fn(pred, targetBatch)
# 3. Compute gradients
loss.backward()
# 4. Update parameters using gradients
opt.step()
# 5. Reset the gradients to zero
opt.zero_grad()
# Print the progress
if (epoch_1) % 10 == 0:
print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item().4f}')
# Train model
fit(100, model, loss_fn, opt, train_dl)
Python
복사
View Result
# Generate predictions
preds = model(inputs)
# Compare it with targets
print(preds)
print(targets)
Python
복사
Final Code
'''Import'''
import torch # PyTorch
import numpy as np # NumPy
import torch.nn as nn # neural network models
from torch.utils.data import TensorDataset # dataset
from torch.utils.data import DataLoader # dataloader
import torch.nn.functional as F # functions
'''Define and Load Data'''
# Input (temp, rainfall, humidity)
inputs = np.array([[73, 67, 43],
[91, 88, 64],
[87, 134, 58],
[102, 43, 37],
[69, 96, 70],
[74, 66, 43],
[91, 87, 65],
[88, 134, 59],
[101, 44, 37],
[68, 96, 71],
[73, 66, 44],
[92, 87, 64],
[87, 135, 57],
[103, 43, 36],
[68, 97, 70]],
dtype='float32')
# Targets (apples, oranges)
targets = np.array([[56, 70],
[81, 101],
[119, 133],
[22, 37],
[103, 119],
[57, 69],
[80, 102],
[118, 132],
[21, 38],
[104, 118],
[57, 69],
[82, 100],
[118, 134],
[20, 38],
[102, 120]],
dtype='float32')
inputs = torch.from_numpy(inputs)
targets = torch.from_numpy(targets)
# Define dataset
train_ds = TensorDataset(inputs, targets)
# Define data loader
batch_size = 5 #(1)
train_dl = DataLoader(train_ds, batch_size, shuffle=True)
'''Define Model'''
model = nn.Linear(3,2) #(1)
'''Define Loss Function'''
loss_fn = F.mse_loss
'''Define Optimizer'''
opt = torch.optim.SGD(model.parameters(), lr=1e-5) #(1)
'''Build training function'''
# Utility function to train the model
def fit(num_epochs, model, loss_fn, opt, train_dl):
# Repeat for given number of epochs
for epoch in range(num_epochs):
# Train with batches of data
for inputBatch, targetBatch in train_dl:
# 1. Generate predictions
pred = model(inputBatch)
# 2. Calculate loss
loss = loss_fn(pred, targetBatch)
# 3. Compute gradients
loss.backward()
# 4. Update parameters using gradients
opt.step()
# 5. Reset the gradients to zero
opt.zero_grad()
# Print the progress
if (epoch+1) % 10 == 0:
print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}')
'''Train Model'''
fit(100, model, loss_fn, opt, train_dl) #(2)
'''View Result'''
# Generate predictions
preds = model(inputs)
# Compare it with targets
print(preds)
print(targets)
Python
복사
#(1) Can change batch_size for the data loader
#(2) Can change num_epochs for the model training

