解析TFRecord时输出张量错误的形状

时间:2017-02-01 20:54:52

标签: python parsing tensorflow

当我尝试解析TFRecord文件时,我收到以下错误:

(<tf.Tensor 'ParseSingleExample/Squeeze_tokens:0' shape=() dtype=int64>, <tf.Tensor 'ParseSingleExample/Squeeze_labels:0' shape=() dtype=int64>)
W tensorflow/core/framework/op_kernel.cc:940] Invalid argument: Name: <unknown>, Key: tokens, Index: 0.  Number of int64 values != expected.  values size: 5 but output shape: []

据我所知,行错误意味着输出张量是错误的形状。我一直在关注here的信息。在标准TensorFlow格式标题下,它列出了两个编码和解析mnist数据集的github链接。在执行解析的文件中,就我所知,没有参考设置输出张量的形状。我不知道如何纠正这一点,我已经尝试了所有我能想到的东西。我在下面提供了我的代码。

import tensorflow as tf
import tempfile
import os

def make_example(sequence, label):
    #converts a sequence and its labels into a SequenceExample

    # The object we return
    ex = tf.train.Example()
    # Feature lists for the two sequential features of our example
    fl_tokens = ex.features.feature["tokens"]
    fl_labels = ex.features.feature["labels"]
    for token in sequence:
        fl_tokens.int64_list.value.append(token)

    fl_labels.int64_list.value.append(label)

    return ex

def parse_example(filename_queue):
    #reads a TFRecord into its constituent parts

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

    # Parse the example
    sequence_parsed = tf.parse_single_example(
        serialized=example,
        features={
            "tokens": tf.FixedLenFeature([], dtype=tf.int64),
            "labels": tf.FixedLenFeature([], dtype=tf.int64)
        }
    )

    return sequence_parsed["tokens"], sequence_parsed["labels"]

if __name__ == '__main__':
    sequence = [1,2,3,4,5]
    label = 1
    #################################
    # Write all examples into a TFRecords file
    #################################
    with tempfile.NamedTemporaryFile(dir=".") as fp:
        writer = tf.python_io.TFRecordWriter(fp.name)

        ex = make_example(sequence, label)
        writer.write(ex.SerializeToString())
        writer.close()

        #################################
        # Read contents of TFRecord file
        #################################

        filename = os.path.join(os.getcwd(), fp.name)
        filename_queue = tf.train.string_input_producer([filename])

        with tf.Session() as sess:
            coord = tf.train.Coordinator()
            tf.train.start_queue_runners(coord=coord)
            out_sequence, out_label = parse_example(filename_queue)


            # Print the contents of a single entry:
            v1,v2 = sess.run([out_sequence, out_label])
            print(v1,v2)

-

编辑:所以我发现我可以通过更改行来摆脱错误并使事情有效

"tokens": tf.FixedLenFeature([], dtype=tf.int64)

"tokens": tf.FixedLenFeature([5], dtype=tf.int64)

这会强制输出张量的形状。我仍然不清楚为什么我需要为我所呈现的情况指定这个,而张量流方向的例子却没有。如果有人能澄清我会非常感激。

0 个答案:

没有答案