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

How to use the dynamic shape of a tensor in tensorflow

如何为以下计算构建张量流图?我现在的问题是如何使用形状大小可变的张量 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]

您已经将张量 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})