Keras: Deep Learning for Humans
Keras is an open-source neural network library written in Python. It is designed to enable fast experimentation with deep neural networks and focuses on being user-friendly, modular, and extensible. Keras acts as an interface for the TensorFlow library (and previously others like Theano or CNTK), making it easier to build and train deep learning models.
Key Features:
- User-friendly: Simple and consistent API designed for human intuition.
- Modular: Models are built by connecting configurable building blocks.
- Extensible: Easy to add new modules, layers, and activation functions.
- Python-centric: Native Python code, allowing for easy debugging.
- Backend Agnostic: Can run on top of TensorFlow, Theano, or CNTK (primarily TensorFlow now).
- Pre-trained Models: Provides access to a wide array of pre-trained models for transfer learning.
Getting Started: Installation
Keras is typically installed as part of TensorFlow 2.x. If you install TensorFlow, Keras is included.
Using pip (for TensorFlow with Keras):
pip install tensorflow
If you specifically want a standalone Keras (which still uses TensorFlow as a backend by default), you can install it separately:
pip install keras
Basic Concepts: Models, Layers, and Compilation
Keras models are built up from layers, which are the fundamental building blocks of neural networks.
Sequential Model
The Sequential model is a linear stack of layers. It's suitable for simple models where the output of one layer is the input to the next.
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
# 1. Define a Sequential model
model = keras.Sequential([
# Input layer and first hidden layer with 64 neurons and ReLU activation
layers.Dense(64, activation='relu', input_shape=(784,)),
# Second hidden layer
layers.Dense(64, activation='relu'),
# Output layer with 10 neurons for a 10-class classification problem
layers.Dense(10, activation='softmax')
])
# 2. Compile the model
# - optimizer: How the model is updated based on the data it sees and its loss function.
# - loss: Measures how accurate the model is during training.
# - metrics: Used to monitor the training and testing steps.
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# Display a summary of the model's architecture
model.summary()
Functional API
For more complex models with multiple inputs, multiple outputs, or shared layers, Keras offers the Functional API.
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
# Input layer
input_tensor = keras.Input(shape=(784,))
# Hidden layers
x = layers.Dense(64, activation='relu')(input_tensor)
x = layers.Dense(64, activation='relu')(x)
# Output layer
output_tensor = layers.Dense(10, activation='softmax')(x)
# Create the model
model_functional = keras.Model(inputs=input_tensor, outputs=output_tensor)
# Compile the model
model_functional.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model_functional.summary()
Training a Model (Example with MNIST)
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import numpy as np
# Load and prepare the MNIST dataset
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
# Flatten images from (28, 28) to (784,) and normalize
x_train = x_train.reshape(-1, 784).astype('float32') / 255.0
x_test = x_test.reshape(-1, 784).astype('float32') / 255.0
# Define the model
model = keras.Sequential([
layers.Dense(128, activation='relu', input_shape=(784,)),
layers.Dropout(0.2), # Dropout for regularization
layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# Train the model
print("\nTraining the model...")
history = model.fit(x_train, y_train, epochs=5, batch_size=32, validation_split=0.1)
# Evaluate the model
print("\nEvaluating the model...")
test_loss, test_acc = model.evaluate(x_test, y_test, verbose=0)
print(f"Test accuracy: {test_acc:.4f}")
Further Topics:
- Different Layer Types (Convolutional, Recurrent, Embedding, etc.)
- Custom Layers and Models
- Callbacks (Early Stopping, Model Checkpointing)
- Data Preprocessing and Augmentation
- Transfer Learning
- Saving and Loading Models
- Hyperparameter Tuning with Keras Tuner
This document provides a basic introduction to Keras. More detailed topics, advanced model architectures, and practical examples will be covered in subsequent files.