Skip to content
Snippets Groups Projects
Commit 72a9c57e authored by Barry Timothee's avatar Barry Timothee
Browse files

fix: change sigmoid with softmax and add some comments

parent e24774ef
Branches main
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.
images/mlp_accuracy.png

28.5 KiB

images/mlp_accuracy_tf.png

26.4 KiB

images/mlp_loss.png

22.5 KiB | W: | H:

images/mlp_loss.png

25.8 KiB | W: | H:

images/mlp_loss.png
images/mlp_loss.png
images/mlp_loss.png
images/mlp_loss.png
  • 2-up
  • Swipe
  • Onion skin
images/mlp_loss_tf.png

20.7 KiB | W: | H:

images/mlp_loss_tf.png

25.2 KiB | W: | H:

images/mlp_loss_tf.png
images/mlp_loss_tf.png
images/mlp_loss_tf.png
images/mlp_loss_tf.png
  • 2-up
  • Swipe
  • Onion skin
from utils.sigmoid import sigmoid from utils.sigmoid import sigmoid
import numpy as np import numpy as np
from scipy.special import softmax
def forward_pass(w1, b1, w2, b2, data): def forward_pass(w1, b1, w2, b2, data):
# compute the forward pass of the MLP with sigmoid activations # compute the forward pass of the MLP with sigmoid activations for the hidden layer and softmax for the output layer
z1 = np.matmul(data, w1) + b1 z1 = np.matmul(data, 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, axis=1)
return a1, a2 return a1, a2
\ No newline at end of file
...@@ -6,16 +6,19 @@ from utils.learn_once_cross_entropy import learn_once_cross_entropy ...@@ -6,16 +6,19 @@ from utils.learn_once_cross_entropy import learn_once_cross_entropy
def train_mlp(w1, b1, w2, b2, data_train, labels_train, learning_rate, num_epochs, batch_size, n_classes): def train_mlp(w1, b1, w2, b2, data_train, labels_train, learning_rate, num_epochs, batch_size, n_classes):
# train the MLP for num_epochs epochs, using batches of size batch_size # train the MLP for num_epochs epochs, using batches of size batch_size and return the train accuracies, losses and weights
losses = [] losses = []
train_accuracies = []
for epoch in range(num_epochs): for epoch in range(num_epochs):
for i in tqdm.tqdm(range(0, data_train.shape[0], batch_size)): for i in tqdm.tqdm(range(0, data_train.shape[0], batch_size)):
data = data_train[i:i+batch_size] data = data_train[i:i+batch_size]
targets = one_hot(labels_train[i:i+batch_size], n_classes) targets = one_hot(labels_train[i:i+batch_size], n_classes)
w1, b1, w2, b2, loss = learn_once_cross_entropy(w1, b1, w2, b2, data, targets, learning_rate) w1, b1, w2, b2, loss = learn_once_cross_entropy(w1, b1, w2, b2, data, targets, learning_rate)
losses.append(loss) losses.append(loss)
print(f'epoch={epoch}, loss={loss}') train_accuracy = test_mlp(w1, b1, w2, b2, data_train, labels_train)
return losses, w1, b1, w2, b2 train_accuracies.append(train_accuracy)
print(f'epoch={epoch}, loss={loss}, train_accuracy={train_accuracy}')
return train_accuracies, losses, w1, b1, w2, b2
def test_mlp(w1, b1, w2, b2, data_test, labels_test): def test_mlp(w1, b1, w2, b2, data_test, labels_test):
# test the MLP on data_test, and return the accuracy # test the MLP on data_test, and return the accuracy
...@@ -37,6 +40,6 @@ def run_mlp_training(data_train, labels_train, data_test, labels_test, d_h, lear ...@@ -37,6 +40,6 @@ def run_mlp_training(data_train, labels_train, data_test, labels_test, d_h, lear
d_in = data_train.shape[1] d_in = data_train.shape[1]
d_out = np.max(labels_train) + 1 d_out = np.max(labels_train) + 1
w1, b1, w2, b2 = initialize_mlp(d_in, d_h, d_out) w1, b1, w2, b2 = initialize_mlp(d_in, d_h, d_out)
losses, w1, b1, w2, b2 = train_mlp(w1, b1, w2, b2, data_train, labels_train, learning_rate, num_epochs, batch_size, n_classes) train_accuracies, losses, w1, b1, w2, b2 = train_mlp(w1, b1, w2, b2, data_train, labels_train, learning_rate, num_epochs, batch_size, n_classes)
test_accuracy = test_mlp(w1, b1, w2, b2, data_test, labels_test) test_accuracy = test_mlp(w1, b1, w2, b2, data_test, labels_test)
return losses, test_accuracy return losses, test_accuracy, train_accuracies
\ No newline at end of file \ No newline at end of file
...@@ -5,10 +5,11 @@ def plot_image_with_label(img, label): ...@@ -5,10 +5,11 @@ def plot_image_with_label(img, label):
plt.title(label) plt.title(label)
plt.show() plt.show()
def save_plot_as_image(X, Y, y_label, x_label, save_path): def save_plot_as_image(X, Y, y_label, x_label, title, save_path):
# plot and save image as png # plot and save image as png
plt.figure(figsize=(10,5)) plt.figure(figsize=(10,5))
plt.plot(X, Y) plt.plot(X, Y)
plt.title(title)
plt.ylabel(y_label) plt.ylabel(y_label)
plt.xlabel(x_label) plt.xlabel(x_label)
plt.savefig(save_path) plt.savefig(save_path)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment