Input
functiontf.keras.Input(
shape=None,
batch_size=None,
name=None,
dtype=None,
sparse=None,
tensor=None,
ragged=None,
type_spec=None,
**kwargs
)
Input()
is used to instantiate a Keras tensor.
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)
Arguments
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.float32
, float64
, int32
...)sparse
is False, sparse tensors can still be passed into the
input - they will be densified with a default value of 0.Input
layer.
If set, the layer will use the tf.TypeSpec
of this tensor rather
than creating a new placeholder tensor.tf.TypeSpec
object to create the input placeholder from.
When provided, all other args except name must be None.batch_shape
and
batch_input_shape
.Returns
A tensor
.
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
sparse
and ragged
are provided.shape
and (batch_input_shape
or batch_shape
) are
provided.shape
, tensor
and type_spec
are None.type_spec
are non-None while
type_spec
is passed.