Mike Azatov
Mike Azatov

Reputation: 442

Keras Flatten layer returns output shape (None, None)

So, I noticed this strange behavior of Flatten layer of Keras. I'm using TF1.15 and Keras 2.3.0.

Basically the output of the Flatten layer has an unknown shape. It's hard to troubleshoot the model when you can't keep track of the shape. Why is this happening with Flatten layer, and can I do something so it recognizes the shape?

from keras.layers import Flatten
inputs = Input(shape=(3,2,4))
prediction = Flatten()(inputs)
print(inputs.shape, prediction.shape)

(?, 3, 2, 4) (?, ?)

Upvotes: 1

Views: 1111

Answers (1)

AloneTogether
AloneTogether

Reputation: 26698

Try using tf.keras instead of just keras:

import tensorflow as tf
print(tf.__version__)
inputs = tf.keras.layers.Input(shape=(3,2,4))
prediction = tf.keras.layers.Flatten()(inputs)
print(inputs.shape, prediction.shape)
1.15.2
(?, 3, 2, 4) (?, 24)

Upvotes: 3

Related Questions