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
59861d42
Commit
59861d42
authored
1 year ago
by
Sucio
Browse files
Options
Downloads
Patches
Plain Diff
maj
parent
32d8ee01
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
Rapport.ipynb
+63
-58
63 additions, 58 deletions
Rapport.ipynb
mlp.py
+39
-10
39 additions, 10 deletions
mlp.py
test.py
+10
-3
10 additions, 3 deletions
test.py
with
112 additions
and
71 deletions
Rapport.ipynb
+
63
−
58
View file @
59861d42
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
+
39
−
10
View file @
59861d42
...
...
@@ -5,8 +5,20 @@ import matplotlib.pyplot as plt
def
learning_methode
(
k
,
dk
,
learning_rate
):
k
=
k
-
learning_rate
*
dk
#normalisation de k entre [-1,1]
# max_k=np.max(k)
# min_k=np.min(k)
# k=(k*2)/(max_k-min_k)-min_k-1
print
(
np
.
max
(
dk
))
return
(
k
)
def
softmax
(
y
):
y
=
np
.
exp
(
y
)
v
=
np
.
sum
(
y
,
axis
=
1
)
return
(
y
/
v
[:,
np
.
newaxis
])
# def reugalisation(W)
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
...
...
@@ -14,10 +26,17 @@ def learn_once_mse(w1,b1,w2,b2,data,targets,learning_rate):
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)
# s=np.sum(a2,axis=1)
# a2=a2/s[:, np.newaxis]
# print(np.max(a2,axis=1))
#a2=softmax(a2)
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_da2=((np.ones(targets.shape)-2*targets)/(data.shape[0]*a2))
# dc_da2=(-targets)/(data.shape[0]*a2)
#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
)
...
...
@@ -31,10 +50,18 @@ def learn_once_mse(w1,b1,w2,b2,data,targets,learning_rate):
w2
=
learning_methode
(
w2
,
dc_dw2
,
learning_rate
)
b2
=
learning_methode
(
b2
,
dc_db2
,
learning_rate
)
# prediction_2 = np.zeros(predictions.shape, dtype=int)
# for i, ligne in enumerate(predictions):
# prediction_2[i][np.argmin(ligne)] = 1
# indices_egalite = np.where(prediction_2 == targets)[0]
# nombre_indices = len(indices_egalite)
# 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
))
# loss = np.mean(targets*np.log(predictions)-(1-targets)*np.log(1-predictions))
# loss=np.mean(-np.log(np.max(targets*predictions,axis=1)))
# loss=np.mean((np.ones(targets.shape)-2*targets)*np.log(predictions))
return
(
w1
,
b1
,
w2
,
b2
,
loss
)
def
one_hot
(
label
):
...
...
@@ -77,15 +104,18 @@ def run_mlp_training(data_train, labels_train, data_test, labels_test,d_h,learni
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
# 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
=
(
2
*
np
.
random
.
rand
(
d_in
,
d_h
)
-
1
)
# first layer weights
b1
=
2
*
np
.
random
.
rand
(
1
,
d_h
)
-
1
# 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
b2
=
2
*
np
.
random
.
rand
(
1
,
d_out
)
-
1
# 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
)
#test_accuracy2=unit_test(w1,b1,w2,b2,data_test, labels_test)
return
(
loss
,
test_accuracy
)
def
unit_test
(
w1
,
b1
,
w2
,
b2
,
data_test
,
labels_test
):
...
...
@@ -98,7 +128,6 @@ def unit_test(w1,b1,w2,b2,data_test, labels_test):
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
))
...
...
This diff is collapsed.
Click to expand it.
test.py
+
10
−
3
View file @
59861d42
...
...
@@ -20,6 +20,13 @@ import numpy as np
# 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
mat
=
np
.
array
([[
1
,
2
,
3
,
4
],[
6
,
6
,
4
,
4
],[
3
,
2
,
4
,
85
]])
mat_exp
=
np
.
exp
(
mat
)
v
=
np
.
sum
(
mat_exp
,
axis
=
1
)
print
(
v
)
mat_exp_norm
=
mat_exp
/
v
[:,
np
.
newaxis
]
vrai
=
np
.
array
([[
0
,
0
,
0
,
1
],[
1
,
0
,
0
,
0
],[
0
,
0
,
1
,
0
]])
print
(
-
np
.
log
(
np
.
max
(
mat_exp_norm
*
vrai
,
axis
=
1
)))
L
=
np
.
mean
(
-
np
.
log
(
np
.
max
(
vrai
*
mat_exp_norm
,
axis
=
1
)))
print
(
L
)
\ 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