tensorflow中数据批次划分示例教程
1.简介
将数据划分成若干批次的数据,可以使用tf.train或者tf.data.Dataset中的方法。
1.1 tf.train
tf.train.slice_input_producer(tensor_list,shuffle=True,seed=None,capacity=32)
tf.train.batch(tensors,batch_size,num_threads=1,capacity=32,allow_smaller_final_batch=False)
参数说明:
shuffle:为True时进行数据清洗
allow_smaller_final_batch:为True时将小于batch_size的批次值输出
-------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------
1.2 tf.data.Dataset
tf.data.Dataset是一个类,可以使用以下方法:
from_tensor_slices(tensors)
batch(batch_size,drop_remainder=False)
shuffle(buffer_size,seed=None,reshuffle_each_iteration=None)
repeat(count=None)
make_one_shot_iterator() / get_next()
注:make_one_shot_iterator() / get_next()用于Dataset数据的迭代器
参数说明:
tensors:可以是列表、字典、元组等类型
drop_remainder:为False时表示不保留小于batch_size的批次,否则删除
buffer_size:数据清洗时使用的buffer大小
count:对应为epoch个数,为None时表示数据序列无限延续
2.示例
2.1 使用tf.train.slice_input_producer和tf.train.batch
import tensorflow as tf
import numpy as np
import math # 生成样例数据集
def generate_data():
num = 15
labels = np.asarray(range(num))
images = np.random.random([num, 5, 5, 3])
return images, labels # 打印样例信息
images, labels = generate_data()
print('images.shape={0}, labels.shape={1}'.format(images.shape, labels.shape)) # 定义周期、批次、数据总量和遍历一次所有数据所需的迭代次数
n_epochs = 3
batch_size = 6
train_nums = 15
iterations = math.ceil(train_nums/batch_size) # 使用tf.train.slice_input_producer将所有数据放入队列,使用tf.train.batch划分队列中的数据
input_queue = tf.train.slice_input_producer([images, labels], shuffle=False)
image_batch, label_batch = tf.train.batch(input_queue, batch_size=batch_size, num_threads=1, capacity=32)
print('image_batch.shape={0}, label_batch.shape={1}'.format(image_batch.shape, label_batch.shape)) with tf.Session() as sess:
tf.global_variables_initializer().run()
# 启动队列线程
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess, coord)
# 打印信息
for epoch in range(n_epochs):
for iteration in range(iterations):
cu_image_batch, cu_label_batch = sess.run([image_batch, label_batch])
print('The {0} epoch, the {1} iteration, current batch is {2}'.format(epoch+1,iteration+1,cu_label_batch))
# 接收线程
coord.request_stop()
coord.join(threads) # 打印结果如下
images.shape=(15, 5, 5, 3), labels.shape=(15,)
image_batch.shape=(6, 5, 5, 3), label_batch.shape=(6,)
The 1 epoch, the 1 iteration, current batch is [0 1 2 3 4 5]
The 1 epoch, the 2 iteration, current batch is [ 6 7 8 9 10 11]
The 1 epoch, the 3 iteration, current batch is [12 13 14 0 1 2]
The 2 epoch, the 1 iteration, current batch is [3 4 5 6 7 8]
The 2 epoch, the 2 iteration, current batch is [ 9 10 11 12 13 14]
The 2 epoch, the 3 iteration, current batch is [0 1 2 3 4 5]
The 3 epoch, the 1 iteration, current batch is [ 6 7 8 9 10 11]
The 3 epoch, the 2 iteration, current batch is [12 13 14 0 1 2]
The 3 epoch, the 3 iteration, current batch is [3 4 5 6 7 8]
如果tf.train.slice_input_producer(shuffle=True),输出为乱序,结果如下:
images.shape=(15, 5, 5, 3), labels.shape=(15,)
image_batch.shape=(6, 5, 5, 3), label_batch.shape=(6,)
The 1 epoch, the 1 iteration, current batch is [ 2 5 8 11 3 10]
The 1 epoch, the 2 iteration, current batch is [ 9 12 7 1 14 13]
The 1 epoch, the 3 iteration, current batch is [0 6 4 2 3 6]
The 2 epoch, the 1 iteration, current batch is [11 10 12 14 13 5]
The 2 epoch, the 2 iteration, current batch is [8 1 0 9 4 7]
The 2 epoch, the 3 iteration, current batch is [10 13 1 4 12 3]
The 3 epoch, the 1 iteration, current batch is [ 2 8 5 9 14 7]
The 3 epoch, the 2 iteration, current batch is [ 0 11 6 1 14 9]
The 3 epoch, the 3 iteration, current batch is [11 6 12 7 0 13]
如果tf.train.batch(allow_smaller_final_batch=True),则会返回不足批次数目的数据,结果如下:
images.shape=(15, 5, 5, 3), labels.shape=(15,)
The 1 epoch, the 1 iteration, current batch is [0 1 2 3 4 5]
The 1 epoch, the 2 iteration, current batch is [ 6 7 8 9 10 11]
The 1 epoch, the 3 iteration, current batch is [12 13 14]
The 2 epoch, the 1 iteration, current batch is [0 1 2 3 4 5]
The 2 epoch, the 2 iteration, current batch is [ 6 7 8 9 10 11]
The 2 epoch, the 3 iteration, current batch is [12 13 14]
The 3 epoch, the 1 iteration, current batch is [0 1 2 3 4 5]
The 3 epoch, the 2 iteration, current batch is [ 6 7 8 9 10 11]
The 3 epoch, the 3 iteration, current batch is [12 13 14]
2.2 使用tf.data.Dataset类
import tensorflow as tf
import numpy as np
import math # 生成样例数据集
def generate_data():
num = 15
labels = np.asarray(range(num))
images = np.random.random([num, 5, 5, 3])
return images, labels
# 打印样例信息
images, labels = generate_data()
print('images.shape={0}, labels.shape={1}'.format(images.shape, labels.shape)) # 定义周期、批次、数据总数、遍历一次所有数据需的迭代次数
n_epochs = 3
batch_size = 6
train_nums = 15
iterations = math.ceil(train_nums/batch_size) # 使用from_tensor_slices将数据放入队列,使用batch和repeat划分数据批次,且让数据序列无限延续
dataset = tf.data.Dataset.from_tensor_slices((images, labels))
dataset = dataset.batch(batch_size).repeat()
# 使用生成器make_one_shot_iterator和get_next取数据
iterator = dataset.make_one_shot_iterator()
next_iterator = iterator.get_next()
with tf.Session() as sess:
for epoch in range(n_epochs):
for iteration in range(iterations):
cu_image_batch, cu_label_batch = sess.run(next_iterator)
print('The {0} epoch, the {1} iteration, current batch is {2}'.format(epoch+1,iteration+1,cu_label_batch)) # 结果如下:
images.shape=(15, 5, 5, 3), labels.shape=(15,)
The 1 epoch, the 1 iteration, current batch is [0 1 2 3 4 5]
The 1 epoch, the 2 iteration, current batch is [ 6 7 8 9 10 11]
The 1 epoch, the 3 iteration, current batch is [12 13 14]
The 2 epoch, the 1 iteration, current batch is [0 1 2 3 4 5]
The 2 epoch, the 2 iteration, current batch is [ 6 7 8 9 10 11]
The 2 epoch, the 3 iteration, current batch is [12 13 14]
The 3 epoch, the 1 iteration, current batch is [0 1 2 3 4 5]
The 3 epoch, the 2 iteration, current batch is [ 6 7 8 9 10 11]
The 3 epoch, the 3 iteration, current batch is [12 13 14]
使用shuffle(),第23行修改为dataset = dataset.shuffle(100).batch(batch_size).repeat(),结果如下:
images.shape=(15, 5, 5, 3), labels.shape=(15,)
The 1 epoch, the 1 iteration, current batch is [ 7 4 10 8 3 11]
The 1 epoch, the 2 iteration, current batch is [ 0 2 12 13 14 5]
The 1 epoch, the 3 iteration, current batch is [6 9 1]
The 2 epoch, the 1 iteration, current batch is [ 6 14 7 9 3 8]
The 2 epoch, the 2 iteration, current batch is [13 5 12 1 11 2]
The 2 epoch, the 3 iteration, current batch is [ 0 4 10]
The 3 epoch, the 1 iteration, current batch is [10 8 13 12 3 14]
The 3 epoch, the 2 iteration, current batch is [ 6 9 2 5 1 11]
The 3 epoch, the 3 iteration, current batch is [0 4 7]
!!!
tensorflow中数据批次划分示例教程的更多相关文章
- TensorFlow中数据读取之tfrecords
		
关于Tensorflow读取数据,官网给出了三种方法: 供给数据(Feeding): 在TensorFlow程序运行的每一步, 让Python代码来供给数据. 从文件读取数据: 在TensorFlow ...
 - 大数据下基于Tensorflow框架的深度学习示例教程
		
近几年,信息时代的快速发展产生了海量数据,诞生了无数前沿的大数据技术与应用.在当今大数据时代的产业界,商业决策日益基于数据的分析作出.当数据膨胀到一定规模时,基于机器学习对海量复杂数据的分析更能产生较 ...
 - TensorFlow中数据读取—如何载入样本
		
考虑到要是自己去做一个项目,那么第一步是如何把数据导入到代码中,何种形式呢?是否需要做预处理?官网中给的实例mnist,数据导入都是写好的模块,那么自己的数据呢? 一.从文件中读取数据(CSV文件.二 ...
 - .NET 5/.NET Core使用EF Core 5连接MySQL数据库写入/读取数据示例教程
		
本文首发于<.NET 5/.NET Core使用EF Core 5(Entity Framework Core)连接MySQL数据库写入/读取数据示例教程> 前言 在.NET Core/. ...
 - [开发技巧]·TensorFlow中numpy与tensor数据相互转化
		
[开发技巧]·TensorFlow中numpy与tensor数据相互转化 个人主页–> https://xiaosongshine.github.io/ - 问题描述 在我们使用TensorFl ...
 - python操作txt文件中数据教程[4]-python去掉txt文件行尾换行
		
python操作txt文件中数据教程[4]-python去掉txt文件行尾换行 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文章 python操作txt文件中数据教程[1]-使用pyt ...
 - python操作txt文件中数据教程[3]-python读取文件夹中所有txt文件并将数据转为csv文件
		
python操作txt文件中数据教程[3]-python读取文件夹中所有txt文件并将数据转为csv文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 python操作txt文件中 ...
 - python操作txt文件中数据教程[2]-python提取txt文件
		
python操作txt文件中数据教程[2]-python提取txt文件中的行列元素 觉得有用的话,欢迎一起讨论相互学习~Follow Me 原始txt文件 程序实现后结果-将txt中元素提取并保存在c ...
 - python操作txt文件中数据教程[1]-使用python读写txt文件
		
python操作txt文件中数据教程[1]-使用python读写txt文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 原始txt文件 程序实现后结果 程序实现 filename = '. ...
 
随机推荐
- 报错解决——xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
			
一般在遇到这个问题的时候都是想用git或者svn,结果发现用不了并报错xcrun: error: invalid active developer path (/Library/Developer/C ...
 - kafka7 探索生产者同步or异步发送消息
			
1.生产者:在发送完消息后,收到回执确认. 主要是在SimpleProducer.java中修改了发送消息的2行代码,用到了回调函数,修改如下: //发送消息 ProducerRecord<St ...
 - Ethzasl MSF源码阅读(1):程序入口和主题订阅
			
关于IMU融合知乎上的一篇问答:有哪些开源项目是关于单目+imu做slam的? Ethz的Stephen Weiss的工作,是一个IMU松耦合的方法. 1.程序入口:ethzasl_msf\msf_u ...
 - [js]变量提升-关于条件
			
条件函数变量提示于全局中函数变量提升不一样. 条件中: 函数变量提升, 只是声明(现新版本浏览器中) if(g()){ function g() { return true } console.log ...
 - 467A
			
#include <stdio.h> int main() { int n; int p, q; int rooms=0; scanf("%d", &n); i ...
 - python frist lesson
			
1.print("") 代表打印字符段 2.name2 = name ,然后改变name的赋值,name2的值还是以前name的值,说明name2指向的是内存中name的赋值. 3 ...
 - SPP空间金字塔池化技术的直观理解
			
空间金字塔池化技术, 厉害之处,在于使得我们构建的网络,可以输入任意大小的图片,不需要经过裁剪缩放等操作. 是后续许多金字塔技术(psp,aspp等)的起源,主要的目的都是为了获取场景语境信息,获取上 ...
 - IP2——IP地址和子网划分学习笔记之《子网掩码详解》
			
2018-05-04 16:21:21 在学习掌握了前面的<进制计数><IP地址详解>这两部分知识后,要学习子网划分,首先就要必须知道子网掩码,只有掌握了子网掩码这部分内容 ...
 - ASP.NET页面之间传值的方式之QueryString(个人整理)
			
QueryString Querystring也叫查询字符串,这种页面间传递数据是利用网页地址URL.如果要从A页面跳转到B页面,则可以用Request.Redirect(”B.aspx?参数名=参数 ...
 - timer控件、三级联动、帐号激活权限设置
			
一.Timer控件 Timer实际就是一个线程控件. 属性:Enabled 是否被启用 Interval 多长时间执行一次控件中的代码 事件: Tick 事件中放要执行的代码. ...