Keras

Language: Python

ML/AI

Keras was developed by François Chollet and released in March 2015. Its design philosophy focuses on user-friendliness, modularity, and extensibility. Keras became popular for its simple API that abstracts the complexities of deep learning, and it was later integrated tightly with TensorFlow as its official high-level API.

Keras is a high-level neural networks API, written in Python and capable of running on top of TensorFlow, Microsoft Cognitive Toolkit (CNTK), or Theano. It allows for easy and fast prototyping of deep learning models with minimal code.

Installation

pip: pip install keras
conda: conda install -c conda-forge keras

Usage

Keras allows you to define deep learning models using Sequential or Functional APIs. You can build layers, compile models, train with fit(), evaluate, and make predictions easily. It supports a wide variety of layers, optimizers, loss functions, and metrics.

Simple Sequential Model

from tensorflow import keras
from tensorflow.keras import layers

model = keras.Sequential([
    layers.Dense(32, activation='relu', input_shape=(784,)),
    layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

Defines a basic feedforward neural network with one hidden layer and a softmax output layer for classification.

Training a model

import numpy as np
X_train = np.random.rand(100,784)
y_train = np.random.randint(0,10,100)
model.fit(X_train, y_train, epochs=5, batch_size=10)

Trains the Keras model on synthetic data for 5 epochs using a batch size of 10.

Functional API Example

from tensorflow.keras import Input, Model
inputs = Input(shape=(784,))
x = layers.Dense(64, activation='relu')(inputs)
outputs = layers.Dense(10, activation='softmax')(x)
model = Model(inputs=inputs, outputs=outputs)
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

Defines a model using the Functional API, allowing for more flexible architectures than Sequential.

Using Callbacks

from tensorflow.keras.callbacks import EarlyStopping
early_stop = EarlyStopping(monitor='loss', patience=3)
model.fit(X_train, y_train, epochs=50, callbacks=[early_stop])

Demonstrates stopping training early when the loss stops improving to prevent overfitting.

Saving and loading models

model.save('my_keras_model.h5')
new_model = keras.models.load_model('my_keras_model.h5')

Shows how to save a trained Keras model to disk and load it later.

Error Handling

ValueError: Input arrays should have the same number of samples: Ensure that your features and labels arrays have the same number of samples.
InvalidArgumentError: Check that the input shape matches the model's expected input.
ModuleNotFoundError: No module named 'keras': Install Keras using pip or conda in your current Python environment.

Best Practices

Normalize input data for faster convergence.

Use validation sets and callbacks to prevent overfitting.

Leverage the Functional API for complex models like multi-input/multi-output networks.

Use pretrained models from Keras Applications for transfer learning.

Monitor training with TensorBoard for visual insights.