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
Babay Mohamed-Khalil
Image Classification
Commits
ef4c73ac
Commit
ef4c73ac
authored
1 year ago
by
Khalil
Browse files
Options
Downloads
Patches
Plain Diff
commit
parent
3644e96e
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
+1
-1
1 addition, 1 deletion
knn.py
mlp.py
+13
-8
13 additions, 8 deletions
mlp.py
results/mpl.png
+0
-0
0 additions, 0 deletions
results/mpl.png
with
14 additions
and
9 deletions
knn.py
+
1
−
1
View file @
ef4c73ac
...
@@ -58,7 +58,7 @@ if __name__== '__main__':
...
@@ -58,7 +58,7 @@ if __name__== '__main__':
plt
.
title
(
"
Accuracy=f(k)
"
)
plt
.
title
(
"
Accuracy=f(k)
"
)
plt
.
xlabel
(
"
k
"
)
plt
.
xlabel
(
"
k
"
)
plt
.
ylabel
(
"
Accuracy
"
)
plt
.
ylabel
(
"
Accuracy
"
)
plt
.
savefig
(
'
C:
\\
Users
\\
LENOVO
\\
Desktop
\\
deeplearning
\\
BE1 - Image Classification
\\
image-classification
\\
results
'
)
plt
.
savefig
(
'
C:
\\
Users
\\
LENOVO
\\
Desktop
\\
deeplearning
\\
BE1 - Image Classification
\\
image-classification
\\
results
\\
knn.png
'
)
plt
.
show
()
plt
.
show
()
...
...
This diff is collapsed.
Click to expand it.
mlp.py
+
13
−
8
View file @
ef4c73ac
...
@@ -9,7 +9,7 @@ def learn_once_mse(w1,b1,w2,b2,data,targets,learning_rate):
...
@@ -9,7 +9,7 @@ def learn_once_mse(w1,b1,w2,b2,data,targets,learning_rate):
z1
=
np
.
matmul
(
a0
,
w1
)
+
b1
# input of the hidden 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)
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
z2
=
np
.
matmul
(
a1
,
w2
)
+
b2
# input of the output layer
a2
=
np
.
exp
(
z2
)
/
np
.
sum
(
z2
)
# output of the output layer (s
oftmax
activation function)
a2
=
1
/
(
1
+
np
.
exp
(
-
z2
))
# output of the output layer (s
igmoid
activation function)
predictions
=
a2
# the predicted values are the outputs of the output layer
predictions
=
a2
# the predicted values are the outputs of the output layer
# Compute loss (MSE)
# Compute loss (MSE)
...
@@ -64,17 +64,17 @@ def learn_once_cross_entropy(w1,b1,w2,b2,data,labels_train,learning_rate):
...
@@ -64,17 +64,17 @@ def learn_once_cross_entropy(w1,b1,w2,b2,data,labels_train,learning_rate):
predictions
=
a2
# the predicted values are the outputs of the output layer
predictions
=
a2
# the predicted values are the outputs of the output layer
# Compute loss (Binary X-entropy)
# Compute loss (Binary X-entropy)
loss
=
-
np
.
sum
(
one_hot_labels
*
np
.
log
(
predictions
)
+
(
1
-
one_hot_labels
)
*
np
.
log
(
1
-
predictions
))
/
N
loss
=
-
np
.
sum
(
one_hot_labels
*
np
.
log
(
predictions
)
+
(
1
-
one_hot_labels
)
*
np
.
log
(
1
-
predictions
))
# Backward pass
# Backward pass
dz2
=
a2
-
one_hot_labels
dz2
=
a2
-
one_hot_labels
dw2
=
np
.
dot
(
np
.
transpose
(
a1
),
dz2
)
dw2
=
np
.
dot
(
np
.
transpose
(
a1
),
dz2
)
/
N
db2
=
dz2
db2
=
dz2
da1
=
np
.
dot
(
dz2
,
np
.
transpose
(
w2
))
da1
=
np
.
dot
(
dz2
,
np
.
transpose
(
w2
))
dz1
=
da1
*
a1
*
(
1
-
a1
)
dz1
=
da1
*
a1
*
(
1
-
a1
)
dw1
=
np
.
dot
(
np
.
transpose
(
a0
),
dz1
)
dw1
=
np
.
dot
(
np
.
transpose
(
a0
),
dz1
)
/
N
db1
=
dz1
db1
=
dz1
w1
-=
learning_rate
*
dw1
w1
-=
learning_rate
*
dw1
...
@@ -88,17 +88,19 @@ def learn_once_cross_entropy(w1,b1,w2,b2,data,labels_train,learning_rate):
...
@@ -88,17 +88,19 @@ def learn_once_cross_entropy(w1,b1,w2,b2,data,labels_train,learning_rate):
#Q13
#Q13
def
train_mlp
(
w1
,
b1
,
w2
,
b2
,
data_train
,
labels_train
,
learning_rate
,
num_epoch
):
def
train_mlp
(
w1
,
b1
,
w2
,
b2
,
data_train
,
labels_train
,
learning_rate
,
num_epoch
):
#encoding one hot labels
one_hot_labels
=
one_hot
(
labels_train
)
one_hot_labels
=
one_hot
(
labels_train
)
#encoding one hot labels
N
,
_
=
np
.
shape
(
data_train
)
N
,
_
=
np
.
shape
(
data_train
)
train_accuracies
=
[]
train_accuracies
=
[]
for
i
in
range
(
num_epoch
):
for
i
in
range
(
num_epoch
):
w1
,
b1
,
w2
,
b2
,
loss
,
predictions
=
learn_once_cross_entropy
(
w1
,
b1
,
w2
,
b2
,
data_train
,
labels_train
,
learning_rate
)
w1
,
b1
,
w2
,
b2
,
loss
,
predictions
=
learn_once_cross_entropy
(
w1
,
b1
,
w2
,
b2
,
data_train
,
labels_train
,
learning_rate
)
# predictions is a matrix of probabilities, we need to put one for the biggest propobility for each indivual
# predictions is a matrix of probabilities, we need to put one for the biggest propobility for each indivual
maxi
=
np
.
max
(
predictions
,
1
)
maxi
=
np
.
max
(
predictions
,
1
)
predictions_zeros_ones
=
np
.
floor
(
predictions
/
maxi
[:,
np
.
newaxis
]).
astype
(
int
)
predictions_zeros_ones
=
np
.
floor
(
predictions
/
maxi
[:,
np
.
newaxis
]).
astype
(
int
)
A
=
np
.
sum
(
one_hot_labels
==
predictions_zeros_ones
)
A
=
np
.
sum
(
np
.
all
(
one_hot_labels
==
predictions_zeros_ones
,
axis
=
1
)
)
train_accuracies
.
append
(
A
/
N
)
train_accuracies
.
append
(
A
/
N
)
print
(
i
,
A
/
N
)
return
w1
,
b1
,
w2
,
b2
,
train_accuracies
return
w1
,
b1
,
w2
,
b2
,
train_accuracies
...
@@ -119,9 +121,12 @@ def test_mlp(w1,b1,w2,b2,data_test,labels_test):
...
@@ -119,9 +121,12 @@ def test_mlp(w1,b1,w2,b2,data_test,labels_test):
N
,
_
=
np
.
shape
(
data_test
)
N
,
_
=
np
.
shape
(
data_test
)
maxi
=
np
.
max
(
predictions
,
1
)
maxi
=
np
.
max
(
predictions
,
1
)
predictions_zeros_ones
=
np
.
floor
(
predictions
/
maxi
[:,
np
.
newaxis
]).
astype
(
int
)
predictions_zeros_ones
=
np
.
floor
(
predictions
/
maxi
[:,
np
.
newaxis
]).
astype
(
int
)
A
=
np
.
sum
(
one_hot_labels
==
predictions_zeros_ones
)
V
=
np
.
all
(
one_hot_labels
==
predictions_zeros_ones
,
axis
=
1
)
A
=
np
.
sum
(
V
)
test_accuracy
=
A
/
N
test_accuracy
=
A
/
N
print
(
'
test
'
,
A
/
N
)
return
test_accuracy
return
test_accuracy
#Q15
#Q15
...
...
This diff is collapsed.
Click to expand it.
results/mpl.png
+
0
−
0
View replaced file @
3644e96e
View file @
ef4c73ac
25.9 KiB
|
W:
|
H:
20.5 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