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
Matías Duhalde
Image classification
Commits
5f854f60
Commit
5f854f60
authored
1 year ago
by
Matías Duhalde
Browse files
Options
Downloads
Patches
Plain Diff
feat: use softmax
parent
fffcc01f
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
mlp.py
+13
-21
13 additions, 21 deletions
mlp.py
with
13 additions
and
21 deletions
mlp.py
+
13
−
21
View file @
5f854f60
...
@@ -98,16 +98,12 @@ def learn_once_cross_entropy(
...
@@ -98,16 +98,12 @@ def learn_once_cross_entropy(
"""
"""
# Forward pass
# Forward pass
a0
=
data
# the data are the input of the first layer
a0
=
data
z1
=
np
.
matmul
(
a0
,
w1
)
+
b1
# input of the hidden layer
z1
=
np
.
matmul
(
a0
,
w1
)
+
b1
a1
=
1
/
(
a1
=
1
/
(
1
+
np
.
exp
(
-
z1
))
1
+
np
.
exp
(
-
z1
)
z2
=
np
.
matmul
(
a1
,
w2
)
+
b2
)
# output of the hidden layer (sigmoid activation function)
a2
=
np
.
exp
(
z2
)
/
np
.
sum
(
np
.
exp
(
z2
),
axis
=
1
,
keepdims
=
True
)
z2
=
np
.
matmul
(
a1
,
w2
)
+
b2
# input of the output layer
predictions
=
a2
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
one_hot_targets
=
one_hot
(
labels_train
)
one_hot_targets
=
one_hot
(
labels_train
)
...
@@ -171,7 +167,7 @@ def train_mlp(
...
@@ -171,7 +167,7 @@ def train_mlp(
for
_
in
range
(
num_epoch
):
for
_
in
range
(
num_epoch
):
# Train once
# Train once
w1
,
b1
,
w2
,
b2
,
_
=
learn_once_
mse
(
w1
,
b1
,
w2
,
b2
,
_
=
learn_once_
cross_entropy
(
w1
,
b1
,
w2
,
b2
,
data_train
,
labels_train
,
learning_rate
w1
,
b1
,
w2
,
b2
,
data_train
,
labels_train
,
learning_rate
)
)
...
@@ -204,16 +200,12 @@ def test_mlp(
...
@@ -204,16 +200,12 @@ def test_mlp(
float: The testing accuracy of the model on the given data.
float: The testing accuracy of the model on the given data.
"""
"""
# Forward pass
# Forward pass
a0
=
data_test
# the data are the input of the first layer
a0
=
data_test
z1
=
np
.
matmul
(
a0
,
w1
)
+
b1
# input of the hidden layer
z1
=
np
.
matmul
(
a0
,
w1
)
+
b1
a1
=
1
/
(
a1
=
1
/
(
1
+
np
.
exp
(
-
z1
))
1
+
np
.
exp
(
-
z1
)
z2
=
np
.
matmul
(
a1
,
w2
)
+
b2
)
# output of the hidden layer (sigmoid activation function)
a2
=
np
.
exp
(
z2
)
/
np
.
sum
(
np
.
exp
(
z2
),
axis
=
1
,
keepdims
=
True
)
z2
=
np
.
matmul
(
a1
,
w2
)
+
b2
# input of the output layer
predictions
=
a2
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
# Compute accuracy
# Compute accuracy
accuracy
=
np
.
mean
(
np
.
argmax
(
predictions
,
axis
=
1
)
==
labels_test
)
accuracy
=
np
.
mean
(
np
.
argmax
(
predictions
,
axis
=
1
)
==
labels_test
)
...
...
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