Model construction
Compose neural networks from reusable tensor transformations.
TensorFlow · Module
tf.keras is TensorFlow's high-level API for building and using deep-learning models. It combines reusable layers and models with workflows for data processing, training, evaluation, tuning, and deployment.
tf.keras is TensorFlow's high-level interface for constructing, training, evaluating, and exporting deep-learning models. It organizes models from reusable layers and supplies a standard workflow through compilation, fitting, prediction, and evaluation.
tf.kerasimport tensorflow as tf
inputs = tf.keras.Input(shape=(4,))
x = tf.keras.layers.Dense(8, activation="relu")(inputs)
outputs = tf.keras.layers.Dense(1)(x)
model = tf.keras.Model(inputs, outputs)
model.compile(optimizer="adam", loss="mse")
model.fit(tf.random.normal((8, 4)), tf.random.normal((8, 1)), epochs=1, verbose=0)Builds a small functional model, configures its training behavior, and performs one training epoch.
Compose neural networks from reusable tensor transformations.
Configure and run standard training and evaluation loops.
Prepare inputs and integrate preprocessing into models.
Export models for scalable or cross-platform inference.
tf.keras is TensorFlow's high-level API for deep learning. It provides abstractions for layers and models, plus a workflow covering model construction, training, evaluation, and deployment.
Follow-up: How does a model differ from a layer? A layer performs an input/output transformation, whereas a model represents a graph or grouping of layers and exposes training and inference operations.
The main building blocks are layers and models. Layers encapsulate computation and state, while models compose layers into a directed acyclic graph and provide methods such as compile, fit, predict, and evaluate.
Follow-up: Why use the model-level API? It centralizes training configuration and execution while preserving access to TensorFlow's scaling and deployment capabilities.
A typical workflow creates a model from layers, configures it with compile, trains it with fit, and uses predict or evaluate afterward. Keras can also cover data processing, hyperparameter tuning, and deployment.
Follow-up: How can a Keras model be deployed beyond Python? It can be exported for browser or mobile execution and served through a web API.
tf.keras.layers.Layertf.keras.Modeltf.keras.Model.compiletf.keras.Model.fittf.keras.Model.predicttf.keras.Model.evaluate