在Python中,我尝试将空列表提供给第一维None
的多维张量流占位符。
import tensorflow as tf
a = [0,1,2]
b = [[4,5,6],
[7,8,9]]
c = []
# 1-D and 2-D placeholders
p_1D = tf.placeholder(shape=(None), dtype=tf.float32)
p_2D = tf.placeholder(shape=(None,3), dtype=tf.float32)
with tf.Session() as sess:
print(sess.run(p_1D, feed_dict={p_1D:a})) # prints [0.,1.,2.]
print(sess.run(p_2D, feed_dict={p_2D:b})) # prints [[4.,5.,6.],
# [7.,8.,9.]]
print(sess.run(p_1D, feed_dict={p_1D:c})) # prints []
print(sess.run(p_2D, feed_dict={p_2D:c})) # raises ValueError
具体而言,引发的错误是ValueError: Cannot feed value of shape (0,) for Tensor 'Placeholder_3:0', which has shape '(?, 3)
。
从中我可以看到,如果空列表只有一个维度(None
),则可以将空列表提供给占位符,但是更高维度的占位符似乎不允许它。有没有办法允许更高维度的占位符接受空列表作为输入?
(是的,我知道,想要输入一个空列表似乎很愚蠢,但如果有解决方案,它仍然有用。)
答案 0 :(得分:1)
feed_dict={p_1D: np.zeros(shape=(0,), dtype=np.float32), p_2D: np.zeros(shape=(0, 3), dtype=np.float32)}
等。