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
Audard Lucile
Image classification
Commits
56cceedd
Commit
56cceedd
authored
1 year ago
by
Audard Lucile
Browse files
Options
Downloads
Patches
Plain Diff
Update mlp.py
parent
3ae0d3c9
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
mlp.py
+65
-4
65 additions, 4 deletions
mlp.py
with
65 additions
and
4 deletions
mlp.py
+
65
−
4
View file @
56cceedd
import
numpy
as
np
from
read_cifar
import
*
import
matplotlib.pyplot
as
plt
def
sigmoid
(
x
):
return
1
/
(
1
+
np
.
exp
(
-
x
))
...
...
@@ -57,7 +59,7 @@ def learn_once_cross_entropy(w1, b1, w2, b2, data, labels_train, learning_rate):
targets_one_hot
=
one_hot
(
labels_train
)
# target as a one-hot encoding for the desired labels
#
c
ross-entropy loss
#
C
ross-entropy loss
loss
=
-
np
.
sum
(
targets_one_hot
*
np
.
log
(
predictions
))
/
N
# Backpropagation
...
...
@@ -94,18 +96,77 @@ def train_mlp(w1, b1, w2, b2, data_train, labels_train, learning_rate, num_epoch
# Find the predicted class
prediction
=
np
.
argmax
(
predictions
,
axis
=
1
)
# Calculate the accuracy
# Calculate the accuracy
for the step
accuracy
=
np
.
mean
(
labels_train
==
prediction
)
train_accuracies
[
i
]
=
accuracy
train_accuracies
[
i
]
=
accuracy
return
w1
,
b1
,
w2
,
b2
,
train_accuracies
def
test_mlp
(
w1
,
b1
,
w2
,
b2
,
data_test
,
labels_test
):
# Forward pass
a0
=
data_test
# the data are the input of the first layer
z1
=
np
.
matmul
(
a0
,
w1
)
+
b1
# input of the hidden layer
a1
=
1
/
(
1
+
np
.
exp
(
-
z1
))
# output of the hidden layer (sigmoid activation function)
z2
=
np
.
matmul
(
a1
,
w2
)
+
b2
# input of the output layer
a2
=
1
/
(
1
+
np
.
exp
(
-
z2
))
# output of the output layer (sigmoid activation function)
predictions
=
a2
# the predicted values are the outputs of the output layer
# Find the predicted label
prediction
=
np
.
argmax
(
predictions
,
axis
=
1
)
# Calculation of the test accuracy
test_accuracy
=
np
.
mean
(
prediction
==
labels_test
)
return
test_accuracy
def
run_mlp_training
(
data_train
,
labels_train
,
data_test
,
labels_test
,
d_h
,
learning_rate
,
num_epoch
):
# Define parameters
d_in
=
data_train
.
shape
[
1
]
# number of input neurons
d_out
=
len
(
np
.
unique
(
labels_train
))
# number of output neurons = number of classes
# Random initialization of the network weights and biaises
w1
=
2
*
np
.
random
.
rand
(
d_in
,
d_h
)
-
1
# first layer weights
b1
=
np
.
zeros
((
1
,
d_h
))
# first layer biaises
w2
=
2
*
np
.
random
.
rand
(
d_h
,
d_out
)
-
1
# second layer weights
b2
=
np
.
zeros
((
1
,
d_out
))
# second layer biaises
# Training of the MLP classifier with num_epoch steps
w1
,
b1
,
w2
,
b2
,
train_accuracies
=
train_mlp
(
w1
,
b1
,
w2
,
b2
,
data_train
,
labels_train
,
learning_rate
,
num_epoch
)
# Caculation of the final testing accuracy with the new values of the weights and bias
test_accuracy
=
test_mlp
(
w1
,
b1
,
w2
,
b2
,
data_test
,
labels_test
)
return
train_accuracies
,
test_accuracy
if
__name__
==
"
__main__
"
:
split_factor
=
0.9
d_h
=
64
learning_rate
=
0.1
num_epoch
=
100
data
,
labels
=
read_cifar
(
"
./data/cifar-10-batches-py
"
)
data_train
,
labels_train
,
data_test
,
labels_test
=
split_dataset
(
data
,
labels
,
split_factor
)
epochs
=
[
i
for
i
in
range
(
1
,
num_epoch
+
1
)]
learning_accuracy
=
[
0
]
*
num_epoch
for
i
in
range
(
num_epoch
)
:
train_accuracies
,
test_accuracy
=
run_mlp_training
(
data_train
,
labels_train
,
data_test
,
labels_test
,
d_h
,
learning_rate
,
i
+
1
)
learning_accuracy
[
i
]
=
test_accuracy
plt
.
plot
(
epochs
,
learning_accuracy
)
plt
.
title
(
"
Evolution of learning accuracy across learning epochs
"
)
plt
.
xlabel
(
"
number of epochs
"
)
plt
.
ylabel
(
"
Accuracy
"
)
plt
.
grid
(
True
,
which
=
'
both
'
)
plt
.
savefig
(
"
results/mlp.png
"
)
...
...
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