简体   繁体   中英

Why isn't Tensorflow/Keras Flatten layer flattening my array?

I am trying to use the tensorflow.keras.layers.Flatten layer outside of a model to flatten a 4x4 tensor. I can't figure out why the Flatten layer isn't actually flattening my array.

Here is my code:

import tensorflow as tf
import numpy as np

flayer = tf.keras.layers.Flatten()
X = tf.constant(np.random.random((4,4)),dtype=tf.float32)
Xf = flatten_layer(X)
print(Xf)

and print(Xf) shows

tf.Tensor(
[[0.9866459  0.52488756 0.86211777 0.06254051]
 [0.32552275 0.23201537 0.8646714  0.80754006]
 [0.55823076 0.51929855 0.538077   0.4111973 ]
 [0.95845264 0.14468837 0.30223057 0.09648433]], shape=(4, 4), dtype=float32)

Why doesn't my flatten layer output a 16x1 tensor?

That's because the Flatten() layer assumes that the first dimension is the number of samples, so it returns 4 flattened rows. You have 4 observations, and 1D input for each of these already. It would behave differently if you had data with shape (32, 28, 28, 1) , for example, which has a higher dimensionality for each row.

import tensorflow as tf
import numpy as np

flayer = tf.keras.layers.Flatten()
X = tf.constant(np.random.random((32, 28, 28, 1)),dtype=tf.float32)
Xf = flayer(X)
print(Xf.shape)
(32, 784)

If you meant to flatten one observation with shape (4, 4) , you should add a batch dimension for it to work:

X = tf.constant(np.random.random((1, 4, 4)),dtype=tf.float32)
Xf = flayer(X)
print(Xf.shape)
(1, 16)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM