Neural networks Keras Python Deep learning Convolutional neural networks MNIST dataset Data preprocessing Model training

Building Neural Networks with Keras: A Step-by-Step Tutorial

2023-05-01 11:09:52

//

9 min read

Blog article placeholder

Building Neural Networks with Keras: A Step-by-Step Tutorial

Neural networks have become an essential tool for many industries, from healthcare to finance. They can learn to recognize complex patterns and make predictions based on input data. Keras is a popular high-level neural networks API written in Python. It makes it easy to build advanced deep learning models, requiring minimal code. In this tutorial, we will explore how to build and train a neural network with Keras.

Prerequisite

Before we begin, it is essential to have familiarity with the following topics:

  • Python
  • Neural networks
  • Keras
  • Tensorflow

Install Required Packages

First, we need to make sure we have all the required packages installed. Open your terminal and run the following command:

pip install keras tensorflow matplotlib numpy pandas

Import Packages

After installing the packages, let's import them in our Python script:

import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K

import numpy as np
import matplotlib.pyplot as plt

We imported Keras and other necessary packages. They include:

  • keras.datasets.mnist: This contains a set of handwritten digits images that we will use to train our neural network.
  • keras.models.Sequential: This is the most common Keras model, a linear stack of layers. We will use it to build our neural network.
  • keras.layers.Dense: This is a neural network layer consisting of densely connected nodes.
  • keras.layers.Flatten: This layer flattens the input, which is necessary for our neural network.
  • keras.layers.Conv2D: This layer creates a convolution kernel that is convolved with the input layer to produce a tensor of outputs.
  • keras.layers.MaxPooling2D: This layer down-samples input along its spatial dimensions.
  • keras.backend: This provides an abstraction over different available backends for Keras, such as TensorFlow.

Load the Data

Now let's load the MNIST dataset into our Python script:

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

This will load the dataset into four NumPy arrays: x_train, y_train, x_test, and y_test. The x arrays contain the input data - images of digits, and the y arrays contain the labels for each image.

Data Preprocessing

Before we can train our neural network, we need to preprocess the data:

img_rows, img_cols = 28, 28

if K.image_data_format() == 'channels_first':
    x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
    x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
    input_shape = (1, img_rows, img_cols)
else:
    x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
    x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
    input_shape = (img_rows, img_cols, 1)

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

num_classes = 10
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

We will first normalize the input data, divide by 255. Since the pixel values of our images are between 0 and 255, we divide by 255 to make them between 0 and 1.

We will reshape our input data to be suitable for our neural network. Also, we will convert the labels to one-hot encoded format.

Build the Model

Now that we have prepared the data let's build our neural network:

model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
                 activation='relu',
                 input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))

model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.Adadelta(),
              metrics=['accuracy'])

Our neural network consists of:

  • Two convolutional layers with 32 and 64 filters, respectively, and each with a kernel size of 3x3 pixels.
  • A max-pooling layer with a pool size of 2x2 pixels.
  • Two dropout layers that randomly deactivate some neurons, preventing overfitting.
  • A dense layer with 128 neurons, which transforms the input into a vector of size 128.
  • A final densely connected layer with 10 nodes, each representing one digit.

We compile our model using the categorical_crossentropy loss function and Adadelta optimizer. We also evaluate our model based on accuracy.

Train the Model

Finally, we will run the training of our neural network:

batch_size = 128
epochs = 10

history = model.fit(x_train, y_train,
                    batch_size=batch_size,
                    epochs=epochs,
                    verbose=1,
                    validation_data=(x_test, y_test))

Our neural network will train for 10 epochs, with a batch size of 128. After the end of the training, we would evaluate the model accuracy and visualize the training and validation loss.

Conclusion

In this tutorial, we learned how to build and train a neural network with Keras. We also demonstrated how to preprocess the data, build a neural network, and train it using the MNIST dataset. From here, you can modify the code to build your own neural network models, with more complex architectures, and train them on your own datasets. Happy coding!

Related posts