0
# Keras
1
2
A multi-backend deep learning framework that provides a unified API for building and training neural networks across JAX, TensorFlow, PyTorch, and OpenVINO backends. Keras 3 is designed for accelerated model development with high-level abstractions while maintaining state-of-the-art performance through backend-specific optimizations.
3
4
## Package Information
5
6
- **Package Name**: keras-nightly
7
- **Language**: Python
8
- **Installation**: `pip install keras-nightly`
9
10
## Core Imports
11
12
```python
13
import keras
14
```
15
16
Common imports for building neural networks:
17
18
```python
19
from keras import layers, models, optimizers, losses, metrics
20
from keras.models import Sequential, Model
21
from keras.layers import Dense, Conv2D, LSTM
22
```
23
24
## Basic Usage
25
26
```python
27
import keras
28
from keras import layers
29
30
# Create a simple sequential model
31
model = keras.Sequential([
32
layers.Dense(64, activation='relu', input_shape=(784,)),
33
layers.Dropout(0.2),
34
layers.Dense(10, activation='softmax')
35
])
36
37
# Compile the model
38
model.compile(
39
optimizer='adam',
40
loss='sparse_categorical_crossentropy',
41
metrics=['accuracy']
42
)
43
44
# Train the model
45
model.fit(x_train, y_train, epochs=5, validation_split=0.2)
46
47
# Make predictions
48
predictions = model.predict(x_test)
49
```
50
51
## Architecture
52
53
Keras provides a layered architecture enabling flexible neural network construction:
54
55
- **Models**: High-level containers (Sequential, Functional API) that organize layers into computation graphs
56
- **Layers**: Fundamental building blocks that transform data (Dense, Conv2D, LSTM, etc.)
57
- **Backend Operations**: Low-level mathematical operations abstracted across multiple frameworks
58
- **Training Loop**: Compilation, fitting, and evaluation with automatic differentiation and optimization
59
- **Preprocessing**: Data pipeline components for text, image, and numerical preprocessing
60
61
This design allows every component to be customized while providing seamless model portability across different backends (JAX, TensorFlow, PyTorch, OpenVINO) without code changes.
62
63
## Capabilities
64
65
### Core Framework Classes
66
67
Essential classes for building and training neural networks, including model containers, base layer classes, and core data structures.
68
69
```python { .api }
70
class Sequential:
71
def __init__(self, layers=None, name=None): ...
72
def add(self, layer): ...
73
def compile(self, optimizer, loss=None, metrics=None, **kwargs): ...
74
def fit(self, x, y=None, **kwargs): ...
75
76
class Model:
77
def __init__(self, inputs, outputs, name=None): ...
78
def compile(self, optimizer, loss=None, metrics=None, **kwargs): ...
79
def fit(self, x, y=None, **kwargs): ...
80
81
class Layer:
82
def __init__(self, **kwargs): ...
83
def call(self, inputs, **kwargs): ...
84
def build(self, input_shape): ...
85
86
def Input(shape, batch_size=None, name=None, dtype=None, **kwargs): ...
87
```
88
89
[Core Framework](./core-framework.md)
90
91
### Neural Network Layers
92
93
Complete collection of neural network layer types including dense, convolutional, recurrent, normalization, attention, and preprocessing layers.
94
95
```python { .api }
96
class Dense:
97
def __init__(self, units, activation=None, use_bias=True, **kwargs): ...
98
99
class Conv2D:
100
def __init__(self, filters, kernel_size, strides=(1,1), padding='valid', **kwargs): ...
101
102
class LSTM:
103
def __init__(self, units, activation='tanh', return_sequences=False, **kwargs): ...
104
105
class BatchNormalization:
106
def __init__(self, axis=-1, momentum=0.99, epsilon=1e-3, **kwargs): ...
107
108
class MultiHeadAttention:
109
def __init__(self, num_heads, key_dim, **kwargs): ...
110
```
111
112
[Neural Network Layers](./layers.md)
113
114
### Loss Functions and Metrics
115
116
Comprehensive collection of loss functions for training and metrics for evaluation across classification, regression, and specialized tasks.
117
118
```python { .api }
119
class BinaryCrossentropy:
120
def __init__(self, from_logits=False, **kwargs): ...
121
122
class SparseCategoricalCrossentropy:
123
def __init__(self, from_logits=False, **kwargs): ...
124
125
class MeanSquaredError:
126
def __init__(self, **kwargs): ...
127
128
class Accuracy:
129
def __init__(self, name='accuracy', **kwargs): ...
130
131
class F1Score:
132
def __init__(self, average=None, threshold=None, **kwargs): ...
133
```
134
135
[Loss Functions and Metrics](./losses-metrics.md)
136
137
### Optimizers
138
139
Optimization algorithms for training neural networks, from basic gradient descent to advanced adaptive methods.
140
141
```python { .api }
142
class Adam:
143
def __init__(self, learning_rate=0.001, beta_1=0.9, beta_2=0.999, **kwargs): ...
144
145
class SGD:
146
def __init__(self, learning_rate=0.01, momentum=0.0, nesterov=False, **kwargs): ...
147
148
class AdamW:
149
def __init__(self, learning_rate=0.001, weight_decay=0.004, **kwargs): ...
150
151
class RMSprop:
152
def __init__(self, learning_rate=0.001, rho=0.9, **kwargs): ...
153
```
154
155
[Optimizers](./optimizers.md)
156
157
### Pre-trained Models
158
159
Extensive collection of pre-trained computer vision models with ImageNet weights for transfer learning and feature extraction.
160
161
```python { .api }
162
def ResNet50(include_top=True, weights='imagenet', input_tensor=None, **kwargs): ...
163
def EfficientNetB0(include_top=True, weights='imagenet', **kwargs): ...
164
def VGG16(include_top=True, weights='imagenet', **kwargs): ...
165
def MobileNetV2(include_top=True, weights='imagenet', **kwargs): ...
166
def InceptionV3(include_top=True, weights='imagenet', **kwargs): ...
167
```
168
169
[Pre-trained Models](./applications.md)
170
171
### Mathematical Operations
172
173
Backend-agnostic mathematical operations providing NumPy-compatible APIs and neural network specific functions.
174
175
```python { .api }
176
# Core operations
177
def cast(x, dtype): ...
178
def convert_to_tensor(x, dtype=None): ...
179
def shape(x): ...
180
def dtype(x): ...
181
182
# Mathematical functions
183
def add(x1, x2): ...
184
def matmul(x1, x2): ...
185
def softmax(x, axis=-1): ...
186
def relu(x): ...
187
188
# Neural network operations
189
def conv(inputs, kernel, strides=1, padding='valid', **kwargs): ...
190
def batch_normalization(x, mean, variance, offset, scale, **kwargs): ...
191
```
192
193
[Mathematical Operations](./operations.md)
194
195
### Data Processing
196
197
Comprehensive data preprocessing utilities for images, text, audio, and numerical data with built-in augmentation capabilities.
198
199
```python { .api }
200
class TextVectorization:
201
def __init__(self, max_tokens=None, **kwargs): ...
202
203
class Normalization:
204
def __init__(self, axis=-1, **kwargs): ...
205
206
class RandomRotation:
207
def __init__(self, factor, **kwargs): ...
208
209
def image_dataset_from_directory(directory, labels='inferred', **kwargs): ...
210
def text_dataset_from_directory(directory, labels='inferred', **kwargs): ...
211
```
212
213
[Data Processing](./preprocessing.md)
214
215
### Training and Callbacks
216
217
Training utilities, callbacks for monitoring and controlling training, and model persistence functionality.
218
219
```python { .api }
220
class EarlyStopping:
221
def __init__(self, monitor='val_loss', patience=0, **kwargs): ...
222
223
class ModelCheckpoint:
224
def __init__(self, filepath, monitor='val_loss', **kwargs): ...
225
226
class ReduceLROnPlateau:
227
def __init__(self, monitor='val_loss', factor=0.1, patience=10, **kwargs): ...
228
229
def save_model(model, filepath, overwrite=True, **kwargs): ...
230
def load_model(filepath, custom_objects=None, compile=True, **kwargs): ...
231
```
232
233
[Training and Callbacks](./training-callbacks.md)
234
235
### Backend Configuration
236
237
Backend configuration utilities for managing numerical precision, data formats, and cross-backend compatibility.
238
239
```python { .api }
240
def backend(): ...
241
def set_floatx(dtype): ...
242
def set_image_data_format(data_format): ...
243
def clear_session(): ...
244
def set_random_seed(seed): ...
245
```
246
247
[Backend Configuration](./backend-config.md)
248
249
### Activation Functions
250
251
Comprehensive collection of activation functions for neural networks, from traditional ReLU and sigmoid to modern alternatives like GELU and Swish.
252
253
```python { .api }
254
def relu(x, negative_slope=0.0, max_value=None, threshold=0.0): ...
255
def sigmoid(x): ...
256
def tanh(x): ...
257
def softmax(x, axis=-1): ...
258
def gelu(x, approximate=False): ...
259
def silu(x): ...
260
def mish(x): ...
261
def elu(x, alpha=1.0): ...
262
def leaky_relu(x, negative_slope=0.3): ...
263
```
264
265
[Activation Functions](./activations.md)
266
267
### Weight Initializers
268
269
Weight initialization strategies for neural network layers, including constant, random, and variance-scaling methods.
270
271
```python { .api }
272
class Zeros: ...
273
class Ones: ...
274
class Constant:
275
def __init__(self, value=0.0): ...
276
class RandomNormal:
277
def __init__(self, mean=0.0, stddev=0.05, seed=None): ...
278
class GlorotUniform:
279
def __init__(self, seed=None): ...
280
class HeNormal:
281
def __init__(self, seed=None): ...
282
```
283
284
[Weight Initializers](./initializers.md)
285
286
### Regularizers
287
288
Regularization techniques for preventing overfitting, including L1, L2, and orthogonal regularization.
289
290
```python { .api }
291
class L1:
292
def __init__(self, l1=0.01): ...
293
class L2:
294
def __init__(self, l2=0.01): ...
295
class L1L2:
296
def __init__(self, l1=0.0, l2=0.0): ...
297
class OrthogonalRegularizer:
298
def __init__(self, factor=0.01, mode='rows'): ...
299
```
300
301
[Regularizers](./regularizers.md)
302
303
## Types
304
305
```python { .api }
306
# Core tensor type
307
class KerasTensor:
308
shape: tuple
309
dtype: str
310
311
# Variable for trainable parameters
312
class Variable:
313
def __init__(self, initializer, shape=None, dtype=None, **kwargs): ...
314
def assign(self, value): ...
315
316
# Base types for extensibility
317
class Optimizer: ...
318
class Loss: ...
319
class Metric: ...
320
class Layer: ...
321
class Initializer: ...
322
class Regularizer: ...
323
class Constraint: ...
324
class Callback: ...
325
```