Lecture 2
Lecture 2
- K-Nearest Neighbor
- Linear classifiers: SVM, Softmax
- Two-layer neural network
- Image features
Contact us on Ed, each project team will have a TA assigned to them for future
questions
your assigned TA for initial guidance (Canvas -> People -> Groups)
Use Google Form to find project partners (will be posted later today)
“Is X a valid project for 231n?” --- Ed private post / TA Office Hours
Today:
● The image classification task
● Two basic data-driven approaches to image classification
○ K-nearest neighbor and linear classifier
cat
This image is CC0 1.0 public domain This image is CC0 1.0 public domain This image is CC0 1.0 public domain This image is CC0 1.0 public domain
This image is CC0 1.0 public domain This image is CC0 1.0 public domain
This image by Umberto Salvagnin is This image by sare bear is This image by Tom Thai is licensed
This image by Umberto Salvagnin is
licensed under CC-BY 2.0 licensed under CC-BY 2.0 under CC-BY 2.0
licensed under CC-BY 2.0
?
John Canny, “A Computational Approach to Edge Detection”, IEEE TPAMI 1986
query data
Distance Metric
L1 distance:
add
A good implementation:
https://github.com/facebookresearch/faiss
1-nearest neighbor
Fei-Fei Li, Ehsan Adeli Lecture 2 - 29 April 4, 2023
K-Nearest Neighbors
Instead of copying label from nearest neighbor,
take majority vote from K closest points
K=1 K=1
http://vision.stanford.edu/teaching/cs231n-demos/knn/
Very problem/dataset-dependent.
Must try them all out and see what works best.
train
train
train
train
Never do this!
train
Useful for small datasets, but not used too frequently in deep learning
Fei-Fei Li, Ehsan Adeli Lecture 2 - 40 April 4, 2023
Example Dataset: CIFAR10
10 classes
50,000 training images
10,000 testing images
Alex Krizhevsky, “Learning Multiple Layers of Features from Tiny Images”, Technical Report, 2009.
Alex Krizhevsky, “Learning Multiple Layers of Features from Tiny Images”, Technical Report, 2009.
(All three images on the right have the same pixel distances to the one on the left)
The K-Nearest Neighbors classifier predicts labels based on the K nearest training
examples
Image
10 numbers giving
f(x,W) class scores
Array of 32x32x3 numbers
(3072 numbers total)
W
parameters
or weights
Image
f(x,W). = W x
10 numbers giving
f(x,W) class scores
Array of 32x32x3 numbers
(3072 numbers total)
W
parameters
or weights
Image
f(x,W) = W x
10x1 10x3072
10 numbers giving
f(x,W) class scores
Array of 32x32x3 numbers
(3072 numbers total)
W
parameters
or weights
Image
f(x,W) = W x + b 10x1
10x1 10x3072
10 numbers giving
f(x,W) class scores
Array of 32x32x3 numbers
(3072 numbers total)
W
parameters
or weights
Linear
classifiers
56
56 231
231
24 2
24
Input image
2
56
0.2 -0.5 0.1 2.0 1.1 -96.8 Cat score
56 231
231
24 2
1.5 1.3 2.1 0.0
24
+ 3.2
= 437.9 Dog score
f(x,W) = Wx + b
Plot created using Wolfram Cloud Cat image by Nikita is licensed under CC-BY 2.0
Cat image by Nikita is licensed under CC-BY 2.0; Car image is CC0 1.0 public domain; Frog image is in the public domain
cat 3.2
car 5.1
frog -1.7
cat 3.2
car 5.1
frog -1.7
cat 3.2
car 5.1
frog -1.7
cat 3.2
Q1: What is the min/max possible softmax loss Li?
car 5.1
frog -1.7 Q2: At initialization all sj will be approximately equal;
what is the softmax loss Li, assuming C classes?
- Regularization
- Optimization