Skip to content
Snippets Groups Projects
Commit 59861d42 authored by Sucio's avatar Sucio
Browse files

maj

parent 32d8ee01
Branches
No related tags found
No related merge requests found
Source diff could not be displayed: it is too large. Options to address this: view the blob.
......@@ -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))
......
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment