session.run上的Tensorflow ValueError与批处理培训

时间:2016-11-21 14:45:31

标签: python tensorflow

我培训的RNN类似于tensorflow教程中的RNN,用于顺序数据。数据为[batch_size,step,dimension],标签为[batch_size,num_classes]。

由于不同样本的序列长度不同,我想创建批量训练 - 每次我抓取32个样本数据,将它们填充到最长的序列大小,然后将它们输入rnn图形进行训练。

数据定义为:

data = DataGenerator(data_path, label_path)
train_data, train_label, train_seqlen, test_data, test_label = data.train_test_data(0.2)    
x = tf.placeholder(tf.float32, [batch_size, None, num_dim])
y = tf.placeholder(tf.float32, [batch_size, num_classes])
seqlen = tf.placeholder(tf.int32, [batch_size])
model = VariableSeqModel(x, y, seqlen)

Train_data是[batch_size,step,dim],train_label是[batch_size,num_classes]。 Seqlen是[batch_size,1],用于记录每批中样品的实际序列长度。是否正确我将x定义为[batch_size,None,num_dim]以获得可变序列长度?

定义RNN和数据结构后,按照此代码示例启动会话:

with tf.Session() as sess:
    sess.run(tf.initialize_all_variables())
    step = 1
    while step*batch_size < 1000:
        batch_xx, batch_y, batch_seqlen = data.next(batch_size, train_data, train_label, train_seqlen)
        batch_x = data.batch_padding(batch_xx,batch_seqlen)
        sess.run(model.optimize, feed_dict={x: batch_xx, y: batch_y, seqlen: batch_seqlen})

        step += 1

我遇到了以下ValueError(下面的stacktrace):

dynamic_rnn.py in <module>()
--> 129  sess.run(model.optimize, feed_dict={x: batch_xx, y: batch_y, seqlen: batch_seqlen})

tensorflow/python/client/session.pyc in run(self, fetches, feed_dict, options, run_metadata)
    708    try:
    709    result = self._run(None, fetches, feed_dict, options_ptr,
--> 710   run_metadata_ptr)
    711    if run_metadata:
    712    proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

tensorflow/python/client/session.pyc in _run(self, handle, fetches, feed_dict, options, run_metadata)
    879   ' to a larger type (e.g. int64).')
    880 
--> 881   np_val = np.asarray(subfeed_val, dtype=subfeed_dtype)
    882 
    883   if not subfeed_t.get_shape().is_compatible_with(np_val.shape):

numpy/core/numeric.pyc in asarray(a, dtype, order)
    480 
    481 """
--> 482 return array(a, dtype, copy=False, order=order)
    483 
    484 def asanyarray(a, dtype=None, order=None):

ValueError: setting an array element with a sequence.

我很难过。任何帮助表示赞赏!

2 个答案:

答案 0 :(得分:0)

我不是专家,但在我看来问题就在这里

  

Seqlen是[batch_size,1]

正如tensorflow教程所示,序列长度应为

  

sequence_length :(可选)大小为[batch_size]的int32 / int64向量。

您可以尝试相应地填充seqlen数组。 希望这会有所帮助。

答案 1 :(得分:0)

问题解决了。只需声明seqlen = tf.placeholder(tf.int32, [None])即可。 &#34; [无]&#34;这里表示批量大小的动态张量类型,而batch_size只是tf.int32。