【问题标题】:Difference between `tf.reshape(a, [m, n])` and `tf.transpose(tf.reshape(a, [n, m]))`?`tf.reshape(a, [m, n])` 和 `tf.transpose(tf.reshape(a, [n, m]))` 之间的区别?
【发布时间】:2018-09-30 01:14:05
【问题描述】:

其实我在 coursera 上做 deeplearning.ai 的作业“Art Generation with Neural Style Transfer”。在函数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))

为什么这段代码给出了正确的答案,但是下面的却没有:

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)

【问题讨论】:

    标签: python numpy matrix tensorflow deep-learning


    【解决方案1】:

    这里有一个简单的例子来说明这两个表达式之间的区别:

    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())
    

    结果:

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

    如您所见,ab 是不同的,但形状相同。这是因为第一次重塑“分裂”x[0 1][2 3][4 5],而第二次重塑为 [0 1 2][3 4 5]

    【讨论】:

      猜你喜欢
      • 2021-08-07
      • 1970-01-01
      • 2018-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-16
      • 1970-01-01
      相关资源
      最近更新 更多