0
# Models and Architecture
1
2
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.
3
4
## Capabilities
5
6
### Model Base Class
7
8
The fundamental Model class that provides the core functionality for all Keras models, including compilation, training, evaluation, and prediction.
9
10
```python { .api }
11
class Model:
12
def __init__(self, inputs=None, outputs=None, name=None, **kwargs):
13
"""
14
Create a Model from functional API inputs/outputs or subclass.
15
16
Parameters:
17
- inputs: Input tensor(s) for functional API models
18
- outputs: Output tensor(s) for functional API models
19
- name: Name of the model
20
"""
21
22
def compile(self, optimizer='rmsprop', loss=None, metrics=None,
23
loss_weights=None, weighted_metrics=None, **kwargs):
24
"""
25
Configure the model for training.
26
27
Parameters:
28
- optimizer: String or optimizer instance
29
- loss: String or loss function
30
- metrics: List of metrics to monitor
31
- loss_weights: Optional list/dict of weights for loss functions
32
- weighted_metrics: List of metrics to apply sample weighting to
33
"""
34
35
def fit(self, x=None, y=None, batch_size=None, epochs=1, verbose='auto',
36
callbacks=None, validation_split=0.0, validation_data=None,
37
shuffle=True, class_weight=None, sample_weight=None, **kwargs):
38
"""
39
Train the model for a fixed number of epochs.
40
41
Parameters:
42
- x: Input data
43
- y: Target data
44
- batch_size: Number of samples per gradient update
45
- epochs: Number of epochs to train
46
- verbose: Verbosity mode (0, 1, 2, or 'auto')
47
- callbacks: List of callbacks to apply during training
48
- validation_split: Fraction of data to use for validation
49
- validation_data: Data on which to evaluate loss and metrics
50
- shuffle: Whether to shuffle the training data
51
- class_weight: Dict mapping class indices to weights
52
- sample_weight: Array of weights for training samples
53
54
Returns:
55
History object containing training metrics
56
"""
57
58
def evaluate(self, x=None, y=None, batch_size=None, verbose='auto',
59
sample_weight=None, **kwargs):
60
"""
61
Evaluate the model on test data.
62
63
Parameters:
64
- x: Input data
65
- y: Target data
66
- batch_size: Number of samples per batch
67
- verbose: Verbosity mode
68
- sample_weight: Array of weights for samples
69
70
Returns:
71
Scalar test loss or list of scalars
72
"""
73
74
def predict(self, x, batch_size=None, verbose='auto', **kwargs):
75
"""
76
Generate predictions for input samples.
77
78
Parameters:
79
- x: Input data
80
- batch_size: Number of samples per batch
81
- verbose: Verbosity mode
82
83
Returns:
84
Numpy array(s) of predictions
85
"""
86
87
def save(self, filepath, overwrite=True, **kwargs):
88
"""
89
Save the model to a file.
90
91
Parameters:
92
- filepath: Path to save the model
93
- overwrite: Whether to overwrite existing file
94
"""
95
96
def save_weights(self, filepath, overwrite=True, **kwargs):
97
"""
98
Save model weights to a file.
99
100
Parameters:
101
- filepath: Path to save weights
102
- overwrite: Whether to overwrite existing file
103
"""
104
105
def load_weights(self, filepath, **kwargs):
106
"""
107
Load model weights from a file.
108
109
Parameters:
110
- filepath: Path to load weights from
111
"""
112
113
def summary(self, line_length=None, positions=None, print_fn=None):
114
"""
115
Print a summary of the model architecture.
116
117
Parameters:
118
- line_length: Total length of printed lines
119
- positions: Relative or absolute positions of log elements
120
- print_fn: Print function to use
121
"""
122
```
123
124
### Sequential Model
125
126
A linear stack of layers where each layer has exactly one input tensor and one output tensor, providing the simplest way to build models.
127
128
```python { .api }
129
class Sequential(Model):
130
def __init__(self, layers=None, name=None, **kwargs):
131
"""
132
Create a Sequential model.
133
134
Parameters:
135
- layers: List of layers to add to the model
136
- name: Name of the model
137
"""
138
139
def add(self, layer):
140
"""
141
Add a layer to the end of the model.
142
143
Parameters:
144
- layer: Layer instance to add
145
"""
146
147
def pop(self):
148
"""
149
Remove the last layer in the model.
150
151
Returns:
152
The removed layer
153
"""
154
```
155
156
### Functional API Input
157
158
Creates input tensors for functional API models, defining the input shape and properties.
159
160
```python { .api }
161
def Input(shape=None, batch_size=None, name=None, dtype=None,
162
sparse=None, tensor=None, ragged=None, **kwargs):
163
"""
164
Create an input tensor for functional API models.
165
166
Parameters:
167
- shape: Shape tuple, not including batch dimension
168
- batch_size: Static batch size
169
- name: Name of the input tensor
170
- dtype: Data type of the input tensor
171
- sparse: Whether the tensor is sparse
172
- tensor: Optional existing tensor to wrap
173
- ragged: Whether the tensor is ragged
174
175
Returns:
176
KerasTensor that can be used as input to layers
177
"""
178
```
179
180
### Model Utilities
181
182
Utility functions for working with models, including cloning, loading, and saving.
183
184
```python { .api }
185
def clone_model(model, input_tensors=None, clone_function=None):
186
"""
187
Clone a model instance.
188
189
Parameters:
190
- model: Model to clone
191
- input_tensors: Optional list of input tensors for functional models
192
- clone_function: Callable to clone layers
193
194
Returns:
195
Cloned model instance
196
"""
197
198
def load_model(filepath, custom_objects=None, compile=True, safe_mode=True):
199
"""
200
Load a saved model.
201
202
Parameters:
203
- filepath: Path to saved model
204
- custom_objects: Dict of custom objects needed for loading
205
- compile: Whether to compile the model after loading
206
- safe_mode: Whether to load in safe mode
207
208
Returns:
209
Loaded model instance
210
"""
211
212
def save_model(model, filepath, overwrite=True, **kwargs):
213
"""
214
Save a model to file.
215
216
Parameters:
217
- model: Model to save
218
- filepath: Path to save model to
219
- overwrite: Whether to overwrite existing file
220
"""
221
222
def model_from_json(json_string, custom_objects=None):
223
"""
224
Parse a JSON model configuration string and return a model instance.
225
226
Parameters:
227
- json_string: JSON string encoding model configuration
228
- custom_objects: Dict of custom objects
229
230
Returns:
231
Model instance
232
"""
233
```
234
235
## Usage Examples
236
237
### Sequential Model Example
238
239
```python
240
import keras
241
from keras import layers
242
243
# Create a sequential model
244
model = keras.Sequential([
245
layers.Dense(128, activation='relu', input_shape=(784,)),
246
layers.Dropout(0.2),
247
layers.Dense(64, activation='relu'),
248
layers.Dense(10, activation='softmax')
249
])
250
251
# Compile the model
252
model.compile(
253
optimizer='adam',
254
loss='sparse_categorical_crossentropy',
255
metrics=['accuracy']
256
)
257
258
# Print model summary
259
model.summary()
260
```
261
262
### Functional API Example
263
264
```python
265
import keras
266
from keras import layers
267
268
# Define inputs
269
inputs = keras.Input(shape=(784,))
270
271
# Define model architecture
272
x = layers.Dense(128, activation='relu')(inputs)
273
x = layers.Dropout(0.2)(x)
274
x = layers.Dense(64, activation='relu')(x)
275
outputs = layers.Dense(10, activation='softmax')(x)
276
277
# Create model
278
model = keras.Model(inputs=inputs, outputs=outputs, name='mnist_model')
279
280
# Compile the model
281
model.compile(
282
optimizer='adam',
283
loss='sparse_categorical_crossentropy',
284
metrics=['accuracy']
285
)
286
```
287
288
### Model Subclassing Example
289
290
```python
291
import keras
292
from keras import layers
293
294
class MyModel(keras.Model):
295
def __init__(self, num_classes=10):
296
super().__init__()
297
self.dense1 = layers.Dense(128, activation='relu')
298
self.dropout = layers.Dropout(0.2)
299
self.dense2 = layers.Dense(64, activation='relu')
300
self.classifier = layers.Dense(num_classes, activation='softmax')
301
302
def call(self, inputs, training=None):
303
x = self.dense1(inputs)
304
x = self.dropout(x, training=training)
305
x = self.dense2(x)
306
return self.classifier(x)
307
308
# Create and compile model
309
model = MyModel(num_classes=10)
310
model.compile(
311
optimizer='adam',
312
loss='sparse_categorical_crossentropy',
313
metrics=['accuracy']
314
)
315
```