How to train a neural network with Keras library in Python

Keras is a Python library that helps developers in making neural networks. This library provides functions that are very feasible in defining the model's architecture. Due to the Keras functions' feasibility, people from various domains use it to train the model on their dataset. In this Answer, we will discuss the steps to train a neural network with the help of this library.

As you know, training neural networks includes pre-processing the data, defining an algorithm, and evaluating the model based on the training.

Neural network process
Neural network process

Model architecture

The model architecture we are going to build is sequential. In this architecture, the neural network is designed with stacked layers. The convolutional neural network and the dense layer network are types of sequential architecture.

model = tf.keras.Sequential()

Preprocessing the data

We are going to use an MNISThand written digits data set. We have normalized the dataset between 0 and 1. Moreover, we have divided the data into two parts - training and testing data. The training data trains the neural network; testing data is used to evaluate the model's performance.

mnist = tf.keras.datasets.mnist
(train_I, train_T), (test_I, test_T) = mnist.load_data()
# Normalize the data
train_I = train_I / 255.0
test_I = test_I / 255.0

Add layers

We defined a neural network model with three layers: input (64 units, ReLU), hidden (128 units, ReLU), and output (10 units, Softmax). It's designed for multi-class classification, particularly recognizing handwritten digits from the MNIST dataset.

model.add(layers.Dense(units=64, activation='relu', input_shape=(input_shape,)))
model.add(layers.Dense(units=128, activation='relu'))
model.add(layers.Dense(units=10, activation='softmax'))

Compile

The code below compiles the model using the Adam optimizer, categorical cross-entropy loss function, and accuracy metric for evaluation.

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

Train

The model is trained using the training data train_data and corresponding labels train_labels. The training is performed in batches of size 32 for 10 epochs. A validation split of 20% is used for validation during training.

model.fit(train_data, train_labels, batch_size=32, epochs=10, validation_split=0.2)

Evaluate

The model is evaluated on the test_data and corresponding labels test_labels. The evaluation results, including the test loss and test accuracy, are stored in test_loss and test_accuracy variables, respectively. Finally, the test accuracy is printed using print("Test Accuracy:", test_accuracy).

test_loss, test_accuracy = model.evaluate(test_data, test_labels)
print("Model Accuracy:", test_accuracy)

Complete model

By summing all the above functions, we have our model built. Run the code below for further understanding.

import tensorflow as tf
from tensorflow.keras import layers

# Load the MNIST dataset
mnist = tf.keras.datasets.mnist
(train_data, train_target), (test_data, test_target) = mnist.load_data()

# Prepare your data
# Normalize the pixel values to range between 0 and 1
train_data = train_data / 255.0
test_data = test_data / 255.0

# Define the architecture of the model
model = tf.keras.Sequential()

# Add layers to the model
model.add(layers.Flatten(input_shape=(28, 28)))  # Flatten the 28x28 image into a 1D array
model.add(layers.Dense(units=128, activation='relu'))
model.add(layers.Dense(units=10, activation='softmax'))

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

# Train the model
model.fit(train_data, train_target, batch_size=32, epochs=5, validation_split=0.2)

# Evaluate the model
test_loss, test_accuracy = model.evaluate(test_data, test_target)
print("Test Accuracy:", test_accuracy)
Training model using Keras

Conclusion

Keras is a popular deep-learning library that simplifies the process of building neural networks. We use Keras to build a neural network for recognizing handwritten digits from the MNIST dataset. We compile the model with the help of the Adam optimizer, categorical cross-entropy loss function, and the accuracy metric. After that, we can train on the training data and evaluate the accuracy of the testing data.

Q

What does the compile() function in Keras do?

A)

Preprocess the input data

B)

Train the model

C)

Evaluate the model on test data

D)

Specify the optimizer, loss function, and metrics for training

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved