在Tensorflow中,我想保存一個多維數組到TFRecord。例如:tf.SequenceExample與多維數組
[[1, 2, 3], [1, 2], [3, 2, 1]]
由於我試圖解決的任務是連續的,我想使用Tensorflow的tf.train.SequenceExample()
和寫入數據時,我成功地在數據寫入TFRecord文件。但是,當我嘗試加載使用tf.parse_single_sequence_example
的TFRecord文件中的數據,我招呼着大量神祕的錯誤:
W tensorflow/core/framework/op_kernel.cc:936] Invalid argument: Name: , Key: input_characters, Index: 1. Number of int64 values != expected. values size: 6 but output shape: []
E tensorflow/core/client/tensor_c_api.cc:485] Name: , Key: input_characters, Index: 1. Number of int64 values != expected. values size: 6 but output shape: []
我使用的嘗試加載我的數據是下面的功能:
def read_and_decode_single_example(filename):
filename_queue = tf.train.string_input_producer([filename],
num_epochs=None)
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
context_features = {
"length": tf.FixedLenFeature([], dtype=tf.int64)
}
sequence_features = {
"input_characters": tf.FixedLenSequenceFeature([], dtype=tf.int64),
"output_characters": tf.FixedLenSequenceFeature([], dtype=tf.int64)
}
context_parsed, sequence_parsed = tf.parse_single_sequence_example(
serialized=serialized_example,
context_features=context_features,
sequence_features=sequence_features
)
context = tf.contrib.learn.run_n(context_parsed, n=1, feed_dict=None)
print context
,我使用保存數據的功能是在這裏:
# http://www.wildml.com/2016/08/rnns-in-tensorflow-a-practical-guide-and-undocumented-features/
def make_example(input_sequence, output_sequence):
"""
Makes a single example from Python lists that follows the
format of tf.train.SequenceExample.
"""
example_sequence = tf.train.SequenceExample()
# 3D length
sequence_length = sum([len(word) for word in input_sequence])
example_sequence.context.feature["length"].int64_list.value.append(sequence_length)
input_characters = example_sequence.feature_lists.feature_list["input_characters"]
output_characters = example_sequence.feature_lists.feature_list["output_characters"]
for input_character, output_character in izip_longest(input_sequence,
output_sequence):
# Extend seems to work, therefore it replaces append.
if input_sequence is not None:
input_characters.feature.add().int64_list.value.extend(input_character)
if output_characters is not None:
output_characters.feature.add().int64_list.value.extend(output_character)
return example_sequence
任何幫助將受到歡迎。
嗨,你能提供更多的上下文嗎?最好提供一個可以實際運行和測試的最小示例,包括如何將數據保存到文件的步驟。 – jlarsch
您的示例非常難以遵循,如果編輯示例以包含相關上下文,則會獲得更多幫助。例如 - 查看您在代碼中添加註釋的鏈接,很明顯,您生成序列示例的片段不包含實際寫入數據的代碼。 –