Enhancing MNIST Accuracy with Python Neural Network | by Alef Matias

SeniorTechInfo
2 Min Read

Are you interested in diving into neural networks and exploring the world of deep learning in a cyberpunk style? If so, you’ve come to the right place. In this article, we’re going to walk you through a step-by-step process of building a neural network using Python, numpy, and matplotlib with a cyberpunk theme.

To kick things off, let’s set the stage with some code snippets:


import numpy as np
import matplotlib.pyplot as plt
import mplcyberpunk

plt.rc('figure', figsize=(12, 8))
plt.rcParams['figure.dpi'] = 100

plt.style.use('cyberpunk')

train_data = np.loadtxt('mnist_train.csv', delimiter=',', skiprows=1, dtype=np.float32)
test_data = np.loadtxt('mnist_test.csv', delimiter=',', skiprows=1, dtype=np.float32)

x_train = train_data[:, 1:] / 255.0
y_train = train_data[:, 0].astype(int)

x_test = test_data[:, 1:] / 255.0
y_test = test_data[:, 0].astype(int)

def ReLU(x):
    return np.maximum(0, x)

def deriv_ReLU(x):
    return (x > 0).astype(float)

...
    

Now that we have our foundation set, we can move on to training the neural network. By adjusting parameters such as learning rate and the number of hidden neurons, we can customize the network to our liking. Check out this snippet:


learning_rate = 0.1
hidden_neurons_size = [8, 16, 32, 64, 128]

for idx in hidden_neurons_size:
    train_hidden_weights, train_output_weights, train_hidden_bias, train_output_bias, train_accuracy, train_epochs, train_losses = train(x_train, y_train, alpha=learning_rate, epochs=300, hidden_size=idx, X_test=x_test, Y_test=y_test)

    plt.title(f'Loss over epochs with {idx} hidden neurons')
    plt.xlabel('Epochs')
    plt.ylabel('Loss (Cross Entropy)')
    plt.plot(train_epochs, train_losses, color='red')
    mplcyberpunk.add_glow_effects()
    plt.savefig(f'loss_{idx}_hidden_neurons.png')
    plt.close()
    

As you can see, the possibilities are endless when it comes to experimenting with neural networks. So put on your cyberpunk goggles and dive into the world of deep learning with style.

Share This Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *