Building neural networks with TensorFlow

Artificial Neural Networks, also referred to as neural networks or ANNs, are a subset of machine learning models that are designed to recognize patterns and relationships in data. They are modeled after the human brain and consist of a series of interconnected nodes that process and transmit information.

TensorFlow is an open-source software library created by Google for numerical computation and machine learning. It provides a flexible platform for building and training neural networks, making it the go-to tool for anyone looking to get started with artificial intelligence.

In this tutorial, we will take a deep dive into building neural networks with TensorFlow. We’ll cover the basics of neural networks, explore the TensorFlow framework, and create a simple neural network that can recognize handwritten digits.

Prerequisites

Before we get started, make sure you have the following prerequisites installed:

  • Python 3.x
  • TensorFlow 2.x
  • NumPy

Understanding Neural Networks

Before we dive into building a neural network with TensorFlow, let’s first understand what a neural network is and how it works.

What is a Neural Network?

As previously mentioned, a neural network is designed to recognize patterns and relationships in data. The basic building block of a neural network is called a neuron. Neurons are connected to each other through nodes, which are similar to the synapses in the human brain.

Neurons compute using an input signal and a set of trainable parameters, producing an output signal. Neural networks consist of layers of interconnected neurons, each layer processing the output signals of the previous layer. The final layer produces the output of the neural network, which is the result of the computation.

Types of Neural Networks

There are several types of neural networks, but the most commonly used ones are:

  • Feedforward Neural Networks
  • Convolutional Neural Networks
  • Recurrent Neural Networks

Feedforward Neural Networks

A feedforward neural network is the most basic type of neural network. Data flows from the input layer, through the hidden layers, and to the output layer. The nodes in the hidden layers perform computations on the input and produce an output, which in turn feeds into the next layer.

Convolutional Neural Networks

Convolutional neural networks, or CNNs, are commonly used in image recognition. They use convolutional layers to filter the inputs and identify features in the data. The output of the convolutional layer is then passed through one or more fully connected layers to produce the final output.

Recurrent Neural Networks

Recurrent neural networks, or RNNs, are commonly used in natural language processing and speech recognition. They are designed to process sequences of data, where the output of one state is fed back into the input of the next state. This allows the network to remember previous inputs and output sequences instead of just individual inputs.

How Neural Networks Learn

Neural networks learn by adjusting the values of the parameters in the nodes. The process of adjusting these parameters is called training.

The training process involves providing the neural network with a large amount of labeled data and adjusting the weights of the nodes to minimize the error between the predicted output and the actual output. This is done using an optimization algorithm such as stochastic gradient descent.

There are several other techniques that can be used to improve the learning process, such as regularization, dropout, and batch normalization.

Building Neural Networks with TensorFlow

Now that we have a good understanding of neural networks, let’s start building one with TensorFlow.

Installation

To install TensorFlow, open up a terminal or command prompt and enter the following command:

pip install tensorflow

Creating a Neural Network

Let’s create a simple neural network that can recognize handwritten digits from the MNIST dataset.

Loading the Data

First, we need to load the data. TensorFlow provides a convenient function for loading the MNIST dataset:

import tensorflow as tf
from tensorflow import keras

(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()

This will load the data into four NumPy arrays:

  • x_train – training images
  • y_train – training labels
  • x_test – testing images
  • y_test – testing labels

Preprocessing the Data

Next, we need to preprocess the data. We’ll do this by normalizing the pixel values to be between 0 and 1:

x_train = x_train.astype("float32") / 255
x_test = x_test.astype("float32") / 255

We’ll also convert the labels to one-hot encoding using the to_categorical function:

y_train = keras.utils.to_categorical(y_train)
y_test = keras.utils.to_categorical(y_test)

Defining the Model

Now, we can define the model. We’ll start with a simple feedforward neural network with two hidden layers:

model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),
    keras.layers.Dense(units=128, activation="relu"),
    keras.layers.Dense(units=64, activation="relu"),
    keras.layers.Dense(units=10, activation="softmax")
])

This model has the following layers:

  • Flatten – reshapes the 28×28 input image into a 1D array
  • Dense – a fully connected layer with 128 units and ReLU activation
  • Dense – a fully connected layer with 64 units and ReLU activation
  • Dense – a fully connected layer with 10 units (one for each digit) and softmax activation

Compiling the Model

Now that we’ve defined the model, we need to compile it. We’ll use categorical cross-entropy loss for the loss function and the Adam optimizer:

model.compile(
    loss="categorical_crossentropy",
    optimizer="adam",
    metrics=["accuracy"]
)

Training the Model

Finally, we can train the model. We’ll use a batch size of 32, train for 10 epochs, and use the test set for validation:

model.fit(
    x_train,
    y_train,
    batch_size=32,
    epochs=10,
    validation_data=(x_test, y_test)
)

After training, we can evaluate the model on the test set:

test_loss, test_acc = model.evaluate(x_test, y_test)
print("Test accuracy:", test_acc)

Our final accuracy on the test set should be around 98%.

Conclusion

In this tutorial, we learned the basics of neural networks and how to build one using TensorFlow. We created a simple feedforward neural network to recognize handwritten digits and achieved an accuracy of around 98% on the test set.

TensorFlow provides a powerful platform for building and training neural networks, and with the techniques discussed in this tutorial, you should be able to build more complex models and improve their performance.

Related Post