Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
Image classification
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Cart Milan
Image classification
Commits
f6c99d4c
Commit
f6c99d4c
authored
1 year ago
by
Cart Milan
Browse files
Options
Downloads
Patches
Plain Diff
Part 3 : ann
parent
5aabffaa
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
knn.py
+3
-2
3 additions, 2 deletions
knn.py
mlp.py
+175
-0
175 additions, 0 deletions
mlp.py
result/knn.png
+0
-0
0 additions, 0 deletions
result/knn.png
with
178 additions
and
2 deletions
knn.py
+
3
−
2
View file @
f6c99d4c
...
@@ -41,6 +41,7 @@ def test_knn() :
...
@@ -41,6 +41,7 @@ def test_knn() :
X_train
,
X_test
,
y_train
,
y_test
=
read_cifar
.
split_dataset
(
data
,
labels
,
0.9
)
X_train
,
X_test
,
y_train
,
y_test
=
read_cifar
.
split_dataset
(
data
,
labels
,
0.9
)
for
k
in
k_indices
:
for
k
in
k_indices
:
print
(
k
)
accuracy
=
evaluate_knn
(
X_train
,
y_train
,
X_test
,
y_test
,
k
)
accuracy
=
evaluate_knn
(
X_train
,
y_train
,
X_test
,
y_test
,
k
)
accuracies
.
append
(
accuracy
)
accuracies
.
append
(
accuracy
)
...
@@ -50,9 +51,9 @@ def test_knn() :
...
@@ -50,9 +51,9 @@ def test_knn() :
plt
.
plot
(
k_indices
,
accuracies
)
plt
.
plot
(
k_indices
,
accuracies
)
plt
.
title
(
"
Accuracy as function of k
"
)
plt
.
title
(
"
Accuracy as function of k
"
)
plt
.
legend
()
plt
.
legend
()
plt
.
show
()
plt
.
savefig
(
'
/Users/milancart/Documents/GitHub/image-classification/result/knn.png
'
)
plt
.
savefig
(
'
/Users/milancart/Documents/GitHub/image-classification/result/knn.png
'
)
plt
.
show
()
plt
.
close
()
if
__name__
==
"
__main__
"
:
if
__name__
==
"
__main__
"
:
...
...
This diff is collapsed.
Click to expand it.
mlp.py
+
175
−
0
View file @
f6c99d4c
import
numpy
as
np
import
read_cifar
import
matplotlib.pyplot
as
plt
def
sigmoid
(
x
):
return
1
/
(
1
+
np
.
exp
(
-
x
))
def
learn_once_mse
(
w1
,
b1
,
w2
,
b2
,
data
,
targets
,
learning_rate
):
N_out
=
len
(
data
)
#number of training examples
# Forward pass
a0
=
data
# the data are the input of the first layer
z1
=
np
.
dot
(
a0
,
w1
)
+
b1
# input of the hidden layer
a1
=
sigmoid
(
z1
)
# output of the hidden layer (sigmoid activation function)
z2
=
np
.
dot
(
a1
,
w2
)
+
b2
# input of the output layer
a2
=
sigmoid
(
z2
)
# output of the output layer (sigmoid activation function)
predictions
=
a2
# the predicted values are the outputs of the output layer
# Compute loss (MSE)
loss
=
np
.
mean
(
np
.
square
(
predictions
-
targets
))
print
(
f
'
loss:
{
loss
}
'
)
# print('shape a1', a1.shape)
# print('shape w1', w1.shape)
# print('shape b1', b1.shape)
# print('shape a2', a2.shape)
# print('shape w2', w2.shape)
# print('shape b2', b2.shape)
# Backpropagation
# Backpropagation
delta_a2
=
2
/
N_out
*
(
a2
-
targets
)
delta_z2
=
delta_a2
*
(
a2
*
(
1
-
a2
))
# We divide by the sample size to have an average on the error and avoid big gradient jumps
delta_w2
=
np
.
dot
(
a1
.
T
,
delta_z2
)
delta_b2
=
np
.
sum
(
delta_z2
,
axis
=
0
,
keepdims
=
True
)
delta_a1
=
np
.
dot
(
delta_z2
,
w2
.
T
)
delta_z1
=
delta_a1
*
(
a1
*
(
1
-
a1
))
delta_w1
=
np
.
dot
(
a0
.
T
,
delta_z1
)
delta_b1
=
np
.
sum
(
delta_z1
,
axis
=
0
,
keepdims
=
True
)
return
w1
,
b1
,
w2
,
b2
,
loss
def
one_hot
(
labels
):
num_classes
=
int
(
np
.
max
(
labels
)
+
1
)
#num_classes = 10
one_hot_matrix
=
np
.
eye
(
num_classes
)[
labels
]
return
one_hot_matrix
def
softmax_stable
(
x
):
#We use this function to avoid computing big numbers
return
(
np
.
exp
(
x
-
np
.
max
(
x
,
axis
=
1
,
keepdims
=
True
))
/
np
.
exp
(
x
-
np
.
max
(
x
,
axis
=
1
,
keepdims
=
True
)).
sum
())
def
cross_entropy_loss
(
y_pred
,
y_true_one_hot
):
epsilon
=
1e-10
loss
=
-
np
.
sum
(
y_true_one_hot
*
np
.
log
(
y_pred
+
epsilon
)
)
/
len
(
y_pred
)
return
loss
def
learn_once_cross_entropy
(
w1
,
b1
,
w2
,
b2
,
data
,
labels_train
,
learning_rate
):
N_out
=
len
(
data
)
a0
=
data
z1
=
np
.
matmul
(
a0
,
w1
)
+
b1
a1
=
sigmoid
(
z1
)
z2
=
np
.
matmul
(
a1
,
w2
)
+
b2
a2
=
softmax_stable
(
z2
)
predictions
=
a2
y_true_one_hot
=
one_hot
(
labels_train
)
loss
=
cross_entropy_loss
(
predictions
,
y_true_one_hot
)
delta_z2
=
(
a2
-
y_true_one_hot
)
delta_w2
=
np
.
dot
(
a1
.
T
,
delta_z2
)
/
N_out
delta_b2
=
np
.
sum
(
delta_z2
,
axis
=
0
,
keepdims
=
True
)
/
N_out
delta_a1
=
np
.
dot
(
delta_z2
,
w2
.
T
)
delta_z1
=
delta_a1
*
(
a1
*
(
1
-
a1
))
/
N_out
delta_w1
=
np
.
dot
(
a0
.
T
,
delta_z1
)
/
N_out
delta_b1
=
np
.
sum
(
delta_z1
,
axis
=
0
,
keepdims
=
True
)
/
N_out
w1
-=
learning_rate
*
delta_w1
b1
-=
learning_rate
*
delta_b1
w2
-=
learning_rate
*
delta_w2
b2
-=
learning_rate
*
delta_b2
return
w1
,
b1
,
w2
,
b2
,
loss
def
forward
(
w1
,
b1
,
w2
,
b2
,
data
):
a0
=
data
z1
=
np
.
matmul
(
a0
,
w1
)
+
b1
a1
=
sigmoid
(
z1
)
z2
=
np
.
matmul
(
a1
,
w2
)
+
b2
a2
=
softmax_stable
(
z2
)
predictions
=
a2
return
(
predictions
)
def
train_mlp
(
w1
,
b1
,
w2
,
b2
,
data_train
,
labels_train
,
learning_rate
,
num_epoch
):
train_accuracies
=
[]
for
epoch
in
range
(
num_epoch
):
w1
,
b1
,
w2
,
b2
,
loss
=
learn_once_cross_entropy
(
w1
,
b1
,
w2
,
b2
,
data_train
,
labels_train
,
learning_rate
)
# Compute accuracy
predictions
=
forward
(
w1
,
b1
,
w2
,
b2
,
data_train
)
predicted_labels
=
np
.
argmax
(
predictions
,
axis
=
1
)
accuracy
=
np
.
mean
(
predicted_labels
==
labels_train
)
train_accuracies
.
append
(
accuracy
)
print
(
f
'
Epoch
{
epoch
+
1
}
/
{
num_epoch
}
, Loss:
{
loss
:
.
3
f
}
, Train Accuracy:
{
accuracy
:
.
5
f
}
'
)
return
w1
,
b1
,
w2
,
b2
,
train_accuracies
def
test_mlp
(
w1
,
b1
,
w2
,
b2
,
data_test
,
labels_test
):
# Compute accuracy
predictions
=
forward
(
w1
,
b1
,
w2
,
b2
,
data_test
)
predicted_labels
=
np
.
argmax
(
predictions
,
axis
=
1
)
test_accuracy
=
np
.
mean
(
predicted_labels
==
labels_test
)
print
(
f
'
Test Accuracy:
{
test_accuracy
:
.
2
f
}
'
)
return
test_accuracy
def
run_mlp_training
(
data_train
,
labels_train
,
data_test
,
labels_test
,
d_h
,
learning_rate
,
num_epoch
):
d_in
=
data_train
.
shape
[
1
]
d_out
=
10
#Random initialisation of weights Xavier initialisation
w1
=
np
.
random
.
randn
(
d_in
,
d_h
)
/
np
.
sqrt
(
d_in
)
b1
=
np
.
zeros
((
1
,
d_h
))
w2
=
np
.
random
.
randn
(
d_h
,
d_out
)
/
np
.
sqrt
(
d_h
)
b2
=
np
.
zeros
((
1
,
d_out
))
# Train MLP
w1
,
b1
,
w2
,
b2
,
train_accuracies
=
train_mlp
(
w1
,
b1
,
w2
,
b2
,
data_train
,
labels_train
,
learning_rate
,
num_epoch
)
# Test MLP
test_accuracy
=
test_mlp
(
w1
,
b1
,
w2
,
b2
,
data_test
,
labels_test
)
return
train_accuracies
,
test_accuracy
def
plot_graph
(
data_train
,
labels_train
,
data_test
,
labels_test
,
d_h
,
learning_rate
,
num_epoch
):
# Run MLP training
train_accuracies
,
test_accuracy
=
run_mlp_training
(
data_train
,
labels_train
,
data_test
,
labels_test
,
d_h
,
learning_rate
,
num_epoch
)
# Plot and save the learning accuracy graph
plt
.
figure
(
figsize
=
(
8
,
6
))
epochs
=
np
.
arange
(
1
,
num_epoch
+
1
)
plt
.
plot
(
epochs
,
train_accuracies
,
marker
=
'
x
'
,
color
=
'
b
'
,
label
=
'
Train Accuracy
'
)
plt
.
xlabel
(
'
Epochs
'
)
plt
.
ylabel
(
'
Accuracy
'
)
plt
.
title
(
'
MLP Train Accuracy
'
)
plt
.
legend
()
plt
.
savefig
(
'
/Users/milancart/Documents/GitHub/image-classification/results/mlp.png
'
)
plt
.
show
()
plt
.
close
()
if
__name__
==
'
__main__
'
:
data
,
labels
=
read_cifar
.
read_cifar
(
'
/Users/milancart/Documents/GitHub/image-classification/Data/cifar-10-batches-py
'
)
X_train
,
X_test
,
y_train
,
y_test
=
read_cifar
.
split_dataset
(
data
,
labels
,
0.9
)
d_in
,
d_h
,
d_out
=
3072
,
64
,
10
learning_rate
=
0.1
num_epoch
=
100
plot_graph
(
X_train
,
y_train
,
X_test
,
y_test
,
d_h
,
learning_rate
,
num_epoch
)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
result/knn.png
+
0
−
0
View replaced file @
5aabffaa
View file @
f6c99d4c
2.34 KiB
|
W:
|
H:
32.8 KiB
|
W:
|
H:
2-up
Swipe
Onion skin
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment