Home > front end >  Why does passing a KerasTensor to Sequential.add not raise a TypeError?
Why does passing a KerasTensor to Sequential.add not raise a TypeError?

Time:04-02

In the definition of keras.Sequential.add, we have (in accordance with the docs)

    if isinstance(layer, tf.Module):
      if not isinstance(layer, base_layer.Layer):
        layer = functional.ModuleWrapper(layer)
    else:
      raise TypeError('The added layer must be an instance of class Layer. '
                      f'Received: layer={layer} of type {type(layer)}.')

and yet, when I execute

model = keras.Sequential()
model.add(keras.Input(100))

a TypeError is not raised. keras.Input returns a tensor, and the definition of a KerasTensor shows that it inherits from an Object, and not a Layer.

Why can I add an Input to a Sequential, rather than being required to add an InputLayer as I would expect?

CodePudding user response:

Good question, if you take a look at the source code, we read:

If we are passed a Keras tensor created by keras.Input(), we can extract the input layer from its keras history and use that without any loss of generality.

It seems like keras.Input is being internally converted into an InputLayer here:

    if hasattr(layer, '_keras_history'):
      origin_layer = layer._keras_history[0]
      if isinstance(origin_layer, input_layer.InputLayer):
        layer = origin_layer

This can also be verified with this snippet:

inputs = tf.keras.Input(100)
print(inputs._keras_history[0])
<keras.engine.input_layer.InputLayer object at 0x7f9379a0cdd0>

And that is the reason you are not seeing any errors.

  • Related