Skip to content
Snippets Groups Projects
Commit 00beb9f4 authored by Duperret Loris's avatar Duperret Loris
Browse files

Last commit

parent 7612de50
No related branches found
No related tags found
1 merge request!3Master
No preview for this file type
No preview for this file type
No preview for this file type
...@@ -14,7 +14,7 @@ batch_path = "data/cifar-10-python\cifar-10-batches-py" ...@@ -14,7 +14,7 @@ batch_path = "data/cifar-10-python\cifar-10-batches-py"
data, labels = read_cifar.read_cifar(batch_path) data, labels = read_cifar.read_cifar(batch_path)
data_train, labels_train, data_test, labels_test = read_cifar.split_dataset(data, labels, split) data_train, labels_train, data_test, labels_test = read_cifar.split_dataset(data, labels, split)
k_values = range(1, 21) """k_values = range(1, 21)
accuracies = [] accuracies = []
times = [] times = []
...@@ -45,7 +45,7 @@ plt.ylabel('time') ...@@ -45,7 +45,7 @@ plt.ylabel('time')
plt.xticks(k_values) plt.xticks(k_values)
plt.grid(True) plt.grid(True)
plt.savefig('results/time_knn.png') plt.savefig('results/time_knn.png')
plt.show() plt.show()"""
train_accuracies,test_accuracy = mlp.run_mlp_training(data_train, labels_train, data_test, labels_test, d_h, learning_rate, num_epochs) train_accuracies,test_accuracy = mlp.run_mlp_training(data_train, labels_train, data_test, labels_test, d_h, learning_rate, num_epochs)
...@@ -58,3 +58,4 @@ def plot_learning_accuracy(train_accuracies): ...@@ -58,3 +58,4 @@ def plot_learning_accuracy(train_accuracies):
plt.savefig("results/mlp.png") plt.savefig("results/mlp.png")
plot_learning_accuracy(train_accuracies) plot_learning_accuracy(train_accuracies)
...@@ -11,28 +11,28 @@ def learn_once_mse(w1, b1, w2, b2, data, targets, learning_rate): ...@@ -11,28 +11,28 @@ def learn_once_mse(w1, b1, w2, b2, data, targets, learning_rate):
z1 = np.matmul(a0, w1) + b1 z1 = np.matmul(a0, w1) + b1
a1 = sigmoid(z1) a1 = sigmoid(z1)
z2 = np.matmul(a1, w2) + b2 z2 = np.matmul(a1, w2) + b2
a2 = sigmoid(z2) a2 = softmax(z2)
predictions = a2 predictions = a2
# MSE # MSE
loss = np.mean(np.square(predictions - targets)) loss = np.mean(np.square(predictions - targets))
# Backpropagation # Backpropagation
delta_a2 = 2 * (predictions - targets) / data.shape[0] delta_a2 = 2 * (predictions.T - targets) / data.shape[0]
delta_z2 = delta_a2 * sigmoid_derivative(a2) delta_z2 = delta_a2 * sigmoid_derivative(a2)
delta_a1 = np.matmul(delta_z2, w2.T) delta_a1 = np.matmul(delta_z2, w2.T)
delta_z1 = delta_a1 * sigmoid_derivative(a1) delta_z1 = delta_a1 * sigmoid_derivative(a1)
# Mise à jour weight et biais # Mise à jour weight et biais
w2 -= learning_rate * np.matmul(a1.T, delta_z2) w2 -= learning_rate * np.matmul(a1.T, delta_z2) / data.shape[0]
b2 -= learning_rate * np.sum(delta_z2, axis=0, keepdims=True) b2 -= learning_rate * np.mean(delta_z2, axis=0)
w1 -= learning_rate * np.matmul(a0.T, delta_z1) w1 -= learning_rate * np.dot(a0.T, delta_z1) / data.shape[0]
b1 -= learning_rate * np.sum(delta_z1, axis=0, keepdims=True) b1 -= learning_rate * np.mean(delta_z1, axis=0)
return w1, b1, w2, b2, loss return w1, b1, w2, b2, loss
def softmax(z): #evite les instabilités de loss def softmax(z): #evite les instabilités de loss
exp_z = np.exp(z - np.max(z, axis=1, keepdims=True)) exp_z = np.exp(z)
return exp_z / np.sum(exp_z, axis=1, keepdims=True) return exp_z / np.sum(exp_z, axis=1, keepdims=True)
...@@ -55,20 +55,18 @@ def learn_once_cross_entropy(w1, b1, w2, b2, data, labels_train, learning_rate): ...@@ -55,20 +55,18 @@ def learn_once_cross_entropy(w1, b1, w2, b2, data, labels_train, learning_rate):
# Calcule cross entropy # Calcule cross entropy
m = len(labels_train) m = len(labels_train)
one_hot_labels = one_hot(labels_train) one_hot_labels = one_hot(labels_train)
epsilon = 1e-9
predictions = np.clip(predictions, epsilon, 1 - epsilon) #évite les instabilités de loss
loss = -np.mean(one_hot_labels * np.log(predictions)) loss = -np.mean(one_hot_labels * np.log(predictions))
# Backpropagation # Backpropagation
delta_z2 = a2 - one_hot_labels delta_z2 = a2 - one_hot_labels
delta_a1 = np.matmul(delta_z2, w2.T) delta_a1 = np.dot(delta_z2, w2.T)
delta_z1 = delta_a1 * sigmoid_derivative(a1) delta_z1 = delta_a1 * sigmoid_derivative(a1)
# Mise à jour weight et biais # Mise à jour weight et biais
w2 -= learning_rate * np.matmul(a1.T, delta_z2) w2 -= learning_rate * np.matmul(a1.T, delta_z2) / data.shape[0]
b2 -= learning_rate * np.sum(delta_z2, axis=0, keepdims=True) b2 -= learning_rate * np.mean(delta_z2, axis=0)
w1 -= learning_rate * np.matmul(a0.T, delta_z1) w1 -= learning_rate * np.dot(a0.T, delta_z1) / data.shape[0]
b1 -= learning_rate * np.sum(delta_z1, axis=0, keepdims=True) b1 -= learning_rate * np.mean(delta_z1, axis=0)
return w1, b1, w2, b2, loss return w1, b1, w2, b2, loss
......
results/mlp.png

49 KiB | W: | H:

results/mlp.png

24.3 KiB | W: | H:

results/mlp.png
results/mlp.png
results/mlp.png
results/mlp.png
  • 2-up
  • Swipe
  • Onion skin
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment