0
# Keras
1
2
Keras is a 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. It enables accelerated model development through its intuitive interface while offering state-of-the-art performance with backend-specific optimizations.
3
4
## Package Information
5
6
- **Package Name**: keras
7
- **Language**: Python
8
- **Installation**: `pip install keras`
9
- **Documentation**: https://keras.io/
10
11
## Core Imports
12
13
```python
14
import keras
15
```
16
17
For specific functionality:
18
19
```python
20
from keras import layers, models, optimizers, losses, metrics
21
from keras.applications import ResNet50, VGG16, MobileNet
22
from keras.datasets import mnist, cifar10
23
```
24
25
## Basic Usage
26
27
```python
28
import keras
29
from keras import layers
30
31
# Create a simple sequential model
32
model = keras.Sequential([
33
layers.Dense(128, activation='relu', input_shape=(784,)),
34
layers.Dropout(0.2),
35
layers.Dense(10, activation='softmax')
36
])
37
38
# Compile the model
39
model.compile(
40
optimizer='adam',
41
loss='sparse_categorical_crossentropy',
42
metrics=['accuracy']
43
)
44
45
# Train the model
46
# model.fit(x_train, y_train, epochs=5, validation_data=(x_test, y_test))
47
```
48
49
## Architecture
50
51
Keras follows a modular architecture with these core components:
52
53
- **Models**: Containers that organize layers (Sequential, Functional API, Model subclassing)
54
- **Layers**: Building blocks for neural networks (Dense, Conv2D, LSTM, etc.)
55
- **Optimizers**: Algorithms for training models (Adam, SGD, RMSprop, etc.)
56
- **Losses**: Functions to measure model performance during training
57
- **Metrics**: Functions to monitor training and testing
58
- **Callbacks**: Utilities to customize training behavior
59
- **Backend**: Abstraction layer supporting JAX, TensorFlow, PyTorch, and OpenVINO
60
61
## Capabilities
62
63
### Models and Architecture
64
65
Core model types and architecture patterns for building neural networks, including Sequential models, Functional API, and Model subclassing approaches.
66
67
```python { .api }
68
class Model:
69
def __init__(self, inputs=None, outputs=None, name=None): ...
70
def compile(self, optimizer='rmsprop', loss=None, metrics=None, **kwargs): ...
71
def fit(self, x=None, y=None, batch_size=None, epochs=1, **kwargs): ...
72
def predict(self, x, batch_size=None, **kwargs): ...
73
74
class Sequential(Model):
75
def __init__(self, layers=None, name=None): ...
76
def add(self, layer): ...
77
78
def Input(shape=None, batch_size=None, name=None, dtype=None, **kwargs): ...
79
```
80
81
[Models and Architecture](./models.md)
82
83
### Layers and Building Blocks
84
85
Comprehensive layer types for building neural networks, including core layers, convolutional layers, recurrent layers, attention mechanisms, normalization, pooling, and preprocessing layers.
86
87
```python { .api }
88
class Layer:
89
def __init__(self, **kwargs): ...
90
def call(self, inputs, **kwargs): ...
91
def build(self, input_shape): ...
92
93
class Dense(Layer):
94
def __init__(self, units, activation=None, use_bias=True, **kwargs): ...
95
96
class Conv2D(Layer):
97
def __init__(self, filters, kernel_size, strides=(1, 1), padding='valid', **kwargs): ...
98
99
class LSTM(Layer):
100
def __init__(self, units, activation='tanh', return_sequences=False, **kwargs): ...
101
```
102
103
[Layers and Building Blocks](./layers.md)
104
105
### Training and Optimization
106
107
Optimizers, losses, metrics, and callbacks for training neural networks effectively with various algorithms and monitoring tools.
108
109
```python { .api }
110
class Optimizer:
111
def __init__(self, learning_rate=0.001, **kwargs): ...
112
113
class Adam(Optimizer):
114
def __init__(self, learning_rate=0.001, beta_1=0.9, beta_2=0.999, **kwargs): ...
115
116
class Loss:
117
def __call__(self, y_true, y_pred): ...
118
119
class SparseCategoricalCrossentropy(Loss):
120
def __init__(self, from_logits=False, **kwargs): ...
121
122
class Metric:
123
def update_state(self, y_true, y_pred): ...
124
def result(self): ...
125
```
126
127
[Training and Optimization](./training.md)
128
129
### Pre-trained Models
130
131
Ready-to-use pre-trained models for computer vision tasks, including VGG, ResNet, MobileNet, EfficientNet, and many others, with ImageNet weights.
132
133
```python { .api }
134
def ResNet50(include_top=True, weights='imagenet', input_tensor=None, **kwargs): ...
135
def VGG16(include_top=True, weights='imagenet', input_tensor=None, **kwargs): ...
136
def MobileNet(include_top=True, weights='imagenet', input_tensor=None, **kwargs): ...
137
def EfficientNetB0(include_top=True, weights='imagenet', input_tensor=None, **kwargs): ...
138
```
139
140
[Pre-trained Models](./applications.md)
141
142
### Data Processing and Utilities
143
144
Built-in datasets, data preprocessing utilities, image processing functions, and various helper utilities for machine learning workflows.
145
146
```python { .api }
147
# Datasets
148
def load_data(): ... # Available in mnist, cifar10, cifar100, fashion_mnist, etc.
149
150
# Image utilities
151
def load_img(path, color_mode='rgb', target_size=None, **kwargs): ...
152
def img_to_array(img, dtype=None): ...
153
def array_to_img(x, scale=True): ...
154
155
# Data utilities
156
def to_categorical(y, num_classes=None, dtype='float32'): ...
157
def normalize(x, axis=-1, order=2): ...
158
```
159
160
[Data Processing and Utilities](./data-utils.md)
161
162
### Activation Functions
163
164
Comprehensive set of activation functions for introducing non-linearity in neural networks, including standard activations (ReLU, sigmoid, tanh), advanced functions (GELU, Swish, Mish), and specialized activations.
165
166
```python { .api }
167
def relu(x, negative_slope=0.0, max_value=None, threshold=0.0): ...
168
def sigmoid(x): ...
169
def tanh(x): ...
170
def softmax(x, axis=-1): ...
171
def gelu(x, approximate=False): ...
172
def silu(x): ...
173
def mish(x): ...
174
def leaky_relu(x, negative_slope=0.01): ...
175
```
176
177
[Activation Functions](./activations.md)
178
179
### Model Saving and Serialization
180
181
Complete functionality for model persistence, weight management, custom object registration, and serialization utilities for saving and loading models across different formats.
182
183
```python { .api }
184
def save_model(model, filepath, overwrite=True, save_format=None, **kwargs): ...
185
def load_model(filepath, custom_objects=None, compile=True, safe_mode=True): ...
186
def save_weights(model, filepath, overwrite=True, save_format=None, **kwargs): ...
187
def load_weights(model, filepath, skip_mismatch=False, by_name=False, **kwargs): ...
188
def register_keras_serializable(package='Custom', name=None): ...
189
```
190
191
[Model Saving and Serialization](./saving.md)
192
193
### Weight Initializers
194
195
Weight initialization strategies for proper model initialization, including constant initializers, random initializers, and variance scaling methods optimized for different activation functions.
196
197
```python { .api }
198
class Initializer: ...
199
class Zeros(Initializer): ...
200
class Ones(Initializer): ...
201
class GlorotNormal(Initializer): ...
202
class GlorotUniform(Initializer): ...
203
class HeNormal(Initializer): ...
204
class HeUniform(Initializer): ...
205
class Orthogonal(Initializer): ...
206
```
207
208
[Weight Initializers](./initializers.md)
209
210
### Regularizers
211
212
Regularization techniques to prevent overfitting through weight penalties, including L1, L2, combined L1L2, and orthogonal regularization for improved model generalization.
213
214
```python { .api }
215
class Regularizer: ...
216
class L1(Regularizer): ...
217
class L2(Regularizer): ...
218
class L1L2(Regularizer): ...
219
class OrthogonalRegularizer(Regularizer): ...
220
def l1(l1=0.01): ...
221
def l2(l2=0.01): ...
222
```
223
224
[Regularizers](./regularizers.md)
225
226
### Random Operations
227
228
Random number generation and sampling functions for various probability distributions, data augmentation, and stochastic operations with seed management for reproducibility.
229
230
```python { .api }
231
def normal(shape, mean=0.0, stddev=1.0, dtype=None, seed=None): ...
232
def uniform(shape, minval=0.0, maxval=1.0, dtype=None, seed=None): ...
233
def randint(shape, minval, maxval, dtype='int32', seed=None): ...
234
def dropout(x, rate, noise_shape=None, seed=None): ...
235
def shuffle(x, axis=0, seed=None): ...
236
class SeedGenerator: ...
237
```
238
239
[Random Operations](./random.md)
240
241
### Backend Operations
242
243
Low-level operations and backend functionality for tensor operations, mathematical functions, and neural network primitives across different backend engines.
244
245
```python { .api }
246
# Core operations
247
def cast(x, dtype): ...
248
def shape(x): ...
249
def reshape(x, shape): ...
250
def transpose(x, axes=None): ...
251
252
# Math operations
253
def add(x1, x2): ...
254
def multiply(x1, x2): ...
255
def matmul(x1, x2): ...
256
def relu(x): ...
257
def softmax(x, axis=-1): ...
258
```
259
260
[Backend Operations](./operations.md)
261
262
## Core Types
263
264
```python { .api }
265
class KerasTensor:
266
"""Symbolic tensor representation used in Keras functional API."""
267
def __init__(self, type_spec, name=None): ...
268
269
class Variable:
270
"""Keras variable for storing mutable state."""
271
def __init__(self, initializer, shape=None, dtype=None, **kwargs): ...
272
def assign(self, value): ...
273
274
class DTypePolicy:
275
"""Policy for dtype handling in mixed precision training."""
276
def __init__(self, name): ...
277
```