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 = '. ...
随机推荐
- Jmeter学习之-从数据库取出数据并且校验数据是否准确
https://www.cnblogs.com/wuyonghuan/p/7479582.html 应用场景:调用某个接口像数据库中插入数据,需要在接口调用完成后查看数据更新或插入的数据是否正确的时候 ...
- [py]使用字典get方法做数据统计
s = "aabbccc" d = {} for i in s: if i in d: d[i] += 1 else: d[i] = 0 for i in s: d[i] = d. ...
- Python开发环境Linux配置
1. 在Windows下安装Linux的连接工具(XShell),选免费的 2.虚拟机安装,注意BIOS对虚拟机使用的设置(enable) 3.虚拟机安装好修改yum源(用阿里云的):https:// ...
- 虚拟机VM三种网络连接方式说明
- 怎样下载youtube的字幕
第一步:安装chrome浏览器 第二步:安装Tampermonkey扩展. google.com的页面 第三步:使用下面两个链接安装(点进去后点击安装此脚本即可)脚本1,脚本2
- [openjudge-搜索]湖的深度
题目描述 描述 一个湖用 R x C (1 ≤ R ≤ 50; 1 ≤ C ≤ 50) 的网格表示.格点上的非负整数 D_rc (0 ≤ D_rc ≤ 1,000,000)表示该位置的深度.整数0表示 ...
- Flask最强攻略 - 跟DragonFire学Flask - 第十五篇 Flask-Script
其实本章就是为下一章做的铺垫啦,但是也要认真学习哦 Flask-Script 从字面意思上来看就是 Flask 的脚本 是的,熟悉Django的同学是否还记得Django的启动命令呢? python ...
- 自制电脑usb红外遥控键盘
2010-08-08 19:20:00 看个ppt,还要一直按键盘或鼠标,能不能拿个遥控器控制一下. 动动脑,自己做一个吧. 电路分2部分,遥控器为发射部分,单片机为解码部分并且包含usb键盘功能. ...
- CSS 边框样式
CSS 边框样式 直线边框样式 <html> <body> <!-- border: 1px 边框像素为1.solid red 边框样式以及边框颜色 --> < ...
- docker之Dockerfile实践
上一篇介绍了Dockerfile中使用的指令,现在开始进行指令实践 先查看下本地的镜像,选一个作为base image: [root@docker ~]# docker images REPOSITO ...