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
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Muniz Silva Samuel
Image classification
Commits
e7947326
Commit
e7947326
authored
Nov 8, 2022
by
Muniz Silva Samuel
Browse files
Options
Downloads
Patches
Plain Diff
final commit
parent
dda81a24
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
knn.py
+69
-197
69 additions, 197 deletions
knn.py
with
69 additions
and
197 deletions
knn.py
+
69
−
197
View file @
e7947326
import
numpy
as
np
import
tensorflow
as
tf
import
pandas
as
pd
import
pickle
import
os
import
scipy
from
sklearn.model_selection
import
train_test_split
from
sklearn.neighbors
import
KNeighborsRegressor
import
matplotlib.pyplot
as
plt
def
learn_once_mse
(
w1
,
b1
,
w2
,
b2
,
data
,
targets
,
learning_rate
):
"""
Take the arrays w1,b1,w2,b2 of a 2-layers neural network
,update them with a gradient descent
and calculate the average lost the MSE method
"""
d_in
,
d_h
=
w1
.
shape
# extracts the dimensions of the variables to define future np.arrays
N
,
d_out
=
targets
.
shape
a0
=
data
# 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
z2
=
np
.
matmul
(
a1
,
w2
)
+
b2
# input of the output layer
a2
=
1
/
(
1
+
np
.
exp
(
-
z2
))
# output of the output layer
predictions
=
a2
# the predicted values are the outputs of the output layer
#Create the gradient for the variables w2,b2,w1,b1
dCdw2
=
np
.
zeros
((
d_h
,
d_out
))
dCdb2
=
np
.
zeros
((
1
,
d_out
))
dCdw1
=
np
.
zeros
((
d_in
,
d_h
))
dCdb1
=
np
.
zeros
((
1
,
d_h
))
#take each data with its respective labels
for
dataRow
,
targetsRow
in
zip
(
data
,
targets
):
a0
=
dataRow
# 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
z2
=
np
.
matmul
(
a1
,
w2
)
+
b2
# input of the output layer
a2
=
1
/
(
1
+
np
.
exp
(
-
z2
))
# output of the output layer
predictionsRow
=
a2
# the predicted values are the outputs of the output layer
# Calculate the partial derivative of the cost in relaltion to each network output
dCda
=
2
*
(
predictionsRow
-
targetsRow
)
# sum the contribution of each data for the w2 updating
for
l
in
range
(
d_h
):
for
m
in
range
(
d_out
):
dCdw2
[
l
][
m
]
+=
(
dCda
[
l
]
*
a2
[
l
]
*
(
1
-
a2
[
l
])
*
a1
[
m
]
)
# sum the contribution of each data for the b2 updating
for
l
in
range
(
d_out
):
dCdb2
[
0
][
l
]
+=
(
dCda
[
l
]
*
a2
[
l
]
*
(
1
-
a2
[
l
])
)
# sum the contribution of each data for the w1 updating
for
l
in
range
(
d_in
):
for
m
in
range
(
d_h
):
for
j
in
range
(
d_out
):
dCdw1
[
l
][
m
]
+=
(
dCda
[
j
]
*
a2
[
j
]
*
(
1
-
a2
[
j
])
*
w2
[
j
][
l
]
*
a1
[
l
]
*
(
1
-
a1
[
l
])
*
a0
[
m
]
)
# sum the contribution of each data for the b1 updating
for
l
in
range
(
d_h
):
for
j
in
range
(
d_out
):
dCdb1
[
0
][
l
]
+=
(
dCda
[
j
]
*
a2
[
j
]
*
(
1
-
a2
[
j
])
*
w2
[
j
][
l
]
*
a1
[
l
]
*
(
1
-
a1
[
l
])
)
def
distance_matrix
(
data_test
,
data_train
):
"""
Takes the matrix data_test and data_train. It returning a 2d array(N,M) such that dists[i,j] represents
the distance between the i-th data_test row and the j-th data_train row
"""
dists
=
np
.
array
([
np
.
sum
((
data_train
-
l
)
**
2
,
axis
=
1
)
**
0.5
for
l
in
data_test
])
#Average value of each data contribution
dCdw1
=
dCdw1
/
N
dCdb1
=
dCdb1
/
N
dCdw2
=
dCdw2
/
N
dCdb2
=
dCdb2
/
N
#Arrays update
w1
-=
learning_rate
*
dCdw1
b1
-=
learning_rate
*
dCdb1
w2
-=
learning_rate
*
dCdw2
b2
-=
learning_rate
*
dCdb2
# realizing a new network interaction with new values
a0
=
data
# the data are the input of the first layer
new_z1
=
np
.
matmul
(
a0
,
new_w1
)
+
new_b1
# input of the hidden layer
new_a1
=
1
/
(
1
+
np
.
exp
(
-
z1
))
# output of the hidden layer
z2
=
np
.
matmul
(
new_a1
,
new_w2
)
+
new_b2
# input of the output layer
a2
=
1
/
(
1
+
np
.
exp
(
-
z2
))
# output of the output layer
predictions
=
a2
# the predicted values are the outputs of the output layer
# Compute loss (MSE)
loss
=
np
.
mean
(
np
.
square
(
predictions
-
targets
))
return
w1
,
b1
,
w2
,
b2
,
loss
def
one_hot
(
labels
):
"""
Returns the 2d array with binary vectors with the 1
'
s in the respective position of the sort matrix
"""
oneHotMat
=
np
.
zeros
((
labels
.
size
,
labels
.
size
),
dtype
=
int
)
for
index
,
values
in
enumerate
(
labels
):
oneHotMat
[
index
,
values
]
=
1
return
oneHotMat
def
learn_once_cross_entropy
(
w1
,
b1
,
w2
,
b2
,
data
,
labels_train
,
learning_rate
):
"""
Take the arrays w1,b1,w2,b2 of a 2-layers neural network
,update them with a gradient descent
and calculate the average lost the cross - entropy method
"""
d_in
,
d_h
=
w1
.
shape
# extracts the dimensions of the variables to define future np.arrays
N
,
d_out
=
targets
.
shape
a0
=
data
# 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
z2
=
np
.
matmul
(
a1
,
w2
)
+
b2
# input of the output layer
a2
=
1
/
(
1
+
np
.
exp
(
-
z2
))
# output of the output layer
predictions
=
a2
# the predicted values are the outputs of the output layer
oneHot
=
one_hot
(
labels_train
)
#Create the gradient for the variables w2,b2,w1,b1
dCdw2
=
np
.
zeros
((
d_h
,
d_out
))
dCdb2
=
np
.
zeros
((
1
,
d_out
))
dCdw1
=
np
.
zeros
((
d_in
,
d_h
))
dCdb1
=
np
.
zeros
((
1
,
d_h
))
#take each data with its respective labels
for
dataRow
,
oneHotLabel
in
zip
(
data
,
oneHot
):
a0
=
dataRow
# 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
z2
=
np
.
matmul
(
a1
,
w2
)
+
b2
# input of the output layer
a2
=
1
/
(
1
+
np
.
exp
(
-
z2
))
# output of the output layer
predictionsRow
=
a2
# the predicted values are the outputs of the output layer
dCdz2
=
predictionsRow
-
oneHotLabel
# sum the contribution of each data for the w2 updating
for
l
in
range
(
d_h
):
for
m
in
range
(
d_out
):
dCdw2
[
l
][
m
]
+=
(
dCdz2
[
l
]
*
a1
[
m
]
)
# sum the contribution of each data for the b2 updating
for
l
in
range
(
d_out
):
dCdb2
[
0
][
l
]
+=
(
dCdz2
[
l
]
)
# sum the contribution of each data for the w1 updating
for
l
in
range
(
d_in
):
for
m
in
range
(
d_h
)
:
for
j
in
range
(
d_out
):
dCdw1
[
l
][
m
]
+=
(
dCdz2
[
j
]
*
w2
[
j
][
l
]
*
a1
[
l
]
*
(
1
-
a1
[
l
])
*
a0
[
m
]
)
# sum the contribution of each data for the b1 updating
for
l
in
range
(
d_h
):
for
j
in
range
(
d_out
):
dCdb1
[
0
][
l
]
+=
(
dCdz2
[
j
]
*
w2
[
j
][
l
]
*
a1
[
l
]
*
(
1
-
a1
[
l
])
return
dists
def
knn_predict
(
dists
,
labels_train
,
k
):
"""
Take the matrix of distances dists, the labels for training and k nearest neighbor
It returns the classification given by the module KNN.
"""
# classif = np.array(0)
print
(
labels_train
[:
20
])
print
(
labels_train
.
size
)
classif
=
[]
for
testRows
in
dists
.
T
:
distances
=
np
.
stack
((
testRows
,
labels_train
),
axis
=
1
)
distances
=
distances
[
distances
[:,
0
].
argsort
()]
# for picturesClasses in distances[:k,1]:
countArray
=
[
np
.
count_nonzero
(
distances
[:
k
,
1
]
==
i
)
for
i
in
range
(
0
,
10
)]
classif
=
np
.
append
(
classif
,
np
.
argmax
(
countArray
))
classif
=
np
.
array
(
classif
,
dtype
=
int
)
return
classif
def
evaluate_knn
(
data_train
,
labels_train
,
data_test
,
labels_test
,
k
):
"""
Receives the datas ans labels for training and teste and k nearest neighbor.
It retuns the accuracy of the KNN module
"""
classif
=
np
.
array
(
knn_predict
(
distance_matrix
(
data_train
,
data_test
),
labels_train
,
k
)
)
result
=
np
.
array
(
classif
==
labels_test
)
acc
=
np
.
count_nonzero
(
result
)
/
np
.
size
(
result
)
#Average value of each data contribution
dCdw1
=
dCdw1
/
N
dCdb1
=
dCdb1
/
N
dCdw2
=
dCdw2
/
N
dCdb2
=
dCdb2
/
N
return
acc
*
100
#Arrays update
w1
-=
learning_rate
*
dCdw1
b1
-=
learning_rate
*
dCdb1
w2
-=
learning_rate
*
dCdw2
b2
-=
learning_rate
*
dCdb2
# realizing a new network interaction with new values
a0
=
data
# the data are the input of the first layer
new_z1
=
np
.
matmul
(
a0
,
new_w1
)
+
new_b1
# input of the hidden layer
new_a1
=
1
/
(
1
+
np
.
exp
(
-
z1
))
# output of the hidden layer
z2
=
np
.
matmul
(
new_a1
,
new_w2
)
+
new_b2
# input of the output layer
a2
=
1
/
(
1
+
np
.
exp
(
-
z2
))
# output of the output layer
predictions
=
a2
# the predicted values are the outputs of the output layer
datas
,
labels
=
read_cifar_batch
(
"
data_batch_1
"
)
dataTrain
,
dataTest
,
labelsTrain
,
labelsTest
=
split_dataset
(
datas
,
labels
)
distanceMatrix
=
distance_matrix
(
dataTrain
,
dataTest
)
print
()
# Compute loss (Entropy Loss)
result
=
[]
for
i
in
range
(
1
,
21
):
result
=
np
.
append
(
result
,
evaluate_knn
(
dataTrain
,
labelsTrain
,
dataTest
,
labelsTest
,
i
)
)
loss
=
np
.
mean
(
(
-
1
*
oneHot
*
np
.
log
(
predictions
)
)
-
(
1
-
oneHot
)
*
np
.
log
(
1
-
predictions
)
)
x
=
np
.
arange
(
1
,
21
)
return
w1
,
b1
,
w2
,
b2
,
loss
# plot the graph of (Accuracy) x k
plt
.
title
(
"
Plot graph
"
)
plt
.
xlabel
(
"
K neighbors
"
)
plt
.
ylabel
(
"
Accuracy %
"
)
plt
.
plot
(
x
,
result
,
color
=
"
red
"
)
plt
.
show
()
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