20.4. Building Neural Networks with Keras and TensorFlow
Neural network development has become more accessible with the introduction of high-level libraries like Keras, which operates on top of TensorFlow, a powerful machine learning library. In this chapter, we will explore how to build sequential models in Keras, allowing the creation of deep neural networks in a simplified and efficient way.
Introduction to Keras
Keras is a high-level neural network API written in Python that is capable of running on top of TensorFlow, CNTK or Theano. It was developed with a focus on rapid experimentation and productivity, allowing ideas to be transformed into results with minimal delay. Keras' simplicity and ease of use have made it one of the most popular tools among data scientists and machine learning engineers.
Sequential Models in Keras
A sequential model in Keras is a linear stack of layers. It is the simplest model type and is suitable for most deep learning problems. With it, you can create a neural network by adding layers one after another sequentially.
Installing Keras and TensorFlow
Before you start building a model, you need to install Keras and TensorFlow. This can be easily done using pip, the Python package manager:
pip install tensorflow
pip install keras
After installation, you can import Keras and check the TensorFlow version to ensure everything is configured correctly:
import tensorflow as tf
import keras
print(tf.__version__)
print(keras.__version__)
Building a Sequential Model
To build a sequential model in Keras, you start by importing the Sequential
class and adding layers using the add()
method. Let's build a simple model for image classification:
from keras.models import Sequential
from keras.layers import Dense, Flatten, Conv2D, MaxPooling2D
# Initialize the sequential model
model = Sequential()
# Add a convolutional layer
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
# Add a pooling layer
model.add(MaxPooling2D((2, 2)))
# Flatten the output to provide input to the dense layers
model.add(Flatten())
# Add a dense layer (fully-connected)
model.add(Dense(100, activation='relu'))
# Add output layer
model.add(Dense(10, activation='softmax'))
This model consists of a convolutional layer followed by a pooling layer, a flattening layer, and two dense layers. The last dense layer uses the softmax activation function to obtain classification probabilities for each of the 10 possible classes.
Compiling the Model
After building the model, you need to compile it. This is done using the compile()
method, where you define the loss function, optimizer and evaluation metrics:
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
The categorical_crossentropy
loss function is commonly used in multiclass classification problems. The adam
optimizer is a popular choice due to its efficiency and automatic learning rate adjustment.
Training the Model
With the model compiled, you can train it using the fit()
method. You will need to provide the training data, labels, number of epochs, and batch size:
history = model.fit(train_data, train_labels, epochs=10, batch_size=32)
The history
object contains information about the training process, such as loss and accuracy at each epoch, which can be used for analysis and model tuning.
Evaluating and Saving the Model
After training, you can evaluate the model's performance on test data using the evaluate()
method:
test_loss, test_acc = model.evaluate(test_data, test_labels)
print('Test accuracy:', test_acc)
If you are satisfied with the model's performance, you can save it using the save()
method, allowing you to load it later to make predictions or continue training:
model.save('my_model.h5')
Conclusion
Building neural networks with Keras and TensorFlow is a straightforward and efficient process. Keras' sequential API allows you to build models layer by layer, while TensorFlow provides the robustness and scalability needed to train complex models. With the coverable to easily compile, train, evaluate, and save models, Keras is an indispensable tool for anyone interested in deep learning with Python.