tf.keras.Input
Stay organized with collections
Save and categorize content based on your preferences.
Input()
is used to instantiate a Keras tensor.
tf.keras.Input(
shape=None,
batch_size=None,
name=None,
dtype=None,
sparse=None,
tensor=None,
ragged=None,
type_spec=None,
**kwargs
)
A Keras tensor is a symbolic tensor-like object, which we augment with
certain attributes that allow us to build a Keras model just by knowing the
inputs and outputs of the model.
For instance, if a
, b
and c
are Keras tensors,
it becomes possible to do:
model = Model(input=[a, b], output=c)
Args |
shape
|
A shape tuple (integers), not including the batch size.
For instance, shape=(32,) indicates that the expected input
will be batches of 32-dimensional vectors. Elements of this tuple
can be None; 'None' elements represent dimensions where the shape is
not known.
|
batch_size
|
optional static batch size (integer).
|
name
|
An optional name string for the layer.
Should be unique in a model (do not reuse the same name twice).
It will be autogenerated if it isn't provided.
|
dtype
|
The data type expected by the input, as a string
(float32 , float64 , int32 ...)
|
sparse
|
A boolean specifying whether the placeholder to be created is
sparse. Only one of 'ragged' and 'sparse' can be True. Note that,
if sparse is False, sparse tensors can still be passed into the
input - they will be densified with a default value of 0.
|
tensor
|
Optional existing tensor to wrap into the Input layer.
If set, the layer will use the tf.TypeSpec of this tensor rather
than creating a new placeholder tensor.
|
ragged
|
A boolean specifying whether the placeholder to be created is
ragged. Only one of 'ragged' and 'sparse' can be True. In this case,
values of 'None' in the 'shape' argument represent ragged
dimensions. For more information about RaggedTensors, see
this guide.
|
type_spec
|
A tf.TypeSpec object to create the input placeholder from.
When provided, all other args except name must be None.
|
**kwargs
|
deprecated arguments support. Supports batch_shape and
batch_input_shape .
|
Example:
# this is a logistic regression in Keras
x = Input(shape=(32,))
y = Dense(16, activation='softmax')(x)
model = Model(x, y)
Note that even if eager execution is enabled,
Input
produces a symbolic tensor-like object (i.e. a placeholder).
This symbolic tensor-like object can be used with lower-level
TensorFlow ops that take tensors as inputs, as such:
x = Input(shape=(32,))
y = tf.square(x) # This op will be treated like a layer
model = Model(x, y)
(This behavior does not work for higher-order TensorFlow APIs such as
control flow and being directly watched by a tf.GradientTape
).
However, the resulting model will not track any variables that were
used as inputs to TensorFlow ops. All variable usages must happen within
Keras layers to make sure they will be tracked by the model's weights.
The Keras Input can also create a placeholder from an arbitrary
tf.TypeSpec
, e.g:
x = Input(type_spec=tf.RaggedTensorSpec(shape=[None, None],
dtype=tf.float32, ragged_rank=1))
y = x.values
model = Model(x, y)
When passing an arbitrary tf.TypeSpec
, it must represent the signature of
an entire batch instead of just one example.
Raises |
ValueError
|
If both sparse and ragged are provided.
|
ValueError
|
If both shape and (batch_input_shape or batch_shape ) are
provided.
|
ValueError
|
If shape , tensor and type_spec are None.
|
ValueError
|
If arguments besides type_spec are non-None while
type_spec is passed.
|
ValueError
|
if any unrecognized parameters are provided.
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2023-10-06 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2023-10-06 UTC."],[],[],null,["# tf.keras.Input\n\n\u003cbr /\u003e\n\n|-----------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/keras-team/keras/tree/v2.13.1/keras/engine/input_layer.py#L295-L463) |\n\n`Input()` is used to instantiate a Keras tensor.\n\n#### View aliases\n\n\n**Main aliases**\n\n[`tf.keras.layers.Input`](https://www.tensorflow.org/api_docs/python/tf/keras/Input)\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.keras.Input`](https://www.tensorflow.org/api_docs/python/tf/keras/Input), \\`tf.compat.v1.keras.layers.Input\\`\n\n\u003cbr /\u003e\n\n tf.keras.Input(\n shape=None,\n batch_size=None,\n name=None,\n dtype=None,\n sparse=None,\n tensor=None,\n ragged=None,\n type_spec=None,\n **kwargs\n )\n\nA Keras tensor is a symbolic tensor-like object, which we augment with\ncertain attributes that allow us to build a Keras model just by knowing the\ninputs and outputs of the model.\n\nFor instance, if `a`, `b` and `c` are Keras tensors,\nit becomes possible to do:\n`model = Model(input=[a, b], output=c)`\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|--------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `shape` | A shape tuple (integers), not including the batch size. For instance, `shape=(32,)` indicates that the expected input will be batches of 32-dimensional vectors. Elements of this tuple can be None; 'None' elements represent dimensions where the shape is not known. |\n| `batch_size` | optional static batch size (integer). |\n| `name` | An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |\n| `dtype` | The data type expected by the input, as a string (`float32`, `float64`, `int32`...) |\n| `sparse` | A boolean specifying whether the placeholder to be created is sparse. Only one of 'ragged' and 'sparse' can be True. Note that, if `sparse` is False, sparse tensors can still be passed into the input - they will be densified with a default value of 0. |\n| `tensor` | Optional existing tensor to wrap into the `Input` layer. If set, the layer will use the [`tf.TypeSpec`](../../tf/TypeSpec) of this tensor rather than creating a new placeholder tensor. |\n| `ragged` | A boolean specifying whether the placeholder to be created is ragged. Only one of 'ragged' and 'sparse' can be True. In this case, values of 'None' in the 'shape' argument represent ragged dimensions. For more information about RaggedTensors, see [this guide](https://www.tensorflow.org/guide/ragged_tensor). |\n| `type_spec` | A [`tf.TypeSpec`](../../tf/TypeSpec) object to create the input placeholder from. When provided, all other args except name must be None. |\n| `**kwargs` | deprecated arguments support. Supports `batch_shape` and `batch_input_shape`. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A `tensor`. ||\n\n\u003cbr /\u003e\n\n#### Example:\n\n # this is a logistic regression in Keras\n x = Input(shape=(32,))\n y = Dense(16, activation='softmax')(x)\n model = Model(x, y)\n\nNote that even if eager execution is enabled,\n`Input` produces a symbolic tensor-like object (i.e. a placeholder).\nThis symbolic tensor-like object can be used with lower-level\nTensorFlow ops that take tensors as inputs, as such: \n\n x = Input(shape=(32,))\n y = tf.square(x) # This op will be treated like a layer\n model = Model(x, y)\n\n(This behavior does not work for higher-order TensorFlow APIs such as\ncontrol flow and being directly watched by a [`tf.GradientTape`](../../tf/GradientTape)).\n\nHowever, the resulting model will not track any variables that were\nused as inputs to TensorFlow ops. All variable usages must happen within\nKeras layers to make sure they will be tracked by the model's weights.\n\nThe Keras Input can also create a placeholder from an arbitrary\n[`tf.TypeSpec`](../../tf/TypeSpec), e.g: \n\n x = Input(type_spec=tf.RaggedTensorSpec(shape=[None, None],\n dtype=tf.float32, ragged_rank=1))\n y = x.values\n model = Model(x, y)\n\nWhen passing an arbitrary [`tf.TypeSpec`](../../tf/TypeSpec), it must represent the signature of\nan entire batch instead of just one example.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|----------------------------------------------------------------------------|\n| `ValueError` | If both `sparse` and `ragged` are provided. |\n| `ValueError` | If both `shape` and (`batch_input_shape` or `batch_shape`) are provided. |\n| `ValueError` | If `shape`, `tensor` and `type_spec` are None. |\n| `ValueError` | If arguments besides `type_spec` are non-None while `type_spec` is passed. |\n| `ValueError` | if any unrecognized parameters are provided. |\n\n\u003cbr /\u003e"]]