zoukankan      html  css  js  c++  java
  • tesnorflow实现N个epoch训练数据读取的办法

    https://blog.csdn.net/lujiandong1/article/details/53991373

    方式一:不显示设置读取N个epoch的数据,而是使用循环,每次从训练的文件中随机读取一个batch_size的数据,直至最后读取的数据量达到N个epoch。说明,这个方式来实现epoch的输入是不合理。不是说每个样本都会被读取到的。

    对于这个的解释,从数学上解释,比如说有放回的抽样,每次抽取一个样本,抽取N次,总样本数为N个。那么,这样抽取过一轮之后,该样本也是会有1/e的概率没有被抽取到。所以,如果使用这种方式去训练的话,理论上是没有用到全部的数据集去训练的,很可能会造成过拟合的现象。

    我做了个小实验验证:

    1.  
      import tensorflow as tf
    2.  
      import numpy as np
    3.  
      import datetime,sys
    4.  
      from tensorflow.contrib import learn
    5.  
      from model import CCPM
    6.  
       
    7.  
      training_epochs = 5
    8.  
      train_num = 4
    9.  
      # 运行Graph
    10.  
      with tf.Session() as sess:
    11.  
       
    12.  
      #定义模型
    13.  
      BATCH_SIZE = 2
    14.  
      # 构建训练数据输入的队列
    15.  
      # 生成一个先入先出队列和一个QueueRunner,生成文件名队列
    16.  
      filenames = ['a.csv']
    17.  
      filename_queue = tf.train.string_input_producer(filenames, shuffle=True)
    18.  
      # 定义Reader
    19.  
      reader = tf.TextLineReader()
    20.  
      key, value = reader.read(filename_queue)
    21.  
      # 定义Decoder
    22.  
      # 编码后的数据字段有24,其中22维是特征字段,2维是lable字段,label是二分类经过one-hot编码后的字段
    23.  
      #更改了特征,使用不同的解析参数
    24.  
      record_defaults = [[1]]*5
    25.  
      col1,col2,col3,col4,col5 = tf.decode_csv(value,record_defaults=record_defaults)
    26.  
      features = tf.pack([col1,col2,col3,col4])
    27.  
      label = tf.pack([col5])
    28.  
       
    29.  
      example_batch, label_batch = tf.train.shuffle_batch([features,label], batch_size=BATCH_SIZE, capacity=20000, min_after_dequeue=4000, num_threads=2)
    30.  
       
    31.  
      sess.run(tf.initialize_all_variables())
    32.  
      coord = tf.train.Coordinator()#创建一个协调器,管理线程
    33.  
      threads = tf.train.start_queue_runners(coord=coord)#启动QueueRunner, 此时文件名队列已经进队。
    34.  
      #开始一个epoch的训练
    35.  
      for epoch in range(training_epochs):
    36.  
      total_batch = int(train_num/BATCH_SIZE)
    37.  
      #开始一个epoch的训练
    38.  
      for i in range(total_batch):
    39.  
      X,Y = sess.run([example_batch, label_batch])
    40.  
      print X,':',Y
    41.  
      coord.request_stop()
    42.  
      coord.join(threads)


    toy data a.csv:

    说明:输出如下,可以看出并不是每个样本都被遍历5次,其实这样的话,对于DL的训练会产生很大的影响,并不是每个样本都被使用同样的次数。

    方式二:显示设置epoch的数目

    1.  
      #-*- coding:utf-8 -*-
    2.  
      import tensorflow as tf
    3.  
      import numpy as np
    4.  
      import datetime,sys
    5.  
      from tensorflow.contrib import learn
    6.  
      from model import CCPM
    7.  
       
    8.  
      training_epochs = 5
    9.  
      train_num = 4
    10.  
      # 运行Graph
    11.  
      with tf.Session() as sess:
    12.  
       
    13.  
      #定义模型
    14.  
      BATCH_SIZE = 2
    15.  
      # 构建训练数据输入的队列
    16.  
      # 生成一个先入先出队列和一个QueueRunner,生成文件名队列
    17.  
      filenames = ['a.csv']
    18.  
      filename_queue = tf.train.string_input_producer(filenames, shuffle=True,num_epochs=training_epochs)
    19.  
      # 定义Reader
    20.  
      reader = tf.TextLineReader()
    21.  
      key, value = reader.read(filename_queue)
    22.  
      # 定义Decoder
    23.  
      # 编码后的数据字段有24,其中22维是特征字段,2维是lable字段,label是二分类经过one-hot编码后的字段
    24.  
      #更改了特征,使用不同的解析参数
    25.  
      record_defaults = [[1]]*5
    26.  
      col1,col2,col3,col4,col5 = tf.decode_csv(value,record_defaults=record_defaults)
    27.  
      features = tf.pack([col1,col2,col3,col4])
    28.  
      label = tf.pack([col5])
    29.  
       
    30.  
      example_batch, label_batch = tf.train.shuffle_batch([features,label], batch_size=BATCH_SIZE, capacity=20000, min_after_dequeue=4000, num_threads=2)
    31.  
      sess.run(tf.initialize_local_variables())
    32.  
      sess.run(tf.initialize_all_variables())
    33.  
      coord = tf.train.Coordinator()#创建一个协调器,管理线程
    34.  
      threads = tf.train.start_queue_runners(coord=coord)#启动QueueRunner, 此时文件名队列已经进队。
    35.  
      try:
    36.  
      #开始一个epoch的训练
    37.  
      while not coord.should_stop():
    38.  
      total_batch = int(train_num/BATCH_SIZE)
    39.  
      #开始一个epoch的训练
    40.  
      for i in range(total_batch):
    41.  
      X,Y = sess.run([example_batch, label_batch])
    42.  
      print X,':',Y
    43.  
      except tf.errors.OutOfRangeError:
    44.  
      print('Done training')
    45.  
      finally:
    46.  
      coord.request_stop()
    47.  
      coord.join(threads)

    说明:输出如下,可以看出每个样本都被访问5次,这才是合理的设置epoch数据的方式。


    http://stats.stackexchange.com/questions/242004/why-do-neural-network-researchers-care-about-epochs

    说明:这个博客也在探讨,为什么深度网络的训练中,要使用epoch,即要把训练样本全部过一遍.而不是随机有放回的从里面抽样batch_size个样本.在博客中,别人的实验结果是如果采用有放回抽样的这种方式来进行SGD的训练.其实网络见不到全部的数据集,推导过程如上所示.所以,网络的收敛速度比较慢.

  • 相关阅读:
    supervisor(一)基础篇
    linux添加开机自启动脚本示例详解
    suse 不能远程登录
    LintCode,hihoCoder,LeetCode有什么区别?
    windows 下安装nodejs 要怎么设置环境变量
    Java 集合:HashSet 与 ArrayList
    Java ArrayList、Vector和LinkedList等的差别与用法(转)
    一行代码实现java list去重
    25 highest paying companies: Which tech co outranks Google, Facebook and Microsoft?
    Chart: Who pays the most in Seattle for software engineers
  • 原文地址:https://www.cnblogs.com/DjangoBlog/p/9467983.html
Copyright © 2011-2022 走看看