"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;

March 27, 2019

Day #226 - Pytorch Sessions (17-19)

Key Lessons
Class Basic Example

#Class name
class Lizard:
#Constructor
#Called for New Instance
def __init__(self,name):
self.name = name
#custom method
def set_name(self,name):
self.name = name
lizard = Lizard('deep')
print(lizard.name)
#object.function
lizard.set_name('lizard')
#Object.attribute name
print(lizard.name)
NN code
#parameters - Inside Funtion Definition
#Arguments - Acutal Value passed
#Build CNN
#Kernel Channel - Set Size of filter, Convolutional Kernel/Filter
#Out Channel - Set the number of filters / Feature Maps
#Out Features - Last Linear Layer
class Network:
def __init__(self):
self.layer = None
def forward(self,t):
t = self.layer(t)
return t
import torch.nn as nn
class Network(nn.Module):
def __init__(self):
super(Network,self).__init__()
#Convolutional Layers
#in_channels Number of Color Channels
self.conv1 = nn.Conv2d(in_channels=1,out_channels=6,kernel_size=5)
self.conv2 = nn.Conv2d(in_channels=6,out_channels=12,kernel_size=5)
#fully connected
#Linear Layers
#12 Output Channels
#4 * 4 -
self.fc1 = nn.Linear(in_features=12*4*4,out_features=120)
#Expand features
self.fc2 = nn.Linear(in_features=120,out_features=60)
#Output Layer
#Shrink Layers
#out_features Number of Classes in Training Set
self.out = nn.Linear(in_features=60,out_features=10)
def forward(self,t):
t = self.layer(t)
return t
#Override
def __repr__(self):
return "lizardnet"
network = Network()
#Check weights
#Values are Learnable Parameters
#Trained to minimise loss function
print(network.conv1)
print(network.conv1.weight)
print(network.conv1.weight.shape)
print(network.conv2)
print(network.conv2.weight)
print(network.conv2.weight.shape)
print(network.fc1)
print(network.fc1.weight)
print(network.fc1.weight.shape)
#Height - Output Feature
#Width - Input Feature
print(network)
for param in network.parameters():
print(param.shape)
for name,param in network.named_parameters():
print(name,'\t\t',param.shape)

Multiplication
import torch
import torchvision
import torchvision.transforms as transforms
in_features = torch.tensor([1,2,3,4],dtype=torch.float32)
weight_matrix = torch.tensor([[1,2,3,4],[2,3,4,5],[3,4,5,6]],dtype=torch.float32)
#matrix multiplication
print(weight_matrix.matmul(in_features))

Happy Mastering DL!!!!



No comments: