tf.reshape vs tf.contrib.layers.flatten

0 votes

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[1]*P3_shape[2]*P3_shape[3]]) <---- Option B

P = tf.reshape(P3, [tf.shape(P3)[0], -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 by eatcodesleeprepeat
• 4,670 points
512 views

1 answer to this question.

0 votes

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[1] * p3_shape[2] * p3_shape[3]])  # <---- Option B

p_c = tf.reshape(p3, [tf.shape(p3)[0], -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 Priyaj
• 56,920 points

Related Questions In Python

0 votes
1 answer

SKLearn NMF Vs Custom NMF

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

answered May 9, 2018 in Python by charlie_brown
• 7,720 points
386 views
+1 vote
2 answers

Difference between append vs. extend list methods in Python

Python append() method adds an element to ...READ MORE

answered Aug 21 in Python by germyrinn
• 180 points
253 views
+2 votes
2 answers

Compiled vs Interpreted Languages

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

answered Dec 3, 2018 in Python by allenvarna
• 520 points
321 views
0 votes
1 answer

Difference between append vs. extend list methods in Python

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

answered Aug 8, 2018 in Python by bug_seeker
• 15,350 points
143 views
+1 vote
2 answers

how can i count the items in a list?

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

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 342 views
0 votes
1 answer

Python string formatting: % vs. .format

To answer your first question... .format just ...READ MORE

answered Aug 17, 2018 in Python by Priyaj
• 56,920 points
33 views
+1 vote
2 answers

Python string formatting: % vs. .format

Using Python format() function is what the ...READ MORE

answered Apr 11 in Python by Dasa Ravi
77 views