I have seen multiple uses of both tf.keras.layers.Flatten()
(ex. here) and tf.keras.layers.Input()
(ex. here). After reading the documentation, it is not clear to me
- whether either of them uses the other
- whether both can be used interchangeably when introducing to a model an input layer (let's say with dimensions
(64, 64)
)
CodePudding user response:
I think the confusion comes from using a tf.keras.Sequential
model, which does not need an explicit Input
layer. Consider the following two models, which are equivalent:
import tensorflow as tf
model1 = tf.keras.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(5, activation='relu'),
])
model1.build((1, 28, 28, 1))
model2 = tf.keras.Sequential([
tf.keras.layers.Input((28, 28, 1)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(5, activation='relu'),
])
The difference is that I explicitly set the input shape of model2
using an Input
layer. In model1
, the input shape will be inferred when you pass real data to it or call model.build
.
Now regarding the Flatten
layer, this layer simply converts a n-dimensional tensor (for example (28, 28, 1)
) into a 1D tensor (28 x 28 x 1)
. The Flatten
layer and Input
layer can coexist in a Sequential
model but do not depend on each other.