tensorflow/tensorflow
Keras (bundled v2)
The legacy v2 copy of Keras shipped with TensorFlow. Lives in tensorflow/python/keras/.
Heads-up: Keras 3 is developed in
keras-team/kerasand is the recommended Keras for new projects. The code in this directory is a frozen v2 snapshot kept around to keeptf.keras.*working for existing users. New features go to keras-team/keras.
Purpose
- Provide the historical
tf.kerassymbols (Sequential,Model,Layer,Optimizer,Loss,Metric,callbacks.*,applications.*). - Keep older user code (using
tf.keras.Model,model.fit,model.compile) functional through deprecation cycles. - Serve as a reference v2 implementation tightly coupled to the bundled TF runtime.
Directory layout
tensorflow/python/keras/
├── __init__.py
├── backend.py # ~206 KB — low-level math used by layers
├── callbacks.py # ~110 KB — TensorBoard, ModelCheckpoint, EarlyStopping, ...
├── metrics.py # ~129 KB — every built-in metric
├── losses.py # ~77 KB — losses
├── models.py # Model, Sequential, save/load
├── activations.py # ReLU, GELU, ...
├── constraints.py
├── regularizers.py
├── distribute/ # Distribution support for Model.fit
├── engine/ # Layer + Model base classes (the heart)
├── initializers/
├── layers/ # The big collection: Dense, Conv2D, LSTM, ...
├── legacy_tf_layers/ # Even older `tf.layers` (pre-Keras-as-default)
├── mixed_precision/
├── optimizer_v1.py # Pre-2.0 optimizer base class
├── optimizer_v2/ # The widely-used Keras V2 optimizers
├── protobuf/ # Keras-specific protos (saved model)
├── saving/ # HDF5, SavedModel, keras_saved_model formats
└── utils/Key abstractions
| Class | File | Role |
|---|---|---|
Layer |
tensorflow/python/keras/engine/base_layer.py |
Base class for all layers. |
Model |
tensorflow/python/keras/engine/training.py |
Trainable graph; provides fit/evaluate/predict. |
Sequential |
tensorflow/python/keras/engine/sequential.py |
Linear stack of layers. |
Optimizer |
tensorflow/python/keras/optimizer_v2/optimizer_v2.py |
Base optimizer. |
Callback |
tensorflow/python/keras/callbacks.py |
Training-time hook. |
Metric |
tensorflow/python/keras/metrics.py |
Stateful metric. |
Loss |
tensorflow/python/keras/losses.py |
Loss base class. |
keras.backend namespace |
tensorflow/python/keras/backend.py |
Kitchen-sink helpers (deprecated, but still imported widely). |
How model.fit works
sequenceDiagram
participant User
participant Model
participant Distribute as DistributionStrategy
participant Function as tf.function
participant Runtime as TF runtime
User->>Model: model.fit(x, y, epochs=...)
Model->>Distribute: scope, replicate dataset
Distribute->>Function: train_step traced as tf.function
loop epochs * steps
Function->>Runtime: forward pass + backward pass + apply_gradients
Runtime-->>Function: losses, metrics
end
Function-->>Model: history
Model-->>User: HistoryInternally model.fit builds a per-step tf.function that runs forward, computes loss, computes gradients (via GradientTape), and applies the optimizer update. Distribution is layered on by entering a tf.distribute.Strategy.scope() before building the model.
Bundled-vs-standalone Keras
import keras (the standalone) reaches Keras 3, which can target multiple backends (TF, JAX, PyTorch). import tensorflow as tf; tf.keras.* resolves into this directory and only targets the bundled TF runtime. Some new APIs land in standalone Keras and are not mirrored here.
Saving and loading
model.save("path")writes a SavedModel by default. Implementation intensorflow/python/keras/saving/.- HDF5 format (
.h5) is supported viatensorflow/python/keras/saving/hdf5_format.py. keras.models.load_modelre-constructs the model from disk and rebuilds the trackable graph.
Integration points
- Builds on:
tf.Tensor,tf.Variable,tf.function,tf.GradientTape,tf.distribute.Strategy. - Saves through:
tensorflow/python/saved_model/(see features/saved-model). - Logs to:
tensorflow/python/summary/for TensorBoard; theTensorBoardcallback is intensorflow/python/keras/callbacks.py.
Entry points for modification
- Bug fixes that affect bundled
tf.kerasusers should land in this directory. - New features should land in
keras-team/keras(Keras 3) and only get backported here when they fix a critical regression. - The Keras team treats this directory as nearly read-only — touch with care, and ping the Keras maintainers (
comp:keraslabel).
Related
- features/tf-function-and-autograph — the trace mechanism
model.fituses. - features/saved-model — what
model.savewrites. - features/distribution-strategy — how multi-GPU
model.fitworks.
Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.