All we need is an easy explanation of the problem, so here it is.
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)
)
How to solve :
I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.
Method 1
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.
Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0