58同城网站建设,网站建设制作微商授权书,自己制作头像的网站 设计 动漫,咨询行业网站开发方式一#xff1a;不显示设置读取N个epoch的数据#xff0c;而是使用循环#xff0c;每次从训练的文件中随机读取一个batch_size的数据#xff0c;直至最后读取的数据量达到N个epoch。说明#xff0c;这个方式来实现epoch的输入是不合理。不是说每个样本都会被读取到的。对…方式一不显示设置读取N个epoch的数据而是使用循环每次从训练的文件中随机读取一个batch_size的数据直至最后读取的数据量达到N个epoch。说明这个方式来实现epoch的输入是不合理。不是说每个样本都会被读取到的。对于这个的解释,从数学上解释比如说有放回的抽样每次抽取一个样本抽取N次总样本数为N个。那么这样抽取过一轮之后该样本也是会有1/e的概率没有被抽取到。所以如果使用这种方式去训练的话理论上是没有用到全部的数据集去训练的很可能会造成过拟合的现象。我做了个小实验验证import tensorflow as tfimport numpy as npimport datetime,sysfrom tensorflow.contrib import learnfrom model import CCPMtraining_epochs 5train_num 4# 运行Graphwith tf.Session() as sess:#定义模型BATCH_SIZE 2# 构建训练数据输入的队列# 生成一个先入先出队列和一个QueueRunner,生成文件名队列filenames [a.csv]filename_queue tf.train.string_input_producer(filenames, shuffleTrue)# 定义Readerreader tf.TextLineReader()key, value reader.read(filename_queue)# 定义Decoder# 编码后的数据字段有24,其中22维是特征字段,2维是lable字段,label是二分类经过one-hot编码后的字段#更改了特征,使用不同的解析参数record_defaults [[1]]*5col1,col2,col3,col4,col5 tf.decode_csv(value,record_defaultsrecord_defaults)features tf.pack([col1,col2,col3,col4])label tf.pack([col5])example_batch, label_batch tf.train.shuffle_batch([features,label], batch_sizeBATCH_SIZE, capacity20000, min_after_dequeue4000, num_threads2)sess.run(tf.initialize_all_variables())coord tf.train.Coordinator()#创建一个协调器管理线程threads tf.train.start_queue_runners(coordcoord)#启动QueueRunner, 此时文件名队列已经进队。#开始一个epoch的训练for epoch in range(training_epochs):total_batch int(train_num/BATCH_SIZE)#开始一个epoch的训练for i in range(total_batch):X,Y sess.run([example_batch, label_batch])print X,:,Ycoord.request_stop()coord.join(threads)toy data a.csv:说明输出如下可以看出并不是每个样本都被遍历5次其实这样的话对于DL的训练会产生很大的影响并不是每个样本都被使用同样的次数。方式二显示设置epoch的数目#-*- coding:utf-8 -*-import tensorflow as tfimport numpy as npimport datetime,sysfrom tensorflow.contrib import learnfrom model import CCPMtraining_epochs 5train_num 4# 运行Graphwith tf.Session() as sess:#定义模型BATCH_SIZE 2# 构建训练数据输入的队列# 生成一个先入先出队列和一个QueueRunner,生成文件名队列filenames [a.csv]filename_queue tf.train.string_input_producer(filenames, shuffleTrue,num_epochstraining_epochs)# 定义Readerreader tf.TextLineReader()key, value reader.read(filename_queue)# 定义Decoder# 编码后的数据字段有24,其中22维是特征字段,2维是lable字段,label是二分类经过one-hot编码后的字段#更改了特征,使用不同的解析参数record_defaults [[1]]*5col1,col2,col3,col4,col5 tf.decode_csv(value,record_defaultsrecord_defaults)features tf.pack([col1,col2,col3,col4])label tf.pack([col5])example_batch, label_batch tf.train.shuffle_batch([features,label], batch_sizeBATCH_SIZE, capacity20000, min_after_dequeue4000, num_threads2)sess.run(tf.initialize_local_variables())sess.run(tf.initialize_all_variables())coord tf.train.Coordinator()#创建一个协调器管理线程threads tf.train.start_queue_runners(coordcoord)#启动QueueRunner, 此时文件名队列已经进队。try:#开始一个epoch的训练while not coord.should_stop():total_batch int(train_num/BATCH_SIZE)#开始一个epoch的训练for i in range(total_batch):X,Y sess.run([example_batch, label_batch])print X,:,Yexcept tf.errors.OutOfRangeError:print(Done training)finally:coord.request_stop()coord.join(threads)说明输出如下可以看出每个样本都被访问5次这才是合理的设置epoch数据的方式。