So I am running a CNN for a classification problem. I have 3 conv layers with 3 pooling layers. P3 is the output of the last pooling layer, whose dimensions are: [Batch_size, 4, 12, 48]_, and I want to flatten that matrix into a [Batch_size, 2304] size matrix, being 2304 = 4*12*48. I had been working with "Option A" (see below) for a while, but one day I wanted to try out "Option B", which would theoretically give me the same result. However, it did not. I have cheked the following thread before

Is tf.contrib.layers.flatten(x) the same as tf.reshape(x, [n, 1])?

but that just added more confusion, since trying "Option C" (taken from the aforementioned thread) gave a new different result.

```P3 = tf.nn.max_pool(A3, ksize = [1, 2, 2, 1], strides = [1, 2, 2, 1], padding='VALID')

P3_shape = P3.get_shape().as_list()

P = tf.contrib.layers.flatten(P3)                             <-----Option A

P = tf.reshape(P3, [-1, P3_shape*P3_shape*P3_shape]) <---- Option B

P = tf.reshape(P3, [tf.shape(P3), -1])                     <---- Option C```

I am more inclined to go with "Option B" since that is the one I have seen in a video by Dandelion Mane (https://www.youtube.com/watch?v=eBbEDRsCmv4&t=631s), but I would like to understand why these 3 options are giving different results.

Thanks for any help! Oct 10, 2018 in Python 684 views

## 1 answer to this question.

All 3 options reshape identically:

```import tensorflow as tf
import numpy as np

p3 = tf.placeholder(tf.float32, [None, 1, 2, 4])

p3_shape = p3.get_shape().as_list()

p_a = tf.contrib.layers.flatten(p3)                              # <-----Option A

p_b = tf.reshape(p3, [-1, p3_shape * p3_shape * p3_shape])  # <---- Option B

p_c = tf.reshape(p3, [tf.shape(p3), -1])                      # <---- Option C

print(p_a.get_shape())
print(p_b.get_shape())
print(p_c.get_shape())

with tf.Session() as sess:
i_p3 = np.arange(16, dtype=np.float32).reshape([2, 1, 2, 4])
print("a", sess.run(p_a, feed_dict={p3: i_p3}))
print("b", sess.run(p_b, feed_dict={p3: i_p3}))
print("c", sess.run(p_c, feed_dict={p3: i_p3}))```

This code yields the same result 3 times. Your different results are caused by something else and not by the reshaping. answered Oct 10, 2018 by
• 57,510 points

## SKLearn NMF Vs Custom NMF

The choice of the optimizer has a ...READ MORE

+1 vote

## Compiled vs Interpreted Languages

Compiled languages are written in a code ...READ MORE

## Difference between append vs. extend list methods in Python

append: Appends object at the end. x = ...READ MORE

## how do i change string to a list?

suppose you have a string with a ...READ MORE

+1 vote

## how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

## how do i use the enumerate function inside a list?

can you give an example using a ...READ MORE