简体   繁体   中英

How does the Flatten() Layer work in Tensorflow?

Im currently working with tensorflow and neural networks and im quite new to the topic.

Im having a stack of 4 images passed to my conv network in the shape of (4,160,120,1) as the images are in grayscale.

After passing my images through the neural network i wanted to flatten the images into one long array that gets passed to dense layers.

But after using Flatten() on the output of my neural network i get a 2 dimensional array in the shape of (4, 2240) instead of a long one dimensional array.

Why is that and what would i need to do to flatten it to a 1D array? Just use Flatten() again?

Im asking because my goal is to concatenate the flattened with another input which is one dimensional but i get an array that concatenate wont work bc they dont have the same shape

Thanks for the help!

Keras layers don't affect batch dimension. You have to use tf.reshape() before feeding your data to the model:

input1 = tf.reshape(input1, (-1,))
output = model(input1, input2) # exclude Flatten layer from your model

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