reading from files
#首先有一个需要读取的文件名列表
#然后将文件名列表通过函数string_input_producer放进文件名队列。
#有时候因为数据量太大,需要把他们放进不同的tfrecord文件中
filename_queue = tf.train.string_input_producer(["file0.csv","file1.csv"])
#对不同格式的文件有不同的reader
reader = tf.TextLineReader()
#通过reader的read函数extract a record from a file whose name is in the queue,
#如果该文件中所有记录都被抽取完,dequeue这个filename,参考readerbase
#read()返回下一个record
key, value = reader.read(filename_queue)
# decoded record,decode方式和文件内部record格式相关,然后拼接成需要的格式
record_defaults =[[1],[1],[1],[1],[1]]
col1, col2, col3, col4, col5 = tf.decode_csv(
value, record_defaults=record_defaults)
features = tf.stack([col1, col2, col3, col4])
with tf.Session()as sess:
# Start populating the filename queue.
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
for i in range(1200):
# Retrieve a single instance:
example, label = sess.run([features, col5])
coord.request_stop()
coord.join(threads)参考:https://www.tensorflow.org/programmers_guide/reading_data
- tf.train.Coordinator:控制多线程,使其同时结束。
- tf.train.QueueRunner:包含一些enqueue op,为其create一些线程,每一个op都在一个线程上运行。
coordinator
Coordinator方法:should_stop,request_stop,join
# Thread body: loop until the coordinator indicates a stop was requested.
# If some condition becomes true, ask the coordinator to stop.
defMyLoop(coord):
whilenot coord.should_stop():#should_stop返回true or false,表示线程是否该结束
...do something...
if...some condition...:
coord.request_stop()#当某些条件发生时,一个进程request_stop,其他进程因为should_stop返回true而终止
# Main thread: create a coordinator.
coord = tf.train.Coordinator()
# Create 10 threads that run 'MyLoop()'
threads =[threading.Thread(target=MyLoop, args=(coord,))for i in xrange(10)]
# Start the threads and wait for all of them to stop.
for t in threads:
t.start()
coord.join(threads)
QueueRunner
example =...ops to create one example...
# Create a queue, and an op that enqueues examples one at a time in the queue.
#区别于filename queue,这是example queue。可以是接着上面读数据解析然后放进这个queue
queue = tf.RandomShuffleQueue(...)
enqueue_op = queue.enqueue(example)#定义入队操作
# Create a training graph that starts by dequeuing a batch of examples.
inputs = queue.dequeue_many(batch_size)
train_op =...use 'inputs' to build the training part of the graph...
# Create a queue runner that will run 4 threads in parallel to enqueue
# examples.
#QueueRunner的构造函数,queuerunner是为一个queue的入队操作多线程化服务的,
#第二个参数是入队操作列表
qr = tf.train.QueueRunner(queue,[enqueue_op]*4)
# Launch the graph.
sess = tf.Session()
# Create a coordinator, launch the queue runner threads.
coord = tf.train.Coordinator()
#queuerunner为queue创造多线程,并且把这些线程的结束交由coordinator管理
enqueue_threads = qr.create_threads(sess, coord=coord, start=True)
# Run the training loop, controlling termination with the coordinator.
for step in xrange(1000000):
if coord.should_stop():
break
sess.run(train_op)
# When done, ask the threads to stop.
coord.request_stop()
# And wait for them to actually do it.
coord.join(enqueue_threads)
reading from files的更多相关文章
- Reading Csv Files with Text_io in Oracle D2k Forms
Below is the example to read and import comma delimited csv file in oracle forms with D2k_Delimited_ ...
- reading/writing files in Python
file types: plaintext files, such as .txt .py Binary files, such as .docx, .pdf, iamges, spreadsheet ...
- Reading Text-based Files In ASP.NET
Friday, July 17, 2015 1:43 PM Every time I need to work with the contents of text-based files in an ...
- PHP | Uploading and reading of files and database 【PHP | 文件的上传和读取与数据库】
这是我自己的一个作业,用的是很基础的代码. 有错误的地方欢迎批评和指正! 这里最容易出错的地方在读取数据后向数据库表中插入数据是的数据格式! 文件上传的页面 uploading.php <htm ...
- Using Text_IO To Read Files in Oracle D2k
Suppose you want to read a file from D2k client and want to store its content in Oracle database. Bu ...
- Fast data loading from files to R
Recently we were building a Shiny App in which we had to load data from a very large dataframe. It w ...
- 解决javascript - node and Error: EMFILE, too many open files
For some days I have searched for a working solution to an error Error: EMFILE, too many open files ...
- (转)使用 SCons 轻松建造程序
在软件项目开发过程中,make 工具通常被用来建造程序.make 工具通过一个被称为 Makefile 的配置文件可以自动的检测文件之间的依赖关系,这对于建造复杂的项目非常有帮助,然而,编写 Make ...
- linux使用wkhtmltopdf报错error while loading shared libraries:
官网提示 linux需要这些动态库.depends on: zlib, fontconfig, freetype, X11 libs (libX11, libXext, libXrender) 在li ...
随机推荐
- HDU1269 迷宫城堡 —— 强连通分量
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1269 迷宫城堡 Time Limit: 2000/1000 MS (Java/Others) M ...
- 给Xcode增加复制行、删除行快捷键的方法
运行: sudo chmod 666 /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Resources/IDETextKey ...
- Codeforces 633H. Fibonacci-ish II
题目大意: 一个数列 q次询问 每次询问l r 将数列中l-r的位置排序去重后的数列成为b 输出 sigma b i * F i (其中F i为斐波那契数列中的第i项) 思路: 由于要去重 考虑权值线 ...
- 【BZOJ 1233】 干草堆
[题目链接] 点击打开链接 [算法] 这题有一个性质 : 位于顶层的干草堆可以满足宽度最小且高度最高 根据这个性质,用单调队列优化DP,即可 [代码] #include<bits/stdc++. ...
- tcp/ip网络通讯安全加密方法
tcp/ip网络通讯安全是一个广受关注的话题,现在也有一些基于tcp/ip加密技术标准如SSL,TLS等.但很多时候编写一些简单的网络通讯把这标准加密应用添加进来乎一下子把程序变得复杂了,而实现自己的 ...
- 【136】Cydia相关插件及配置
插件推荐: iFile:进行文件管理! Music2iPod:同步音乐到iPod内部! LabelEnhance:标签颜色修改! Bridge:貌似功能强大,与Music2iPod类似! Activa ...
- 【黑金教程笔记之005】【建模篇】【Lab 04 消抖模块之二】—笔记
实验四和实验三的区别在于输出.实验三是检测到由高到低的电平变化时就拉高输出,检测到由低到高的电平变化时就拉低输出.而实验四检测到由高到低的电平变化时产生一个100ms的高脉冲.当检测到由低到高的电平变 ...
- bzoj 2016: [Usaco2010]Chocolate Eating【二分+贪心】
二分答案,贪心判断,洛谷上要开long long #include<iostream> #include<cstdio> using namespace std; const ...
- 洛谷 P1966 火柴排队
题目描述 涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度. 现在将每盒中的火柴各自排成一列, 同一列火柴的高度互不相同, 两列火柴之间的距离定义为:∑(ai−bi)2 其中ai 表示 ...
- JDK与JRE、JVM三者间的关系及JDK的安装部署
JDK与JRE.JVM三者间的关系及JDK的安装部署 一.JDK与JRE.JVM三者间的关系 JDK(Java Development Kit)是针对Java开发员的产品,是整个Java的核心,包括了 ...