如何在张量流中使用张量的动态形状
问题描述:
如何为以下计算建立张量流图?我现在面临的问题是如何使用张量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})
谢谢。这工作。这并不完美,因为每次你想要抓取D时,都必须记住喂食形状。 – Negelis
其实这是一个很好的做法,有一个帮助器生成所有的饲料字典,你做的事情就像:'sess.run(,your_dict_helper())' –
我明白了。非常感谢! – Negelis