Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Python Advanced - Deep Learning with Keras

Implementing deep learning models with Keras in Python

Keras is a high-level deep learning API written in Python, running on top of TensorFlow. It allows for easy and fast prototyping of deep learning models. This tutorial explores how to use Keras to implement various deep learning models in Python.

Key Points:

  • Keras is a high-level API for building deep learning models.
  • Keras provides simple and flexible tools for creating, training, and evaluating models.
  • Keras integrates well with TensorFlow and other data science libraries.

Installing Keras

Keras is included in TensorFlow, so you need to install TensorFlow using pip:


pip install tensorflow
            

Creating a Simple Neural Network

You can create a simple neural network using Keras. Here is an example of building and training a neural network for classifying the Iris dataset:


import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder
import numpy as np

# Loading the Iris dataset
iris = load_iris()
X = iris.data
y = iris.target.reshape(-1, 1)

# One-hot encoding the target variable
encoder = OneHotEncoder(sparse=False)
y = encoder.fit_transform(y)

# Splitting the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Creating a neural network model
model = Sequential([
    Dense(10, activation='relu', input_shape=(X.shape[1],)),
    Dense(10, activation='relu'),
    Dense(y.shape[1], activation='softmax')
])

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

# Training the model
model.fit(X_train, y_train, epochs=50, batch_size=5, validation_split=0.2)

# Evaluating the model
loss, accuracy = model.evaluate(X_test, y_test)
print("Test Accuracy:", accuracy)
            

In this example, a simple neural network is created with two hidden layers, trained on the Iris dataset, and evaluated on the testing data.

Building a Convolutional Neural Network (CNN)

You can build a Convolutional Neural Network (CNN) for image classification using Keras. Here is an example of building and training a CNN on the MNIST dataset:


import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical

# Loading the MNIST dataset
(X_train, y_train), (X_test, y_test) = mnist.load_data()

# Reshaping and normalizing the data
X_train = X_train.reshape(-1, 28, 28, 1).astype('float32') / 255.0
X_test = X_test.reshape(-1, 28, 28, 1).astype('float32') / 255.0

# One-hot encoding the target variable
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)

# Creating a CNN model
model = Sequential([
    Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)),
    MaxPooling2D(pool_size=(2, 2)),
    Conv2D(64, kernel_size=(3, 3), activation='relu'),
    MaxPooling2D(pool_size=(2, 2)),
    Flatten(),
    Dense(128, activation='relu'),
    Dense(10, activation='softmax')
])

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

# Training the model
model.fit(X_train, y_train, epochs=10, batch_size=128, validation_split=0.2)

# Evaluating the model
loss, accuracy = model.evaluate(X_test, y_test)
print("Test Accuracy:", accuracy)
            

In this example, a CNN is created with two convolutional layers and trained on the MNIST dataset for digit classification.

Using Callbacks

Keras provides callbacks for customizing the training process, such as early stopping and learning rate scheduling. Here is an example of using the EarlyStopping callback:


import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.callbacks import EarlyStopping
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder
import numpy as np

# Loading the Iris dataset
iris = load_iris()
X = iris.data
y = iris.target.reshape(-1, 1)

# One-hot encoding the target variable
encoder = OneHotEncoder(sparse=False)
y = encoder.fit_transform(y)

# Splitting the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Creating a neural network model
model = Sequential([
    Dense(10, activation='relu', input_shape=(X.shape[1],)),
    Dense(10, activation='relu'),
    Dense(y.shape[1], activation='softmax')
])

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

# Creating an EarlyStopping callback
early_stopping = EarlyStopping(monitor='val_loss', patience=5)

# Training the model with the EarlyStopping callback
model.fit(X_train, y_train, epochs=50, batch_size=5, validation_split=0.2, callbacks=[early_stopping])

# Evaluating the model
loss, accuracy = model.evaluate(X_test, y_test)
print("Test Accuracy:", accuracy)
            

In this example, the EarlyStopping callback is used to stop training when the validation loss does not improve for 5 consecutive epochs.

Transfer Learning

Transfer learning involves using a pre-trained model and fine-tuning it for a specific task. Here is an example of using the pre-trained MobileNetV2 model for image classification:


import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
from tensorflow.keras.applications import MobileNetV2
from tensorflow.keras.preprocessing.image import ImageDataGenerator

# Loading the pre-trained MobileNetV2 model
base_model = MobileNetV2(weights='imagenet', include_top=False, input_shape=(224, 224, 3))

# Freezing the base model
base_model.trainable = False

# Creating a new model with MobileNetV2 as the base
model = Sequential([
    base_model,
    GlobalAveragePooling2D(),
    Dense(128, activation='relu'),
    Dense(10, activation='softmax')
])

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

# Loading and preprocessing the data (example with ImageDataGenerator)
datagen = ImageDataGenerator(rescale=1./255)
train_generator = datagen.flow_from_directory('path/to/train/data', target_size=(224, 224), batch_size=32, class_mode='categorical')
validation_generator = datagen.flow_from_directory('path/to/validation/data', target_size=(224, 224), batch_size=32, class_mode='categorical')

# Training the model
model.fit(train_generator, epochs=10, validation_data=validation_generator)
            

In this example, the MobileNetV2 model is used as the base model, and a new model is built on top of it for image classification.

Model Evaluation and Visualization

Keras provides tools for evaluating and visualizing models, including accuracy, loss, and confusion matrices. Here is an example of evaluating and visualizing a model:


import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.utils import plot_model
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay

# Loading the Iris dataset
iris = load_iris()
X = iris.data
y = iris.target.reshape(-1, 1)

# One-hot encoding the target variable
encoder = OneHotEncoder(sparse=False)
y = encoder.fit_transform(y)

# Splitting the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Creating a neural network model
model = Sequential([
    Dense(10, activation='relu', input_shape=(X.shape[1],)),
    Dense(10, activation='relu'),
    Dense(y.shape[1], activation='softmax')
])

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

# Training the model
model.fit(X_train, y_train, epochs=50, batch_size=5, validation_split=0.2)

# Evaluating the model
loss, accuracy = model.evaluate(X_test, y_test)
print("Test Accuracy:", accuracy)

# Visualizing the model architecture
plot_model(model, to_file='model.png', show_shapes=True, show_layer_names=True)

# Making predictions
y_pred = model.predict(X_test)
y_pred_classes = np.argmax(y_pred, axis=1)
y_true = np.argmax(y_test, axis=1)

# Generating a confusion matrix
cm = confusion_matrix(y_true, y_pred_classes)
disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=iris.target_names)
disp.plot()
plt.show()
            

In this example, the model is evaluated on the test data, its architecture is visualized, and a confusion matrix is generated to evaluate the model's performance.

Summary

In this tutorial, you learned about implementing deep learning models with Keras in Python. Keras provides simple and flexible tools for creating, training, and evaluating deep learning models. You explored creating neural networks, convolutional neural networks, using callbacks, transfer learning, and model evaluation and visualization. Understanding Keras is essential for developing advanced machine learning and deep learning applications in Python.