xinyue Liu
xinyue Liu

Reputation: 157

Difference between `tf.reshape(a, [m, n])` and `tf.transpose(tf.reshape(a, [n, m]))`?

Actually, I'm doing the homework "Art Generation with Neural Style Transfer" of deeplearning.ai on coursera. In the function compute_layer_style_cost(a_S, a_G):

a_S = tf.reshape(a_S, [n_H*n_W, n_C])
a_G = tf.reshape(a_G, [n_H*n_W, n_C])

GS = gram_matrix(tf.transpose(a_S))
GG = gram_matrix(tf.transpose(a_G))

Why does this code give the right answer, however, the following doesn't:

a_S = tf.reshape(a_S, [n_C, n_H*n_W])
a_G = tf.reshape(a_G, [n_C, n_H*n_W])

GS = gram_matrix(a_S)
GG = gram_matrix(a_G)

Upvotes: 6

Views: 3313

Answers (1)

Maxim
Maxim

Reputation: 53768

Here's a trivial example that shows the difference between these two expressions:

import tensorflow as tf
tf.InteractiveSession()

x = tf.range(0, 6)
a = tf.reshape(x, [3, 2])
b = tf.transpose(tf.reshape(x, [2, 3]))

print(x.eval())
print(a.eval())
print(b.eval())

The result:

[0 1 2 3 4 5]

[[0 1]
 [2 3]
 [4 5]]

[[0 3]
 [1 4]
 [2 5]]

As you can notice, a and b are different, though have the same shape. That's because the first reshaping "splits" x into [0 1], [2 3] and [4 5], while the second reshaping into [0 1 2] and [3 4 5].

Upvotes: 4

Related Questions