如果有图会很好理解,最近太忙,以后再加吧
  1. #首先有一个需要读取的文件名列表
    #然后将文件名列表通过函数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


提到queue就不得不提两个帮助多线程异步的类:tf.train.Coordinator和tf.train.QueueRunner;
  • tf.train.Coordinator:控制多线程,使其同时结束。
  • tf.train.QueueRunner:包含一些enqueue op,为其create一些线程,每一个op都在一个线程上运行。

coordinator

Coordinator方法:should_stop,request_stop,join
    1.  # 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

  1.  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的更多相关文章

  1. 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_ ...

  2. reading/writing files in Python

    file types: plaintext files, such as .txt .py Binary files, such as .docx, .pdf, iamges, spreadsheet ...

  3. 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 ...

  4. PHP | Uploading and reading of files and database 【PHP | 文件的上传和读取与数据库】

    这是我自己的一个作业,用的是很基础的代码. 有错误的地方欢迎批评和指正! 这里最容易出错的地方在读取数据后向数据库表中插入数据是的数据格式! 文件上传的页面 uploading.php <htm ...

  5. 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 ...

  6. 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 ...

  7. 解决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 ...

  8. (转)使用 SCons 轻松建造程序

    在软件项目开发过程中,make 工具通常被用来建造程序.make 工具通过一个被称为 Makefile 的配置文件可以自动的检测文件之间的依赖关系,这对于建造复杂的项目非常有帮助,然而,编写 Make ...

  9. linux使用wkhtmltopdf报错error while loading shared libraries:

    官网提示 linux需要这些动态库.depends on: zlib, fontconfig, freetype, X11 libs (libX11, libXext, libXrender) 在li ...

随机推荐

  1. YTU 2903: A--A Repeating Characters

    2903: A--A Repeating Characters 时间限制: 1 Sec  内存限制: 128 MB 提交: 50  解决: 30 题目描述 For this problem,you w ...

  2. SWFObject 的基本使用方法

    SWFObject是一个用于在HTML中方面插入Adobe Flash媒体资源(*.swf文件)的独立.敏捷的JavaScript模块.该模块中的JavaScript脚本能够自动检测PC.Mac机器上 ...

  3. math.floor()函数的用法

    floor() 返回数字的下舍整数. 语法 以下是 floor() 方法的语法: import math math.floor( x ) 注意:floor()是不能直接访问的,需要导入 math 模块 ...

  4. dropZone 回显图片

    初始化dropzone的图片信息 var dropVar = this; var mockFile = { name: "myiamge.jpg", //需要显示给用户的图片名 s ...

  5. org.springframework.web.struts.ContextLoaderPlugIn 和 org.springframework.web.context.ContextLoaderListener

    org.springframework.web.struts.ContextLoaderPlugIn 和 org.springframework.web.context.ContextLoaderLi ...

  6. asp.net MVC5 中的捆绑和更改bootstap默认的样式

    在MVC5的视图中使用@Scritps.Render(),@Styles.Render() 分别可以加载样式和脚本.捆绑的和实际的路径都可以. 并且可以利用 编程的方式灵活引用css文件和脚本文件. ...

  7. JavaScript实现对象的深度克隆及typeof和instanceof【简洁】【分享】

    JavaScript实现对象的深度克隆 代码实现如下: <!DOCTYPE html> <html lang="en"> <head> < ...

  8. Python圈中的符号计算库-Sympy(转载)

    <本文来自公众号“大邓带你玩python”,转载> import math math.sqrt(8) 2.8284271247461903 我们看看Python中结果 math.sqrt( ...

  9. Spring Json数据

    json 数据转换 @RequestBody 用于读取Request请求的body部分数据,使用系统默认配置的HttpMessageConverter 进行解析,然后把相应的数据绑定到Controll ...

  10. linux 文件查阅 cat、more、less、tail

    文件内容查阅1.cat由第一行开始显示文件内容2.tac:从最后一行开始显示,可以看出tac是cat的倒写形式.3.nl:显示的时候,顺便输出行号;4.more:一页一页地显示文件内容5.less:与 ...