我编写了以下代码片段,用于编写和读取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(),但也无法让它工作。
发布于 2017-10-31 16:00:48
程序挂在最后一行,因为在计算tf.TFRecordReader或tf.train.string_input_producer()的输出之前,您需要进行tf.train.string_input_producer()。在创建会话后立即添加对tf.train.start_queue_runners(sess)的调用。
或者,您可以使用新的tf.data API (在TensorFlow 1.4或更高版本中;tf.contrib.data在TensorFlow 1.2和1.3中)读取数据,而不必担心队列运行程序:
# 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']))https://stackoverflow.com/questions/47030529
复制相似问题