Functional model construction
Define the entry node of a graph assembled from layers and models.
TensorFlow · Function
Creates a symbolic Keras tensor that starts a Functional API graph. Specify sample shape, dtype, batching, sparsity, and optionally a name or existing tensor.
tf.keras.Input creates a symbolic Keras tensor that records the expected sample shape and dtype. Use that tensor as the starting node for Functional API graphs, then pass the input and resulting output to tf.keras.Model.
tf.keras.Input(shape=None, batch_size=None, dtype=None, sparse=None, batch_shape=None, name=None, tensor=None)| Name | Type | Description |
|---|---|---|
shape | tuple of integers or None | Per-sample shape tuple, excluding the batch dimension; dimensions may be None. |
batch_size | integer | Optional fixed batch size. |
dtype | string | Expected input data type, such as float32 or int32. |
sparse | boolean | Whether the expected input is sparse; defaults to False. |
batch_shape | tuple | Complete input shape, including the batch dimension. |
name | string | Optional unique layer name; autogenerated if omitted. |
tensor | tensor | Existing tensor to wrap rather than creating a new placeholder. |
A Keras tensor.
import tensorflow as tf
inputs = tf.keras.Input(shape=(4,), name="features")
x = tf.keras.layers.Dense(8, activation="relu")(inputs)
outputs = tf.keras.layers.Dense(1)(x)
model = tf.keras.Model(inputs, outputs)
print(model.input_shape) # (None, 4)Creates a symbolic four-feature input and connects it to a small Functional API model.
Define the entry node of a graph assembled from layers and models.
Accept sequences or other data with a dimension determined at runtime.
Build models whose inputs have explicit names or multiple feature branches.
tf.keras.Input creates a symbolic Keras tensor used to define a Functional API graph. It describes the input structure without representing a concrete data batch.
Follow-up: How would you represent a variable-length sequence? Use shape=(None,), leaving the sequence-length dimension unknown.
shape describes one sample and excludes the batch dimension. For shape=(32,), the model expects batches whose samples are 32-dimensional vectors, with an unspecified batch size by default.
Follow-up: How can you make the batch dimension static? Supply batch_size, or specify the complete shape with batch_shape.
dtype sets the expected input data type, while sparse indicates whether the expected input is sparse. If sparse is False, sparse tensors may still be supplied, but they are densified with zeros.
Follow-up: What limitation applies to sparse=True? Sparse input support is documented as available only with the TensorFlow backend.
tf.keras.Modeltf.keras.layers.InputLayertf.keras.layers.Densetf.keras.InputSpec