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
Cosserat Esteban
Image classification
Commits
32d8ee01
Commit
32d8ee01
authored
1 year ago
by
Sucio
Browse files
Options
Downloads
Patches
Plain Diff
neurone_part
parent
e3c9c6c9
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
Rapport.ipynb
+330
-32
330 additions, 32 deletions
Rapport.ipynb
mlp.py
+117
-0
117 additions, 0 deletions
mlp.py
test.py
+21
-18
21 additions, 18 deletions
test.py
with
468 additions
and
50 deletions
Rapport.ipynb
+
330
−
32
View file @
32d8ee01
Source diff could not be displayed: it is too large. Options to address this:
view the blob
.
This diff is collapsed.
Click to expand it.
mlp.py
0 → 100644
+
117
−
0
View file @
32d8ee01
import
numpy
as
np
import
pickle
from
read_cifar
import
read_cifar_batch
,
split_dataset
import
matplotlib.pyplot
as
plt
def
learning_methode
(
k
,
dk
,
learning_rate
):
k
=
k
-
learning_rate
*
dk
return
(
k
)
def
learn_once_mse
(
w1
,
b1
,
w2
,
b2
,
data
,
targets
,
learning_rate
):
# Forward pass
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 (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
#dc_da2=(2/data.shape[0])*(a2-targets)
dc_da2
=
(
1
/
data
.
shape
[
0
])
*
((
-
targets
/
a2
)
-
(
1
-
targets
)
/
(
1
-
a2
))
dc_dz2
=
dc_da2
*
(
a2
*
(
1
-
a2
))
dc_dw2
=
np
.
matmul
(
np
.
transpose
(
a1
),
dc_dz2
)
dc_db2
=
np
.
matmul
(
np
.
ones
((
1
,
dc_dz2
.
shape
[
0
])),
dc_dz2
)
dc_da1
=
np
.
matmul
(
dc_dz2
,
np
.
transpose
(
w2
))
dc_dz1
=
dc_da1
*
(
a1
*
(
1
-
a1
))
dc_dw1
=
np
.
matmul
(
np
.
transpose
(
a0
),
dc_dz1
)
dc_db1
=
np
.
matmul
(
np
.
ones
((
1
,
dc_dz1
.
shape
[
0
])),
dc_dz1
)
w1
=
learning_methode
(
w1
,
dc_dw1
,
learning_rate
)
b1
=
learning_methode
(
b1
,
dc_db1
,
learning_rate
)
w2
=
learning_methode
(
w2
,
dc_dw2
,
learning_rate
)
b2
=
learning_methode
(
b2
,
dc_db2
,
learning_rate
)
# Compute loss (MSE)
# loss = np.mean(np.square(predictions - targets))
# binary cross-entropy loss
loss
=
np
.
mean
(
targets
*
np
.
log
(
predictions
)
-
(
1
-
targets
)
*
np
.
log
(
1
-
predictions
))
return
(
w1
,
b1
,
w2
,
b2
,
loss
)
def
one_hot
(
label
):
nbr_classe
=
9
mat
=
np
.
zeros
((
len
(
label
),
nbr_classe
))
for
label_indexe
,
label_im
,
in
enumerate
(
label
):
mat
[
label_indexe
,
label_im
-
1
]
=
1
return
(
mat
)
def
learn_once_cross_entropy
(
w1
,
b1
,
w2
,
b2
,
data
,
labels_train
,
learning_rate
):
Y
=
one_hot
(
labels_train
)
w1
,
b1
,
w2
,
b2
,
loss
=
learn_once_mse
(
w1
,
b1
,
w2
,
b2
,
data
,
Y
,
learning_rate
)
return
(
w1
,
b1
,
w2
,
b2
,
loss
)
def
train_mlp
(
w1
,
b1
,
w2
,
b2
,
d_train
,
labels_train
,
learning_rate
,
num_epoch
):
train_accuracies
=
[]
pas
=
len
(
labels_train
)
//
num_epoch
for
k
in
range
(
num_epoch
):
partial_data
=
d_train
[
k
*
pas
:(
k
+
1
)
*
pas
,:]
patial_label
=
l_train
[
k
*
pas
:(
k
+
1
)
*
pas
]
w1
,
b1
,
w2
,
b2
,
loss
=
learn_once_cross_entropy
(
w1
,
b1
,
w2
,
b2
,
partial_data
,
patial_label
,
learning_rate
)
train_accuracies
.
append
(
loss
)
return
(
w1
,
b1
,
w2
,
b2
,
train_accuracies
)
def
test_mlp
(
w1
,
b1
,
w2
,
b2
,
d_test
,
labels_test
):
a0
=
d_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
prediction_2
=
np
.
empty
(
predictions
.
shape
[
0
],
dtype
=
int
)
for
i
,
ligne
in
enumerate
(
predictions
):
prediction_2
[
i
]
=
np
.
argmax
(
ligne
)
+
1
indices_egalite
=
np
.
where
(
prediction_2
==
labels_test
)[
0
]
nombre_indices
=
len
(
indices_egalite
)
return
(
nombre_indices
/
len
(
labels_test
))
def
run_mlp_training
(
data_train
,
labels_train
,
data_test
,
labels_test
,
d_h
,
learning_rate
,
num_epoch
):
d_in
=
data_train
.
shape
[
1
]
# input dimension
d_out
=
max
(
labels_train
)
# output dimension (number of neurons of the output layer)
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
w1
,
b1
,
w2
,
b2
,
loss
=
train_mlp
(
w1
,
b1
,
w2
,
b2
,
data_train
,
labels_train
,
learning_rate
,
num_epoch
)
test_accuracy
=
test_mlp
(
w1
,
b1
,
w2
,
b2
,
data_test
,
labels_test
)
test_accuracy2
=
unit_test
(
w1
,
b1
,
w2
,
b2
,
data_test
,
labels_test
)
print
(
test_accuracy
,
test_accuracy2
)
return
(
loss
,
test_accuracy
)
def
unit_test
(
w1
,
b1
,
w2
,
b2
,
data_test
,
labels_test
):
pos
=
0
for
indexe
,
image
in
enumerate
(
data_test
):
a0
=
[
image
]
# 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
classe
=
np
.
argmax
(
predictions
[
0
])
+
1
if
classe
==
labels_test
[
indexe
]:
pos
+=
1
return
(
pos
/
len
(
labels_test
))
if
__name__
==
"
__main__
"
:
d
,
l
=
read_cifar_batch
(
"
data/cifar-10-batches-py/data_batch_1
"
)
num_epoch
=
100
d_train
,
l_train
,
d_test
,
l_test
=
split_dataset
(
d
,
l
,
0.9
)
loss
,
test_accuracy
=
run_mlp_training
(
d_train
,
l_train
,
d_test
,
l_test
,
64
,
0.1
,
num_epoch
)
print
(
test_accuracy
)
plt
.
plot
(
range
(
num_epoch
),
loss
,
label
=
'
evolution de la fonction loss par epoque
'
)
plt
.
xlabel
(
'
epoque
'
)
plt
.
ylabel
(
'
loss
'
)
plt
.
legend
()
plt
.
show
()
This diff is collapsed.
Click to expand it.
test.py
+
21
−
18
View file @
32d8ee01
import
numpy
as
np
label
=
np
.
array
([
1
,
2
,
2
,
3
,
3
])
dist
=
np
.
array
([[
10
,
25
,
10
,
42
,
3
],[
75
,
63
,
87
,
64
,
1
]])
for
im
in
dist
:
dico
=
{}
kmax
=
np
.
argpartition
(
im
,
3
)[:
3
]
for
indexe
in
kmax
:
if
label
[
indexe
]
in
dico
:
dico
[
label
[
indexe
]][
0
]
+=
1
dico
[
label
[
indexe
]][
1
]
+=
im
[
indexe
]
else
:
dico
[
label
[
indexe
]]
=
[
1
,
im
[
indexe
]]
dico
=
sorted
(
dico
.
items
(),
key
=
lambda
item
:
item
[
1
][
0
],
reverse
=
True
)
#
label=np.array([1,2,2,3,3])
#
dist=np.array([[10,25,10,42,3],[75,63,87,64,1]])
#
for im in dist:
#
dico={}
#
kmax=np.argpartition(im, 3)[:3]
#
for indexe in kmax:
#
if label[indexe] in dico:
#
dico[label[indexe]][0]+=1
#
dico[label[indexe]][1]+=im[indexe]
#
else:
#
dico[label[indexe]]=[1,im[indexe]]
#
dico = sorted(dico.items(), key=lambda item: item[1][0], reverse=True)
max_value
=
dico
[
0
][
1
][
0
]
dico
=
[
item
for
item
in
dico
if
item
[
1
][
0
]
==
max_value
]
print
(
dico
)
if
len
(
dico
)
>
1
:
filtered_dict
=
sorted
(
dico
,
key
=
lambda
item
:
item
[
1
][
1
])
return
(
dico
[
0
][
0
])
#
max_value = dico[0][1][0]
#
dico = [item for item in dico if item[1][0] == max_value]
#
print(dico)
#
if len(dico) > 1:
#
filtered_dict = sorted(dico, key=lambda item: item[1][1])
#
print
(dico[0][0])
K
=
np
.
array
([
8
,
4
])
dc_dw2
=
np
.
matmul
(
np
.
transpose
(
a1
),
dc_dz2
)
print
(
K
)
\ No newline at end of file
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