diff --git a/mlp.py b/mlp.py index 4bc529546eb4b0b33da184569f960c1b8d4638d1..3a8f212e144e57ea473e020687e2f2334736123c 100644 --- a/mlp.py +++ b/mlp.py @@ -166,6 +166,14 @@ def one_hot(array): # 12. learn_once_cross_entropy +def compute_loss_entropy(target, Y): + from sklearn.metrics import log_loss + + entropy_loss = log_loss(target, Y) + + return entropy_loss + + def learn_once_cross_entropy( w1, w2, b1, b2, data_train, labels_train, learning_rate=0.00001 ): @@ -255,8 +263,8 @@ def learn_once_cross_entropy( ) # output of the output layer (sigmoid activation function) predictions = a2 # the predicted values are the outputs of the output layer - # Compute loss (MSE) - loss = np.mean(np.square(predictions - one_hot(labels_train))) + # Compute cross_entropy loss + loss = compute_loss_entropy(one_hot(labels_train), predictions) print(loss) return w1, w2, b1, b2, loss @@ -371,8 +379,8 @@ def train_mlp( ) # output of the output layer (sigmoid activation function) predictions = a2 # the predicted values are the outputs of the output layer - # Compute loss (MSE) - loss = np.mean(np.square(predictions - one_hot(labels_train))) + # Compute cross_entropy loss + loss = compute_loss_entropy(one_hot(labels_train), predictions) losses.append(loss) # Compute accuracy diff --git a/results/mlp_loss.png b/results/mlp_loss.png index e81bd776d4f303e838b4b041587e92cef67ea894..cec90e53482a06848a8acd9551b73878f36d8d54 100644 Binary files a/results/mlp_loss.png and b/results/mlp_loss.png differ