0

I want to reshape a tensor matrix into specific shape. After I did the ops, I found the matrix itself changed. I don't know why this is happened.

tf.reset_default_graph()

with tf.Session() as test:
    tf.set_random_seed(1)
    a_S = tf.random_normal([1, 1,1,3], mean=1, stddev=4)
    a_G = tf.random_normal([1, 1,1,3], mean=1, stddev=4)
    J_style_layer = compute_layer_style_cost(a_S, a_G)

    print("J_style_layer = " + str(J_style_layer.eval()))

The following is the definition of called function compute_layer_style_cost

def compute_layer_style_cost(a_S, a_G):
    """
    Arguments:
    a_S -- tensor of dimension (1, n_H, n_W, n_C), hidden layer activations 
representing style of the image S 
    a_G -- tensor of dimension (1, n_H, n_W, n_C), hidden layer activations 
representing style of the image G

    Returns: 
    J_style_layer -- tensor representing a scalar value, style cost defined 
above by equation (2)
    """

    ### START CODE HERE ###
    # Retrieve dimensions from a_G (≈1 line)
    m, n_H, n_W, n_C = a_S.get_shape().as_list()
    print("m=>", m, "n_H=>", n_H, "n_W=>", n_W, "n_C=>", n_C)
    print("a_S.shape=>", a_S.shape)
    print("a_S=>",a_S.eval())

    # Reshape the images to have them of shape (n_C, n_H*n_W) (≈2 lines)
    a_S = tf.reshape(a_S, [n_C, n_H*n_W])
    a_G = tf.reshape(a_G, [n_C, n_H*n_W])
    print("a_S.shape=>", a_S.shape)
    print("a_S=>",a_S.eval())

After I ran it, I got following result.

m=> 1 n_H=> 1 n_W=> 1 n_C=> 3
a_S.shape=> (1, 1, 1, 3)
a_S=> [[[[-1.68344498  1.89428568  4.18909216]]]]
a_S.shape=> (3, 1)
a_S=> [[-4.78795481]
 [ 5.39861012]
 [ 4.57472849]]

The above result shows, after reshape ops, the value of tensor matrix has changed. And I don't know why this happen exactly.

Jiancong
  • 67
  • 1
  • 7

1 Answers1

2

After refer Operations on random variables not working properly in Tensorflow and https://www.tensorflow.org/programmers_guide/graphs, it seems I don't run two random variable computation in the same session, I changed my code into

with tf.Session() as sess:
    print(sess.run([a_S, a_S_re]))

And it works.

Jiancong
  • 67
  • 1
  • 7