Essential Python for Machine Learning: Keras

The Deep Learning Orchestrator

Dagang Wei
5 min readJan 26, 2024
Source: https://keras.io/keras_3/

This article is part of my book Essential Python for Machine Learning.

Introduction

Deep learning is changing the world, but its intricate algorithms can seem daunting for newcomers. Enter Keras, a Python library that simplifies the complex world of deep learning, making it accessible even for those with limited coding experience. In this chapter, we’ll delve into the world of Keras, exploring its essence, advantages, key concepts, and even building a simple handwritten digit recognition model!

What is Keras?

Imagine building a house of cards. Each card represents a complex mathematical operation. While impressive, building something intricate requires meticulous effort. Keras acts like the glue, holding these cards (operations) together in a user-friendly way. It’s a high-level API, meaning you don’t need to deal with the low-level complexities of deep learning frameworks like TensorFlow or PyTorch. You can focus on the big picture, designing your neural network architecture and achieving your machine learning goals.

In particular, Keras 3 is a full rewrite of Keras that enables you to run your Keras workflows on top of either JAX, TensorFlow, or PyTorch, and that unlocks brand new large-scale model training and deployment capabilities. You can pick the framework that suits you best, and switch from one to another based on your current goals. You can also use Keras as a low-level cross-framework language to develop custom components such as layers, models, or metrics that can be used in native workflows in JAX, TensorFlow, or PyTorch — with one codebase.

Why Choose Keras?

Keras boasts several advantages that make it a popular choice for machine learning enthusiasts:

  • Simplicity: Its concise syntax and focus on readability make it easier to learn and write clean code.
  • Flexibility: It seamlessly integrates with various backends like TensorFlow, PyTorch, and JAX, giving you the freedom to choose the best tool for your project.
  • Power: Despite its simplicity, Keras doesn’t compromise on performance. It leverages the computational power of its backends to deliver efficient training and inference.
  • Community: Boasting a vast and active community, Keras offers ample support and resources to help you on your machine learning journey.

Keras vs. Other Deep Learning Frameworks

Here’s a comparison of Keras with other popular deep learning frameworks, along with a concise summary:

Keras

  • Highlight: User-friendliness and rapid prototyping.
  • Strengths: Intuitive API, ease of use, works well with JAX, TensorFlow, and PyTorch backends.
  • Common Uses: Ideal for beginners, standard deep learning tasks, and when quick development is a priority.

TensorFlow

  • Highlight: Powerful, low-level framework for both research and production.
  • Strengths: Granular control, flexibility, scalability, robust deployment options.
  • Common Uses: Complex and custom neural network architectures, research-oriented projects, large-scale deployments.

PyTorch

  • Highlight: Dynamic computational graphs and an imperative programming style.
  • Strengths: Flexibility, ease of debugging, strong community support.
  • Common Uses: Research environments, scenarios where model structure changes during training.

To sum up, Keras excels in accessibility for both beginners and experienced developers who want to get machine learning models up and running quickly. TensorFlow provides extensive low-level control for fine-tuning and complex projects. PyTorch is favored for its flexibility and a more Pythonic feel, often preferred by researchers.

Key Concepts of Keras

Now, let’s peek into the core concepts of Keras:

  • Layers: The building blocks of your neural network, representing different transformations on your data. Keras offers various layers like dense layers for linear operations, convolutional layers for image processing, and recurrent layers for sequential data like text.
  • Models: An arrangement of layers defining the overall architecture of your neural network. Keras allows you to easily stack and connect layers to create complex models.
  • Optimizers: Techniques that adjust the internal parameters of your neural network to improve its performance. Keras provides various optimizers like Adam and SGD.
  • Loss functions: Metrics that measure how well your model’s predictions align with the actual data. Keras offers different loss functions depending on the task, like categorical cross-entropy for classification problems.

Building Your First Model with Keras

Ready to get your hands dirty? Let’s build a simple model that recognizes handwritten digits using the MNIST dataset, a classic example in machine learning.

  1. Import libraries: Start by importing Keras and other necessary libraries like NumPy and matplotlib.
  2. Load data: Load the MNIST dataset, which contains images of handwritten digits and their corresponding labels.
  3. Preprocess data: Normalize the pixel values and reshape the images for compatibility with Keras.
  4. Define the model: Build your neural network architecture using Keras layers. Start with a simple model with a dense layer and Softmax activation for classification.
  5. Compile the model: Choose an optimizer (e.g., Adam) and a loss function (e.g., categorical cross-entropy) to compile your model.
  6. Train the model: Feed the training data to your model and iterate over mini-batches to update its internal parameters.
  7. Evaluate the model: Assess the model’s performance on the test data using metrics like accuracy.

The code is available in this colab notebook.

import numpy as np
import keras
import matplotlib.pyplot as plt

# ---- Load data ----
(train_images, train_labels), (test_images, test_labels) = keras.datasets.mnist.load_data()

# Preprocess data (normalize)
train_images = train_images / 255.0
test_images = test_images / 255.0

# ---- Build the model ----
model = keras.Sequential([
# Input: Transforms images into 1D vectors
keras.layers.Flatten(input_shape=(28, 28)),
# Hidden: 128 neurons for feature learning
keras.layers.Dense(128, activation='relu'),
# Output: Probabilities for each class
keras.layers.Dense(10, activation='softmax')
])

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

# ---- Train ----
history = model.fit(train_images, train_labels, epochs=10)

# ---- Evaluate ----
test_loss, test_acc = model.evaluate(test_images, test_labels)
print('Test Accuracy:', test_acc)

# ---- Visualize ----
# Plot training & validation accuracy history
plt.plot(history.history['accuracy'])
plt.title('Model accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train'], loc='upper left')
plt.show()

# Function to display an image and its prediction
def display_image_and_prediction(index, show_wrong_only=False):
img = test_images[index]
label = test_labels[index]
img_array = np.expand_dims(img, axis=0) # Add an extra dimension for the batch size
prediction = np.argmax(model.predict(img_array, verbose=0))

# Only display wrong predictions
if show_wrong_only and prediction == label:
return

plt.figure(figsize=(2,2))
plt.imshow(img, cmap=plt.cm.binary)
plt.title(f"Actual: {label}, Predicted: {prediction}")
plt.show()

# Show 10 example images and predictions including correct and wrong
for i in range(10):
display_image_and_prediction(i, show_wrong_only=False)

# Show wrong predictions
for i in range(200):
display_image_and_prediction(i, show_wrong_only=True)

Output:

Test Accuracy: 0.978600025177002
Training history
Correct Prediction Example
Wrong Prediction Example

This is a simplified example, but it showcases the ease of building and training models with Keras. As you progress, you can explore more complex architectures, experiment with different layers and hyperparameters, and tackle more challenging machine learning problems.

Conclusion

Keras is an excellent gateway to the fascinating world of machine learning. Its user-friendly interface and powerful capabilities make it ideal for beginners and experienced developers alike. Remember, this is just the beginning. As you explore Keras further, you’ll unlock its full potential and delve into the exciting world of deep learning and artificial intelligence. So, keep learning, keep experimenting, and keep building amazing things with the power of Keras!

--

--