Functional model construction
Build a Functional API graph by connecting an input tensor to layers and passing the endpoints to keras.Model.
Keras · Function
keras.Input() creates a symbolic Keras tensor for defining model inputs. Its shape describes each sample, while optional arguments configure batching, dtype, sparse or ragged inputs, naming, wrapping an existing tensor, and optional inputs.
keras.Input() creates a symbolic Keras tensor that represents model input. Specify the per-example shape and optional properties such as dtype, batch size, sparsity, raggedness, name, or optionality, then connect the tensor to layers and use it when constructing a 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 or None | Per-sample shape, excluding the batch dimension; dimensions may be integers or None. |
batch_size | int or None | Optional fixed batch size. |
dtype | string or None | Expected input data type. |
sparse | bool or None | Whether sparse tensors are expected. |
ragged | bool or None | Whether ragged tensors are expected. |
batch_shape | tuple or None | Complete shape including the batch dimension. |
name | string or None | Optional unique layer name. |
tensor | tensor or None | Existing tensor to wrap instead of creating a placeholder. |
optional | bool | Whether the input accepts None. |
returns | Keras tensor | A Keras tensor. |
A Keras tensor.
keras.Input() as the symbolic starting point for a Functional API model.shape as the per-sample dimensions; integer values fix dimensions and None permits a variable dimension.batch_size to declare a fixed batch size separately from the sample shape.batch_shape when specifying the complete shape, including the batch dimension, in one argument.dtype to control the expected input type, for example "float32" or "int32".optional=True when the model input may receive None.batch_shape when the batch dimension must be included explicitly.name must not be duplicated within the same model.sparse=False does not reject sparse inputs; they are converted to dense tensors with zero as the default value.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)Creates a symbolic input tensor and connects it to a small Functional API model.
Build a Functional API graph by connecting an input tensor to layers and passing the endpoints to keras.Model.
Describe data whose dimensions vary between examples by using None in the input shape.
Define models that accept sparse or ragged tensor representations.
Create a model signature where a particular input may be absent by enabling optional inputs.
keras.Input() create, and how is its shape interpreted?keras.Input() instantiates a symbolic Keras tensor for defining a model graph. Its shape describes one sample and does not include the batch dimension unless batch_shape is used.
Follow-up: How would you represent variable-length sequences? Use a shape containing None, such as shape=(None,), and choose an appropriate dtype.
dtype, sparse, and ragged?The dtype declares the expected input data type, while sparse and ragged describe whether sparse or ragged tensors are expected. These options let the input specification match the data representation used by the model.
Follow-up: What happens if sparse data is passed while sparse=False? Keras densifies it using a default value of zero.
optional argument change?The optional flag allows the input to receive None; by default, inputs are not optional. The function returns a Keras tensor that can be passed through layers and used as a model input.
Follow-up: When would optionality be useful? It can support model signatures where a particular input is allowed to be absent.
keras.Modelkeras.layers.DenseFunctional API