我编写了以下代码片段来编写和读取TFRecord。
最后一个tf.run()
语句阻止python响应任何内容。这是什么原因?
fn = 'tmp.tfrecord'
seqs = [[1,2,3], [0,1,0]]
writer = tf.python_io.TFRecordWriter(fn)
for seq in seqs:
ex = tf.train.Example(features=
tf.train.Features(feature={'seq': tf.train.Feature(int64_list=tf.train.Int64List(value=seq))}))
writer.write(ex.SerializeToString())
writer.close()
# Now read the written records:
filename_queue = tf.train.string_input_producer([fn])
reader = tf.TFRecordReader()
key, serialized_example = reader.read(filename_queue)
features = { 'seq': tf.FixedLenFeature([], dtype=tf.int64) }
ex_parsed = tf.parse_single_example(
serialized=serialized_example, features=features)
print(ex_parsed) # -> prints a tensor
with tf.Session() as sess:
print(sess.run([ex_parsed['seq']]))
我尝试在代码中包含tf.train.Coordinator()
,但无法使其工作。
答案 0 :(得分:1)
程序在最后一行挂起,因为在评估tf.TFRecordReader
或tf.train.string_input_producer()
的输出之前,您需要start queue runners。在创建会话后立即添加对tf.train.start_queue_runners(sess)
的呼叫。
或者,您可以使用新的tf.data
API(在TensorFlow 1.4或更高版本中;在TensorFlow 1.2和1.3中使用tf.contrib.data
)来读取数据,而无需担心队列运行程序:
# A `tf.data.Dataset` containing all of the records in the file named `fn`.
records = tf.data.TFRecordDataset(fn)
features = {'seq': tf.FixedLenFeature([], dtype=tf.int64)}
# A `tf.data.Dataset` whose elements are dictionaries mapping feature names
# (in this case 'seq') to tensors, based on `features`.
parsed = records.map(lambda x: tf.parse_single_example(x, features))
# Create a `tf.data.Iterator` to access individual elements of a `Dataset`. The
# system will take care of creating any background threads for you.
iterator = parsed.make_one_shot_iterator()
# `ex_parsed` represents the next element of the iterator. It is a dictionary
# mapping feature names to tensors.
ex_parsed = iterator.get_next()
with tf.Session() as sess:
print(sess.run(ex_parsed['seq']))