Skip to main content
 首页 » 编程设计

python之Tensorflow之喂食不同长度的例子

2024年10月17日32birdshome

我的每个训练示例都是一个长度不同的列表。 我正在尝试找到一种方法将这些示例提供给图表。 下面是我通过创建一个列表来实现这一点的尝试,该列表的元素是具有未知维度的占位符。

graph2 = tf.Graph() 
with graph2.as_default(): 
    A = list () 
    for i in np.arange(3): 
        A.append(tf.placeholder(tf.float32 ,shape = [None,None])) 
    A_size = tf.shape(A) 
 
with tf.Session(graph=graph2) as session: 
  tf.initialize_all_variables().run() 
  feed_dict = {A[0]:np.zeros((3,7)) ,A[1] : np.zeros((3,2)) , A[2] : np.zeros((3,2)) } 
  print ( type(feed_dict)) 
  B = session.run(A_size ,feed_dict=feed_dict) 
print type(B) 

但是我得到了以下错误:

InvalidArgumentError: Shapes of all inputs must match: values[0].shape = [3,7] != values[1].shape = [3,2] 

关于如何解决它的任何想法?

请您参考如下方法:

来自tf.placeholder的文档:

shape: The shape of the tensor to be fed (optional). If the shape is not specified, you can feed a tensor of any shape.

您需要编写 shape=None 而不是 shape=[None, None]。使用您的代码,Tensorflow 不知道您正在处理可变大小的输入。