首页 文章

如何在张量流中使用张量的动态形状

提问于
浏览
2

如何为以下计算构建张量流图?我现在的问题是如何使用张量A的形状信息,它具有可变的形状大小 .

A = tf.placeholder(tf.float32, [None,10])
B = tf.Variable(tf.random_normal([10,20]))
C = tf.matmul(A, B)
D = tf.matmul(tf.transpose(C), A) # the value of A.shape[0]

1 回答

  • 1

    您已经将张量 A 的值传递给占位符,当您这样做时,您已经知道它的形状 . 我会为你关心的形状创建另一个占位符并传递它:

    import tensorflow as tf
    import numpy as np
    
    A = tf.placeholder(tf.float32, [None,10])
    L = tf.placeholder(tf.float32, None)
    
    B = tf.Variable(tf.random_normal([10,20]))
    C = tf.matmul(A, B)
    D = tf.multiply(tf.transpose(C), L) // L is a number, matmul does not multiply matrix with a number
    
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        a = np.zeros((5, 10), dtype=np.float32)
        l = a.shape[0]
        sess.run(D, {A: a, L: l})
    

相关问题