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.
pip install kerasconda install -c conda-forge kerasKeras 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.
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.
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.
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.
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.
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.
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.