Declare model inputs
Functional API graph construction
Keras · Function
keras.Input creates a symbolic Keras tensor that serves as the starting point for a model graph. Define its shape and related input properties, connect layers to it, and use the resulting tensors to build a Functional API model.
keras.Input creates a symbolic Keras tensor that describes the shape and type of data entering a model. Layers can consume that tensor to define a computation graph, after which the input and resulting output tensors are passed to keras.Model.
keras.Input(shape=None, batch_size=None, dtype=None, sparse=None, ragged=None, batch_shape=None, name=None, tensor=None, optional=False)| Name | Type | Description |
|---|---|---|
shape | tuple | Shape tuple excluding the batch size; dimensions may be integers or None. |
batch_size | int | Optional fixed batch size. |
dtype | str | Expected input data type, such as float32 or int32. |
sparse | bool | Whether the expected input is sparse; defaults to False. |
ragged | bool | Whether the expected input is ragged. |
batch_shape | tuple | Optional complete shape tuple including the batch size. |
name | str | Optional unique name for the input layer; autogenerated if omitted. |
tensor | tensor | Optional existing tensor to wrap. |
optional | bool | Whether the input may accept None values. |
A Keras tensor.
keras.Input to create the symbolic starting tensor for a Functional API graph.shape; None can represent an unknown dimension.batch_size to declare a fixed batch dimension independently of the remaining input shape.dtype to control the expected input data type, such as float32 or int32.tensor argument lets the input layer wrap an existing tensor instead of creating a new placeholder.shape excludes the batch dimension; use batch_shape when the complete shape, including batch size, must be specified.optional=True only when the model should accept a missing value for that input.import keras
from keras import layers
inputs = keras.Input(shape=(32,), name="features")
x = layers.Dense(16, activation="relu")(inputs)
outputs = layers.Dense(1)(x)
model = keras.Model(inputs, outputs)
model.summary()Creates a symbolic input with 32 features, connects layers to it, and builds a Functional API model.
Functional API graph construction
Fixed-size data pipelines
Multiple data representations
Integrating an existing symbolic tensor
keras.Input create?keras.Input creates a symbolic Keras tensor used to define a model graph. It describes properties such as shape and dtype without representing a concrete data batch.
Follow-up: How would you construct a model from it? Chain layers from the input tensor, then pass the input and final output tensors to keras.Model.
shape and batch_shape differ?shape describes dimensions after the batch dimension, while batch_shape includes the batch dimension. For example, shape=(32,) describes batches of 32-dimensional vectors.
Follow-up: When would batch_size be useful? Use it when a fixed static batch size is required; otherwise leave it unspecified for flexible batches.
The function accepts an optional dtype and can mark inputs as sparse or ragged. Sparse support is limited to the TensorFlow and JAX backends, and sparse inputs are densified when sparse=False.
Follow-up: What does optional=True change? It allows the input to accept None values.
keras.Modelkeras.layers.Densekeras.layers.InputLayer