Multi-backend deep learning framework that provides a unified, high-level API for building and training neural networks across JAX, TensorFlow, PyTorch, and OpenVINO backends.
—
Core model types and architecture patterns for building neural networks in Keras. Models are containers that organize layers and define the training process through compilation and fitting methods.
The fundamental Model class that provides the core functionality for all Keras models, including compilation, training, evaluation, and prediction.
class Model:
def __init__(self, inputs=None, outputs=None, name=None, **kwargs):
"""
Create a Model from functional API inputs/outputs or subclass.
Parameters:
- inputs: Input tensor(s) for functional API models
- outputs: Output tensor(s) for functional API models
- name: Name of the model
"""
def compile(self, optimizer='rmsprop', loss=None, metrics=None,
loss_weights=None, weighted_metrics=None, **kwargs):
"""
Configure the model for training.
Parameters:
- optimizer: String or optimizer instance
- loss: String or loss function
- metrics: List of metrics to monitor
- loss_weights: Optional list/dict of weights for loss functions
- weighted_metrics: List of metrics to apply sample weighting to
"""
def fit(self, x=None, y=None, batch_size=None, epochs=1, verbose='auto',
callbacks=None, validation_split=0.0, validation_data=None,
shuffle=True, class_weight=None, sample_weight=None, **kwargs):
"""
Train the model for a fixed number of epochs.
Parameters:
- x: Input data
- y: Target data
- batch_size: Number of samples per gradient update
- epochs: Number of epochs to train
- verbose: Verbosity mode (0, 1, 2, or 'auto')
- callbacks: List of callbacks to apply during training
- validation_split: Fraction of data to use for validation
- validation_data: Data on which to evaluate loss and metrics
- shuffle: Whether to shuffle the training data
- class_weight: Dict mapping class indices to weights
- sample_weight: Array of weights for training samples
Returns:
History object containing training metrics
"""
def evaluate(self, x=None, y=None, batch_size=None, verbose='auto',
sample_weight=None, **kwargs):
"""
Evaluate the model on test data.
Parameters:
- x: Input data
- y: Target data
- batch_size: Number of samples per batch
- verbose: Verbosity mode
- sample_weight: Array of weights for samples
Returns:
Scalar test loss or list of scalars
"""
def predict(self, x, batch_size=None, verbose='auto', **kwargs):
"""
Generate predictions for input samples.
Parameters:
- x: Input data
- batch_size: Number of samples per batch
- verbose: Verbosity mode
Returns:
Numpy array(s) of predictions
"""
def save(self, filepath, overwrite=True, **kwargs):
"""
Save the model to a file.
Parameters:
- filepath: Path to save the model
- overwrite: Whether to overwrite existing file
"""
def save_weights(self, filepath, overwrite=True, **kwargs):
"""
Save model weights to a file.
Parameters:
- filepath: Path to save weights
- overwrite: Whether to overwrite existing file
"""
def load_weights(self, filepath, **kwargs):
"""
Load model weights from a file.
Parameters:
- filepath: Path to load weights from
"""
def summary(self, line_length=None, positions=None, print_fn=None):
"""
Print a summary of the model architecture.
Parameters:
- line_length: Total length of printed lines
- positions: Relative or absolute positions of log elements
- print_fn: Print function to use
"""A linear stack of layers where each layer has exactly one input tensor and one output tensor, providing the simplest way to build models.
class Sequential(Model):
def __init__(self, layers=None, name=None, **kwargs):
"""
Create a Sequential model.
Parameters:
- layers: List of layers to add to the model
- name: Name of the model
"""
def add(self, layer):
"""
Add a layer to the end of the model.
Parameters:
- layer: Layer instance to add
"""
def pop(self):
"""
Remove the last layer in the model.
Returns:
The removed layer
"""Creates input tensors for functional API models, defining the input shape and properties.
def Input(shape=None, batch_size=None, name=None, dtype=None,
sparse=None, tensor=None, ragged=None, **kwargs):
"""
Create an input tensor for functional API models.
Parameters:
- shape: Shape tuple, not including batch dimension
- batch_size: Static batch size
- name: Name of the input tensor
- dtype: Data type of the input tensor
- sparse: Whether the tensor is sparse
- tensor: Optional existing tensor to wrap
- ragged: Whether the tensor is ragged
Returns:
KerasTensor that can be used as input to layers
"""Utility functions for working with models, including cloning, loading, and saving.
def clone_model(model, input_tensors=None, clone_function=None):
"""
Clone a model instance.
Parameters:
- model: Model to clone
- input_tensors: Optional list of input tensors for functional models
- clone_function: Callable to clone layers
Returns:
Cloned model instance
"""
def load_model(filepath, custom_objects=None, compile=True, safe_mode=True):
"""
Load a saved model.
Parameters:
- filepath: Path to saved model
- custom_objects: Dict of custom objects needed for loading
- compile: Whether to compile the model after loading
- safe_mode: Whether to load in safe mode
Returns:
Loaded model instance
"""
def save_model(model, filepath, overwrite=True, **kwargs):
"""
Save a model to file.
Parameters:
- model: Model to save
- filepath: Path to save model to
- overwrite: Whether to overwrite existing file
"""
def model_from_json(json_string, custom_objects=None):
"""
Parse a JSON model configuration string and return a model instance.
Parameters:
- json_string: JSON string encoding model configuration
- custom_objects: Dict of custom objects
Returns:
Model instance
"""import keras
from keras import layers
# Create a sequential model
model = keras.Sequential([
layers.Dense(128, activation='relu', input_shape=(784,)),
layers.Dropout(0.2),
layers.Dense(64, activation='relu'),
layers.Dense(10, activation='softmax')
])
# Compile the model
model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
# Print model summary
model.summary()import keras
from keras import layers
# Define inputs
inputs = keras.Input(shape=(784,))
# Define model architecture
x = layers.Dense(128, activation='relu')(inputs)
x = layers.Dropout(0.2)(x)
x = layers.Dense(64, activation='relu')(x)
outputs = layers.Dense(10, activation='softmax')(x)
# Create model
model = keras.Model(inputs=inputs, outputs=outputs, name='mnist_model')
# Compile the model
model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)import keras
from keras import layers
class MyModel(keras.Model):
def __init__(self, num_classes=10):
super().__init__()
self.dense1 = layers.Dense(128, activation='relu')
self.dropout = layers.Dropout(0.2)
self.dense2 = layers.Dense(64, activation='relu')
self.classifier = layers.Dense(num_classes, activation='softmax')
def call(self, inputs, training=None):
x = self.dense1(inputs)
x = self.dropout(x, training=training)
x = self.dense2(x)
return self.classifier(x)
# Create and compile model
model = MyModel(num_classes=10)
model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)Install with Tessl CLI
npx tessl i tessl/pypi-keras