%matplotlib inline
import torch
import torch.nn as nn
# Warmup:
# We will explore how nn.Linear layers work by passing it a
# random tensor as input (tensor with random values)
# Predicting the size of the output is a useful exercise to see
# if we really understand what the layer is doing
layer = nn.Linear(in_features=100, out_features=30)
x = torch.randn(1, 100) # tensor consisting of random numbers
x.shape
y.shape # [1, 30]
x = torch.randn(5, 100) # changed 1 => 5
y = layer(x)
y.shape
x = torch.randn(5, 90) # changed 100 => 90
y = layer(x)
y.shape
conv = nn.Conv2d(in_channels=3,
out_channels=7,
kernel_size=5)
conv_paramters = list(conv.parameters())
len(conv_paramters)
conv_paramters[0].shape # kernel
conv_paramters[1].shape # bias
x = torch.randn(1, 3, 128, 128) # 128 pixel x 128 pixle coloured image
# batch size = 1
# This format is called the NCHW format
# N = number
# C = channel
# H = height
# W = width
conv = nn.Conv2d(in_channels=3,
out_channels=7,
kernel_size=5)
y = conv(x)
y.shape
x = torch.randn(16, 3, 128, 128) # change 1 => 16
y = conv(x)
y.shape
# add padding
conv = nn.Conv2d(in_channels=3,
out_channels=7,
kernel_size=5,
padding=1)
x = torch.randn(16, 3, 128, 128) # change 1 => 16
y = conv(x)
y.shape
# add padding
conv = nn.Conv2d(in_channels=3,
out_channels=7,
kernel_size=5,
padding=25)
x = torch.randn(16, 3, 128, 128) # change 1 => 16
y = conv(x)
y.shape
# add padding 3
conv = nn.Conv2d(in_channels=3,
out_channels=7,
kernel_size=5,
padding=3)
x = torch.randn(16, 3, 128, 128) # change 1 => 16
y = conv(x)
y.shape
# add padding 2
conv = nn.Conv2d(in_channels=3,
out_channels=7,
kernel_size=5,
padding=2)
x = torch.randn(16, 3, 128, 128) # change 1 => 16
y = conv(x)
y.shape
print("input:", x.shape)
print("output:", y.shape)
# make kernel_size bigger doesn't change the size of output
conv = nn.Conv2d(in_channels=3,
out_channels=7,
kernel_size=7,
padding=3)
x = torch.randn(16, 3, 128, 128) # change 1 => 16
y = conv(x)
print("input:", x.shape)
print("output:", y.shape)
pool_layer = nn.MaxPool2d(2, 2)
y_pooled = pool_layer(torch.relu(y))
y_pooled.shape
# batch size unchanged
# number of channels unchanged
# h/w divided by 2
# set stride = 2
conv = nn.Conv2d(in_channels=3,
out_channels=7,
kernel_size=7,
padding=3,
stride=2) # added stride
x = torch.randn(16, 3, 128, 128)
y = conv(x)
print("input:", x.shape)
print("output:", y.shape)
# alexnet
import torchvision.models
alexNet = torchvision.models.alexnet(pretrained=True)
alexNet
alexNet.features(x).shape
# lenet verification
# add padding
conv = nn.Conv2d(in_channels=1,
out_channels=1,
kernel_size=5,
padding=0)
x = torch.randn(1, 1, 32, 32)
y = conv(x)
y.shape