Quick Pytorch
Posted on June 2, 2019
Tags: machinelearning
https://towardsdatascience.com/pytorch-how-and-when-to-use-module-sequential-modulelist-and-moduledict-7a54597b5f17
ML Simplified: \[Data Input \overset{linear}{\Rightarrow} s \overset{non-linear}{\Rightarrow} z\]
- Hidden layer represents activation function
- Edges between layers represent weights
- A 0-hidden layer is simply linear regression
- Input layer —weight— Output layer
- x —multiplyWeight— y
Input layer --weight-- Hidden layer(Activation) --weight-- Output layer
1 Overview
- Download
x::datasets
- Load data
DataLoader(x::datasets)
- Define forward model `class myNNforward(nn.Module)
- Create the layers in forward model
nn.Sequential(nn.Linear(22,30),nn.ReLU())
- Define loss function and optimisation function
- Create backprop train function using loss and optimisation function
2 imports
import torch
from torch import nn
from torch.utils.data import DataLoader
from torchvision import datasets
from torchvision.transforms import ToTensor, Lambda, Compose
import matplotlib.pyplot as plt
3 Basics
4 Example
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
= pd.read_csv("../dataset/Boston.csv")
Boston = Boston['lstat']
x = Boston['medv']
y
= lambda g: g.to_numpy().reshape(-1,1).astype('float32') #convert to one column ndarray
Convert_NP = Convert_NP(x) #shape (560,1)
X = Convert_NP(y) #shape (560,1)
Y
= X #USED FOR PLOTTING PURPOSES
X_NoBias
= np.ones((X.shape[0],1)).astype('float32') #column of 1's represent intercept coefficient
bias = np.append(X,bias,axis=1) #shape (560,2)
X_bias = X_bias
X print(f"X-dim: {X.shape}\nY-dim: {Y.shape}")
= torch.from_numpy(X)
Xtensor = torch.from_numpy(Y)
Ytensor # print(X[:,[1]])
= torch.from_numpy(X[:,[0]])
Xlayer = torch.from_numpy(X[:,[1]])
hidlayer = torch.from_numpy(Y) Ytensor
import random
import torch
from torch import nn, optim
import math
from IPython import display
= torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
device = 1
seed
random.seed(seed)
torch.manual_seed(seed)
= 1
inputDim = 1
outputDim = 100
hiddenDim
= 1e-4
learning_rate = 1e-6
lambda_l2
= nn.Sequential(
model
nn.Linear(inputDim, hiddenDim),
nn.Linear(hiddenDim, outputDim)
)# Convert to CUDA
model.to(device)
# nn package also has different loss functions.
# we use MSE loss for our regression task
= torch.nn.MSELoss()
criterion
# we use the optim package to apply
# stochastic gradient descent for our parameter updates
= torch.optim.SGD(model.parameters(), lr=learning_rate, weight_decay=lambda_l2) # built-in L2
optimizer
# Training
for t in range(1000):
# Feed forward to get the logits
= model(Xlayer)
y_pred
# Compute the loss (MSE)
= criterion(y_pred, Ytensor)
loss print("[EPOCH]: %i, [LOSS or MSE]: %.6f" % (t, loss.item()))
=True)
display.clear_output(wait
# zero the gradients before running
# the backward pass.
optimizer.zero_grad()
# Backward pass to compute the gradient
# of loss w.r.t our learnable params.
loss.backward()
# Update params
optimizer.step()
plt.scatter(Xlayer.data.cpu().numpy(), Ytensor.data.cpu().numpy())'r-', lw=5)
plt.plot(Xlayer.data.cpu().numpy(), y_pred.data.cpu().numpy(), 'equal'); plt.axis(
5 Other
5.1 download datasets
# Download training data from open datasets.
= datasets.FashionMNIST(
training_data ="data",
root=True,
train=True,
download=ToTensor(),
transform
)
# Download test data from open datasets.
= datasets.FashionMNIST(
test_data ="data",
root=False,
train=True,
download=ToTensor(),
transform )
5.2 load data
# Download training data from open datasets.
= datasets.FashionMNIST(
training_data ="data",
root=True,
train=True,
download=ToTensor(),
transform
)
# Download test data from open datasets.
= datasets.FashionMNIST(
test_data ="data",
root=False,
train=True,
download=ToTensor(),
transform )
5.3 forward model class
- The init is where we define our weights and activation functions
- The forward() is where we actually use these weights and activation functions.
# Get cpu or gpu device for training.
= "cuda" if torch.cuda.is_available() else "cpu"
device print(f"Using {device} device")
# Define model
class NeuralNetwork(nn.Module):
def __init__(self):
super(NeuralNetwork, self).__init__()
self.flatten = nn.Flatten()
self.linear_relu_stack = nn.Sequential(
28*28, 512),
nn.Linear(
nn.ReLU(),512, 512),
nn.Linear(
nn.ReLU(),512, 10)
nn.Linear(
)
def forward(self, x):
= self.flatten(x)
x = self.linear_relu_stack(x)
logits return logits
= NeuralNetwork().to(device)
model print(model)
5.4 loss and optimisation function
= nn.CrossEntropyLoss()
loss_fn = torch.optim.SGD(model.parameters(), lr=1e-3) optimizer
5.5 backprop training
def train(dataloader, model, loss_fn, optimizer):
= len(dataloader.dataset)
size
model.train()for batch, (X, y) in enumerate(dataloader):
= X.to(device), y.to(device)
X, y
# Compute prediction error
= model(X)
pred = loss_fn(pred, y)
loss
# Backpropagation
optimizer.zero_grad()
loss.backward()
optimizer.step()
if batch % 100 == 0:
= loss.item(), batch * len(X)
loss, current print(f"loss: {loss:>7f} [{current:>5d}/{size:>5d}]")
6 Displaying image
Either
* display image from datasets
* display image through iter(Dataloader(...))