【问题标题】:Tensorflow - ValueError: Shape must be rank 1 but is rank 0 for 'ParseExample/ParseExample'Tensorflow - ValueError:形状必须为 1 级,但对于 'ParseExample/ParseExample' 为 0 级
【发布时间】:2018-02-10 03:15:36
【问题描述】:

我有一个 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)
})

任何想法

【问题讨论】:

  • 您需要提供更多详细信息,最好是展示问题的小型、独立的示例。您说parse_single_example 有效,但您的代码使用parse_single_example。你能给出给出错误的准确代码吗?
  • @PeterHawkins 更改了代码。不小心输入了parse_single_example 而不是parse_example

标签: python tensorflow


【解决方案1】:

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

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

【讨论】:

  • 你也可以找parse_single_example工作;在这种情况下,不需要批次。
【解决方案2】:

我刚才也遇到了类似的问题。尝试在 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)
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-30
    • 2020-10-17
    • 2021-11-21
    • 1970-01-01
    • 2019-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多