0% found this document useful (0 votes)
8 views22 pages

02 Pytorch Classification

The document outlines the fundamentals of neural network classification, including binary and multiclass classification problems, and the architecture of classification models. It covers steps for modeling with PyTorch, including data preparation, model evaluation, and common metrics for assessing model performance. Additionally, it emphasizes the importance of visualization and experimentation in the machine learning process.

Uploaded by

goxemi7367
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views22 pages

02 Pytorch Classification

The document outlines the fundamentals of neural network classification, including binary and multiclass classification problems, and the architecture of classification models. It covers steps for modeling with PyTorch, including data preparation, model evaluation, and common metrics for assessing model performance. Additionally, it emphasizes the importance of visualization and experimentation in the machine learning process.

Uploaded by

goxemi7367
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Neural Network Classification with

Where can you get help?


“If in doubt, run the code”

• Follow along with the code


• Try it for yourself
• Press SHIFT + CMD + SPACE to read the docstring
• Search for it
• Try again
• Ask

[Link]
“What is a classification
problem?”
Example classification problems
“Is this email spam or not spam?” “Is this a photo of sushi, steak or pizza?”
To: daniel@[Link] To: daniel@[Link]
Hey Daniel, Hay daniel…

This deep learning course is incredible! C0ongratu1ations! U win $1139239230


I can’t wait to use what I’ve learned!

Not spam Spam

Binary classi cation Multiclass classi cation


g o r a n o t h e r ) (more than one thing or
(one thin another)
“What tags should this article have?”

Machine learning
Representation learning
Arti cial intelligence

(multiple label options per


sample)

Multilabel classi cation


fi
fi
fi
fi
Binary vs. Multi-class Classification

Binary classi cation


(one thing or another)

Multiclass classi cation


(more than one thing or
another)
fi
fi
What we’re going to cover
(broadly)
• Architecture of a neural network classi cation model

• Input shapes and output shapes of a classi cation model (features and labels)

• Creating custom data to view, t on and predict on

• Steps in modelling

• Creating a model, setting a loss function and optimiser, creating a training loop, evaluating a
model

• Saving and loading models

• Harnessing the power of non-linearity

• Di erent classi cation evaluation methods

👩🍳 👩🔬
(we’ll be cooking up lots of code!)

How:
ff
fi
fi
fi
fi
Classification inputs and outputs
224

W = 224 224 Sushi 🍣


H = 224 Steak 🥩
C=3 Pizza 🍕
(c = colour channels, R, G, B) Actual output

🍣 🥩 🍕
[[0.31, 0.62, 0.44…], [[0.97, 0.00, 0.03],
[0.92, 0.03, 0.27…], [0.81, 0.14, 0.05],
[0.25, 0.78, 0.07…], [0.03, 0.07, 0.90],
…, (normalized pixel valu …,
es)
Numerical
Predicted output
encoding (often already ex
ists, if not,
you can build on (comes from looking at lots
e) of these)
Input and output shapes
(for an image classification example)

224
[[0.31, 0.62, 0.44…], 🍣 🥩 🍕
224 [0.92, 0.03, 0.27…], [0.97, 0.00, 0.03]
[0.25, 0.78, 0.07…], i o n p r ob ab i l i t i e s )
(predict
…,

(gets represented as a tens


or)
[batch_size, colour_channels, width, height] Shape = [3]
Shape = [None, 3, 224, 224]
or
Shape = [32, 3, 224, 224] These will vary depending on the
(32 is a v e ry c o m m o n b a t c h problem you’re working on.
size)
(typical)

Architecture of a classification
model (we’re going to be buildin
lots of these)
g

Sushi 🍣
Steak 🥩
Pizza 🍕
Let’s code!
[Link]
[Link]
[Link] torchmetrics

[Link] [Link] [Link]


[Link]
[Link]

See more: [Link]


Improving a model (from a model’s perspective)

Smaller model

Common ways to improve a deep model:


• Adding layers
• Increase the number of hidden units
• Change/add activation functions Larger model
• Change the optimization function
• Change the learning rate (because you can alter each of

these, they’re hyperparameters)
Fitting for longer
The missing piece: Non-linearity
🤔 “What could you draw if you had an
unlimited amount of straight (linear) and non-
straight (non-linear) lines?”

Linear data Non-linear data


(possible to model with straight lines) (not possible to model with straight lines)
The missing piece: Non-linearity
A = [Link](-10, 10)

[Link](A) [Link]([Link](A)) [Link]([Link](A))

Linear activation Sigmoid activation ReLU activation


(same as original values) (non-linear) (non-linear)
The machine learning explorer’s
motto
“Visualize, visualize, visualize”
Data

Model It’s a good idea to visualize


these as often as possible.

Training

Predictions
The machine learning practitioner’s
motto

“Experiment, experiment, experiment”

👩🍳 👩🔬
(try lots of things an
d see what
tastes good)
Steps in modelling with PyTorch
1. Construct or import a pretrained model relevant to your
problem
2. Prepare the loss function, optimizer and training loop

• Loss — how wrong your model’s predictions are compared to


the truth labels (you want to minimise this).

• Optimizer — how your model should update its internal


patterns to better its predictions.
3. Fit the model to the training data so it can discover patterns

• Epochs — how many times the model will go through all of


the training examples.
4. Evaluate the model on the test data (how reliable are our
model’s predictions?)
(some common)

Classification evaluation methods


Key: tp = True Positive, tn = True Negative, fp = False Positive, fn = False Negative

Metric Name Metric Forumla Code When to use

tp + tn [Link]() Default metric for classi cation


Accuracy Accuracy = or problems. Not the best for
tp + tn + fp + fn [Link].accuracy_score() imbalanced classes.

tp [Link]() Higher precision leads to less false


Precision Precision = or
tp + fp [Link].precision_score() positives.

tp [Link]() Higher recall leads to less false


Recall Recall = or
tp + fn [Link].recall_score() negatives.

precision ⋅ recall torchmetrics.F1Score() Combination of precision and recall,


F1-score F1-score = 2 ⋅ or usually a good overall metric for a
precision + recall [Link].f1_score() classi cation model.

When comparing predictions to truth


labels to see where model gets
Confusion matrix NA [Link]()
confused. Can be hard to use with
large numbers of classes.
fi
fi
Anatomy of a confusion matrix
Confusion Matrix
False positives

0 99 (98.0%) 2 (2.0%) • True positive = model predicts 1 when truth is 1


• True negative = model predicts 0 when truth is 0
True Label

• False positive = model predicts 1 when truth is 0


• False negative = model predicts 0 when truth is 1
1 0 (0.0%) 99 (100.0%)

Correct predictions
t i v e s 0 1 (true positives,
e g a
False n Predicted Label true negatives)
Three datasets (possibly the most important
concept in machine learning…)
r n s f r o m h e r e
rns p a tt e
Model lea

Course materials Practice exam Final exam


(training set) (validation set) (test set)
Tune model patterns See if the model is ready for the
wild

Generalization
The ability for a machine learning model to perform well on data it hasn’t seen
before.
2. Show examples
i t h ra n d om
i t i a l i s e w g )
1. In a t b e g i nn i n
g h ts ( o n l y
we i
[[0.092, 0.210, 0.415],
[0.778, 0.929, 0.030],
[0.019, 0.182, 0.555],
…,

[[116, 78, 15], [[0.983, 0.004, 0.013], Coat,


[117, 43, 96], [0.110, 0.889, 0.001], Ankle boot,
Shirt,
[125, 87, 23], [0.023, 0.027, 0.985], Sandal
…, …,
3. Update representation
4. Repeat with more outputs (weights & biases)
examples

Learns
Numerical representation Representation
Inputs Outputs
encoding (patterns/features/weights) outputs
This course Resources
Course materials Course Q&A Course online book

[Link]
[Link] [Link]
discussions

PyTorch website &


forums

All things PyTorch

You might also like