为什么读取一个简单的 tfrecord 会崩溃 python?
Why does reading a simple tfrecord crash python?
我编写了以下用于编写和读取 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()
,但也无法正常工作。
程序在最后一行挂起,因为您需要在创建会话后立即 before evaluating the output of a tf.TFRecordReader
or a tf.train.string_input_producer()
. Add a call to 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']))
我编写了以下用于编写和读取 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()
,但也无法正常工作。
程序在最后一行挂起,因为您需要在创建会话后立即tf.TFRecordReader
or a tf.train.string_input_producer()
. Add a call to 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']))