AI Unlocked - Episode 2: Building Your First Neural Network in Python

Step-by-step guide on building and training a simple neural network using TensorFlow and Keras.

AI Unlocked - Episode 2: Building Your First Neural Network in Python
Neural Network

  • Focus: Step-by-step guide on building and training a simple neural network using TensorFlow and Keras.
  • Key Learning Objectives:
    • Understanding neural networks
    • Building a neural network architecture
    • Training the model on a real dataset (e.g., MNIST)
    • Evaluating model performance


Welcome back to AI Guide Space! In the last episode, we introduced AI fundamentals. Today, we're going deeper into the exciting world of neural networks. We’ll build and train a neural network from scratch, so let’s jump right into it!


INTRO TO NEURAL NETWORKS

Before we write code, let’s take a moment to understand how neural networks work. At a high level, a neural network consists of layers of nodes, or ‘neurons.’ These neurons are connected, and data passes through the network, getting adjusted through weights and biases to make predictions.


[SEGMENT 2: SETTING UP YOUR ENVIRONMENT]

Alright, let’s start by setting up our environment. You’ll need Python installed, along with the TensorFlow and NumPy libraries. Open up your terminal and type the following:

pip install tensorflow numpy matplotlib

LOADING THE DATA

For this project, we’ll use the MNIST dataset, which contains handwritten digits. TensorFlow makes it easy to load datasets like MNIST. Let’s load the training and testing data:

import tensorflow as tf from tensorflow 
import keras 
# Load dataset 
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data() 
# Normalize the data 
x_train, x_test = x_train / 255.0, x_test / 255.0


CREATING THE NEURAL NETWORK

Now, let's create the architecture of our neural network. We’ll use a simple model with three layers: an input layer, one hidden layer, and an output layer for digit classification.”


model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),  # Flattening the input data
    keras.layers.Dense(128, activation='relu'),  # Hidden layer with 128 neurons
    keras.layers.Dense(10, activation='softmax')  # Output layer for 10 classes (digits 0-9)
])

COMPILING THE MODEL

Now that we’ve defined our model, let’s compile it. We’ll use the Adam optimizer and categorical cross-entropy loss function, which is ideal for multi-class classification problems.

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])


TRAINING THE MODEL

Let’s train the model on our dataset. We’ll train it for 5 epochs, and you’ll see the progress as the model learns to recognize the digits.

model.fit(x_train, y_train, epochs=5)

EVALUATING THE MODEL

Once the model is trained, let’s evaluate how well it performs on unseen data – our test set.

test_loss, test_acc = model.evaluate(x_test, y_test)
print(f'Test accuracy: {test_acc}')

Congratulations! You've just built and trained your first neural network. The model is now capable of recognizing handwritten digits. Keep experimenting with different datasets and architectures! If you enjoyed this tutorial, don’t forget to like, subscribe, and leave any questions in the comments below. See you next time on AI Guide Space!