2016-10-27 11 views
5

過去数日間、データをtfrecord形式にシリアル化した後、parse_single_sequenceの例を使用してデシリアライズする際に問題が発生しました。私はかなり標準的なRNNモデルで使用するためにデータを取得しようとしていますが、これはtfrecordsフォーマットと関連パイプラインを使用する私の最初の試みです。ここで形状はランク0である必要がありますが、ランク1です。parse_single_sequence_example

は私が午前問題を再現するためにおもちゃの例である:

import tensorflow as tf 
import tempfile 
from IPython import embed 

sequences = [[1, 2, 3], [4, 5, 1], [1, 2]] 
label_sequences = [[0, 1, 0], [1, 0, 0], [1, 1]] 

def make_example(sequence, labels): 

    ex = tf.train.SequenceExample() 

    sequence_length = len(sequence) 
    ex.context.feature["length"].int64_list.value.append(sequence_length) 

    fl_tokens = ex.feature_lists.feature_list["tokens"] 
    fl_labels = ex.feature_lists.feature_list["labels"] 
    for token, label in zip(sequence, labels): 
     fl_tokens.feature.add().int64_list.value.append(token) 
     fl_labels.feature.add().int64_list.value.append(label) 
    return ex 


writer = tf.python_io.TFRecordWriter('./test.tfrecords') 
for sequence, label_sequence in zip(sequences, label_sequences): 
    ex = make_example(sequence, label_sequence) 
    writer.write(ex.SerializeToString()) 
writer.close() 

tf.reset_default_graph() 

file_name_queue = tf.train.string_input_producer(['./test.tfrecords'], num_epochs=None) 

reader = tf.TFRecordReader() 



context_features = { 
    "length": tf.FixedLenFeature([], dtype=tf.int64) 
} 
sequence_features = { 
    "tokens": tf.FixedLenSequenceFeature([], dtype=tf.int64), 
    "labels": tf.FixedLenSequenceFeature([], dtype=tf.int64) 
} 

ex = reader.read(file_name_queue) 

# Parse the example (returns a dictionary of tensors) 
context_parsed, sequence_parsed = tf.parse_single_sequence_example(
    serialized=ex, 
    context_features=context_features, 
    sequence_features=sequence_features 
) 


context = tf.contrib.learn.run_n(context_parsed, n=1, feed_dict=None) 
print(context[0]) 
sequence = tf.contrib.learn.run_n(sequence_parsed, n=1, feed_dict=None) 
print(sequence[0]) 

関連するスタックトレースは次のとおりです。

Traceback (most recent call last): 
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/common_shapes.py", line 594, in call_cpp_shape_fn 
status) 
File "/usr/lib/python3.5/contextlib.py", line 66, in exit 
next(self.gen) 
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/errors.py", line 463, in raise_exception_on_not_ok_status 
pywrap_tensorflow.TF_GetCode(status)) 
tensorflow.python.framework.errors.InvalidArgumentError: Shape must be rank 0 but is rank 1 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
File "my_test.py", line 51, in 
sequence_features=sequence_features 
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/parsing_ops.py", line 640, in parse_single_sequence_example 
feature_list_dense_defaults, example_name, name) 
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/parsing_ops.py", line 837, in _parse_single_sequence_example_raw 
name=name) 
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/gen_parsing_ops.py", line 285, in _parse_single_sequence_example 
name=name) 
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/op_def_library.py", line 749, in apply_op 
op_def=op_def) 
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 2382, in create_op 
set_shapes_for_outputs(ret) 
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 1783, in set_shapes_for_outputs 
shapes = shape_func(op) 
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/common_shapes.py", line 596, in call_cpp_shape_fn 
raise ValueError(err.message) 
ValueError: Shape must be rank 0 but is rank 1 

それはそうけれども、私はgithubの上の潜在的な問題として、これを超える投稿しました私はちょうどそれを間違って使用しているかもしれません:Tensorflow Github Issue 私はちょうど私が実際にここでエラーを作りているかどうかちょうど方法の背景情報と、私は思っていますか?正しい方向への助けがあれば大いにありがたく思っています。数日経っています。皆さんありがとう!

答えて

0

私のところは分かりづらいです。 tf.TFRecordReader.read(queue, name=None)は、例パーサに直接渡していた値ではない(key, value)という値を返すと仮定したときにタプルを返します。

関連する問題