Tensorflow细节-P190-输入文件队列
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Tensorflow细节-P190-输入文件队列,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1719字,纯文字阅读大概需要3分钟。
内容图文
以下代码要学会几个地方
1、filename = (‘data.tfrecords-%.5d-of-%.5d‘ % (i, num_shards))
这个东西就是要会data.tfrecords-%.5d-of-%.5d两个.5d,
2、记住这两个操作writer = tf.python_io.TFRecordWriter(filename)
与writer = tf.python_io.TFRecordWriter(filename)
3、得到的是以下TFrecoard两个文件
import tensorflow as tf
def _int64_feature(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
num_shards = 2
instances_per_shard = 2
# 以下文件用于存储数据
for i in range(num_shards):
filename = ('data.tfrecords-%.5d-of-%.5d' % (i, num_shards))
# 将Example结构写入TFRecord文件。
writer = tf.python_io.TFRecordWriter(filename)
for j in range(instances_per_shard):
# Example结构仅包含当前样例属于第几个文件以及是当前文件的第几个样本。
example = tf.train.Example(features=tf.train.Features(feature={
'i': _int64_feature(i),
'j': _int64_feature(j)}))
writer.write(example.SerializeToString())
writer.close()
以下是对上面程序生成文件的读取(该讲的已经讲了)
import tensorflow as tf
# 获取一个符合正则表达式的所有文件列表,这样就可以得到所有的符合要求的文件了
files = tf.train.match_filenames_once("data.tfrecords-*") # *是一个通配符
filename = tf.train.string_input_producer(files, shuffle=True, num_epochs=3) # 打乱顺序,迭代3次
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename)
features = tf.parse_single_example( # 解析serialized_example
serialized_example,
features={
'i': tf.FixedLenFeature([], tf.int64),
'j': tf.FixedLenFeature([], tf.int64),
}
)
with tf.Session() as sess:
tf.local_variables_initializer().run()
print(sess.run(files))
coord = tf.train.Coordinator() # 定义tf.Coordinator类以协同线程
threads = tf.train.start_queue_runners(sess=sess, coord=coord) # 启动线程
for i in range(12):
print(sess.run([features['i'], features['j']]))
coord.request_stop()
coord.join(threads)
原文:https://www.cnblogs.com/liuboblog/p/11650684.html
内容总结
以上是互联网集市为您收集整理的Tensorflow细节-P190-输入文件队列全部内容,希望文章能够帮你解决Tensorflow细节-P190-输入文件队列所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。