Deep Learning Overview - Kuntal Chakraborty
Deep Learning Overview - Kuntal Chakraborty
Kuntal Chakraborty
Director, Data Science & Analytics
TABS Analytics by Telus
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 1/44
8/7/24, 3:54 PM 003-object-detection-dl slides
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 2/44
8/7/24, 3:54 PM 003-object-detection-dl slides
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 3/44
8/7/24, 3:54 PM 003-object-detection-dl slides
Features
Composed of layers of interconnected nodes or
neurons. The input layer receives the data, the
output layer produces the predictions and the
hidden layers perform the computations in
between.
activation functions are applied to the outputs of
each layer to introduce non-linearity and increase
the model's expressiveness. Common activation
functions are ReLU, Sigmoid and Tanh.
Backpropagation is a method for training neural
networks by iteratively adjusting the weights in
each layer to minimize the difference between the
predicted outputs and actual outputs. It Works by
propagating the error backwards from the output
layer to the input layer and adjusting the weights
accordingly.
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 4/44
8/7/24, 3:54 PM 003-object-detection-dl slides
PyTorch
PyTorch is an open-source deep learning framework
that's known for its flexibility and ease of use, This is
enabled in part by its compatibility with the popular
Python high-level programming language.
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 5/44
8/7/24, 3:54 PM 003-object-detection-dl slides
Data Preparation
"Data" in machine learning can be almost anything
you can imagine. A table of numbers (like a big excel),
images of any kind, videos, audio files, text etc.
So machine learning consists of two parts
Turn your data into numbers (a representation)
Build a model to learn the representation as best
as possible
When you have the data ready split it into training and
test
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 6/44
8/7/24, 3:54 PM 003-object-detection-dl slides
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 7/44
8/7/24, 3:54 PM 003-object-detection-dl slides
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 8/44
8/7/24, 3:54 PM 003-object-detection-dl slides
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 9/44
8/7/24, 3:54 PM 003-object-detection-dl slides
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 10/44
8/7/24, 3:54 PM 003-object-detection-dl slides
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 11/44
8/7/24, 3:54 PM 003-object-detection-dl slides
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 12/44
8/7/24, 3:54 PM 003-object-detection-dl slides
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 13/44
8/7/24, 3:54 PM 003-object-detection-dl slides
In [55]:
import numpy as np
import torch
import torchvision
import matplotlib.pyplot as plt
from time import time
from torchvision import datasets, transforms
from torch import nn, optim
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 14/44
8/7/24, 3:54 PM 003-object-detection-dl slides
In [56]:
transform = transforms.Compose([transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,))
])
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 15/44
8/7/24, 3:54 PM 003-object-detection-dl slides
In [57]:
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 16/44
8/7/24, 3:54 PM 003-object-detection-dl slides
In [58]:
input_size = 784
hidden_sizes = [128, 64]
output_size = 10
Sequential(
(0): Linear(in_features=784, out_features=128, bias=Tr
ue)
(1): ReLU()
(2): Linear(in_features=128, out_features=64, bias=Tru
e)
(3): ReLU()
(4): Linear(in_features=64, out_features=10, bias=Tru
e)
(5): LogSoftmax(dim=1)
)
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 17/44
8/7/24, 3:54 PM 003-object-detection-dl slides
In [59]:
criterion = nn.NLLLoss()
images, labels = next(iter(trainloader))
images = images.view(images.shape[0], -1)
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 18/44
8/7/24, 3:54 PM 003-object-detection-dl slides
In [60]:
# Training pass
optimizer.zero_grad()
output = model(images)
loss = criterion(output, labels)
running_loss += loss.item()
else:
print("Epoch {} - Training loss: {}".format(e, running_los
print("\nTraining Time (in minutes) =",(time()-time0)/60)
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 19/44
8/7/24, 3:54 PM 003-object-detection-dl slides
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 20/44
8/7/24, 3:54 PM 003-object-detection-dl slides
In [61]:
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 21/44
8/7/24, 3:54 PM 003-object-detection-dl slides
In [62]:
ps = torch.exp(logps)
probab = list(ps.numpy()[0])
print("Predicted Digit =", probab.index(max(probab)))
view_classify(img.view(1, 28, 28), ps)
Predicted Digit = 4
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 22/44
8/7/24, 3:54 PM 003-object-detection-dl slides
Convolutional Layers
The convolutional layers are the foundation of CNN, as
they contain the learned kernels (weights), which
extract features that distinguish different images from
one another,this is what we want for classification! As
you interact with the convolutional layer, you will
notice links between the previous layers and the
convolutional layers. Each link represents a unique
kernel, which is used for the convolution operation to
produce the current convolutional neuron’s output or
activation map.
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 23/44
8/7/24, 3:54 PM 003-object-detection-dl slides
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 24/44
8/7/24, 3:54 PM 003-object-detection-dl slides
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 25/44
8/7/24, 3:54 PM 003-object-detection-dl slides
In [63]:
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 26/44
8/7/24, 3:54 PM 003-object-detection-dl slides
In [64]:
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
self.conv2_drop = nn.Dropout2d()
self.fc1 = nn.Linear(320, 50)
self.fc2 = nn.Linear(50, 10)
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 27/44
8/7/24, 3:54 PM 003-object-detection-dl slides
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 28/44
8/7/24, 3:54 PM 003-object-detection-dl slides
InceptionNet
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 29/44
8/7/24, 3:54 PM 003-object-detection-dl slides
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 30/44
8/7/24, 3:54 PM 003-object-detection-dl slides
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 31/44
8/7/24, 3:54 PM 003-object-detection-dl slides
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 32/44
8/7/24, 3:54 PM 003-object-detection-dl slides
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 33/44
8/7/24, 3:54 PM 003-object-detection-dl slides
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 34/44
8/7/24, 3:54 PM 003-object-detection-dl slides
In [65]:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import torch
import torch.nn as nn
from torch.autograd import Variable
from sklearn.preprocessing import MinMaxScaler
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 35/44
8/7/24, 3:54 PM 003-object-detection-dl slides
In [66]:
training_set = pd.read_csv('data/airline-passengers.csv')
training_set = training_set.iloc[:,1:2].values
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 36/44
8/7/24, 3:54 PM 003-object-detection-dl slides
In [67]:
for i in range(len(data)-seq_length-1):
_x = data[i:(i+seq_length)]
_y = data[i+seq_length]
x.append(_x)
y.append(_y)
return np.array(x),np.array(y)
sc = MinMaxScaler()
training_data = sc.fit_transform(training_set)
seq_length = 4
x, y = sliding_windows(training_data, seq_length)
dataX = Variable(torch.Tensor(np.array(x)))
dataY = Variable(torch.Tensor(np.array(y)))
trainX = Variable(torch.Tensor(np.array(x[0:train_size])))
trainY = Variable(torch.Tensor(np.array(y[0:train_size])))
testX = Variable(torch.Tensor(np.array(x[train_size:len(x)])))
testY = Variable(torch.Tensor(np.array(y[train_size:len(y)])))
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 37/44
8/7/24, 3:54 PM 003-object-detection-dl slides
In [68]:
class LSTM(nn.Module):
self.num_classes = num_classes
self.num_layers = num_layers
self.input_size = input_size
self.hidden_size = hidden_size
self.seq_length = seq_length
c_0 = Variable(torch.zeros(
self.num_layers, x.size(0), self.hidden_size))
out = self.fc(h_out)
return out
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 38/44
8/7/24, 3:54 PM 003-object-detection-dl slides
In [69]:
num_epochs = 2000
learning_rate = 0.01
input_size = 1
hidden_size = 2
num_layers = 1
num_classes = 1
loss.backward()
optimizer.step()
if epoch % 100 == 0:
print("Epoch: %d, loss: %1.5f" % (epoch, loss.item()))
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 39/44
8/7/24, 3:54 PM 003-object-detection-dl slides
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 40/44
8/7/24, 3:54 PM 003-object-detection-dl slides
In [ ]:
lstm.eval()
train_predict = lstm(dataX)
data_predict = train_predict.data.numpy()
dataY_plot = dataY.data.numpy()
data_predict = sc.inverse_transform(data_predict)
dataY_plot = sc.inverse_transform(dataY_plot)
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 41/44
8/7/24, 3:54 PM 003-object-detection-dl slides
In [70]:
plt.plot(dataY_plot)
plt.plot(data_predict)
plt.suptitle('Time-Series Prediction')
plt.show()
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 42/44
8/7/24, 3:54 PM 003-object-detection-dl slides
Challenges
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 43/44
8/7/24, 3:54 PM 003-object-detection-dl slides
Speaker notes
127.0.0.1:8000/003-object-detection-dl.slides.html#/ 44/44