Tensorflow - 值错误:'ParseExample/ParseExample'的形状必须是等级1,但等级为0。

8

我有一个Ubuntu对话语料库的.tfrecords文件。我正在尝试读取整个数据集,以便将上下文和话语分成批次。使用tf.parse_single_example,我可以读取单个示例。我尝试使用tf.parse_example,但我得到以下错误

ValueError: Shape must be rank 1 but is rank 0 for 'ParseExample/ParseExample' (op: 'ParseExample') with input shapes: [], [0], [], [], [], [], [], [0], [0], [0], [0], [0].

我不确定该怎么处理。我使用的代码出现了错误 -

import tensorflow as tf    
TRAIN_FILE_TFREC = 'data/train.tfrecords'

filename_queue = tf.train.string_input_producer([TRAIN_FILE_TFREC])

reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)

features = tf.parse_example(serialized_example, 
features = {
"context" : tf.FixedLenFeature([160], tf.int64),
"context_len" : tf.FixedLenFeature([1], tf.int64),
"utterance" : tf.FixedLenFeature([80], tf.int64),
"utterance_len" : tf.FixedLenFeature([1], tf.int64),
"label" : tf.FixedLenFeature([1], tf.int64)
})

任何想法。

1
你需要提供更多细节,最好是一个小的、自包含的示例来展示问题。你说parse_single_example起作用了,但你的代码使用了parse_single_example。你能展示给我们出错的精确代码吗? - Peter Hawkins
@PeterHawkins修改了代码。不小心写成了parse_single_example而不是parse_example - Clock Slave
2个回答

9

要使用tf.parse_example,您需要先对示例进行批处理:

batch = tf.train.batch([serialized_example], num_examples, capacity=num_examples)
parsed_examples = tf.parse_example(batch, feature_spec)

1
您也可以尝试使用parse_single_example函数;在这种情况下,不需要批处理。 - Richard

3

我刚刚遇到了类似的问题。试着将serialized_example加上括号,转换成一个列表:

features = tf.parse_example([serialized_example], 
features = {
"context" : tf.FixedLenFeature([160], tf.int64),
"context_len" : tf.FixedLenFeature([1], tf.int64),
"utterance" : tf.FixedLenFeature([80], tf.int64),
"utterance_len" : tf.FixedLenFeature([1], tf.int64),
"label" : tf.FixedLenFeature([1], tf.int64)
})

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接