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中数据批次划分示例教程的更多相关文章

  1. TensorFlow中数据读取之tfrecords

    关于Tensorflow读取数据,官网给出了三种方法: 供给数据(Feeding): 在TensorFlow程序运行的每一步, 让Python代码来供给数据. 从文件读取数据: 在TensorFlow ...

  2. 大数据下基于Tensorflow框架的深度学习示例教程

    近几年,信息时代的快速发展产生了海量数据,诞生了无数前沿的大数据技术与应用.在当今大数据时代的产业界,商业决策日益基于数据的分析作出.当数据膨胀到一定规模时,基于机器学习对海量复杂数据的分析更能产生较 ...

  3. TensorFlow中数据读取—如何载入样本

    考虑到要是自己去做一个项目,那么第一步是如何把数据导入到代码中,何种形式呢?是否需要做预处理?官网中给的实例mnist,数据导入都是写好的模块,那么自己的数据呢? 一.从文件中读取数据(CSV文件.二 ...

  4. .NET 5/.NET Core使用EF Core 5连接MySQL数据库写入/读取数据示例教程

    本文首发于<.NET 5/.NET Core使用EF Core 5(Entity Framework Core)连接MySQL数据库写入/读取数据示例教程> 前言 在.NET Core/. ...

  5. [开发技巧]·TensorFlow中numpy与tensor数据相互转化

    [开发技巧]·TensorFlow中numpy与tensor数据相互转化 个人主页–> https://xiaosongshine.github.io/ - 问题描述 在我们使用TensorFl ...

  6. python操作txt文件中数据教程[4]-python去掉txt文件行尾换行

    python操作txt文件中数据教程[4]-python去掉txt文件行尾换行 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文章 python操作txt文件中数据教程[1]-使用pyt ...

  7. python操作txt文件中数据教程[3]-python读取文件夹中所有txt文件并将数据转为csv文件

    python操作txt文件中数据教程[3]-python读取文件夹中所有txt文件并将数据转为csv文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 python操作txt文件中 ...

  8. python操作txt文件中数据教程[2]-python提取txt文件

    python操作txt文件中数据教程[2]-python提取txt文件中的行列元素 觉得有用的话,欢迎一起讨论相互学习~Follow Me 原始txt文件 程序实现后结果-将txt中元素提取并保存在c ...

  9. python操作txt文件中数据教程[1]-使用python读写txt文件

    python操作txt文件中数据教程[1]-使用python读写txt文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 原始txt文件 程序实现后结果 程序实现 filename = '. ...

随机推荐

  1. Qt中信号(signal)和槽(slot)的几种关联方法

    声明,个人总结,不一定正确! 1.最常见的,使用connect语句.比如:connect(btnSend,SIGNAL(clicked()),this,SLOT(clear()); 2.在 .ui设计 ...

  2. WGDC2019第八届全球地理信息开发者大会(北京)

    WGDC2019第八届全球地理信息开发者大会 会议召开时间:2019-07-09 08:00至 2019-07-10 18:00结束 会议召开地点:北京  北京国际会议中心  朝阳区北辰东路8号 会议 ...

  3. JavaScript 常用数组函数方法专题

    1. 由字符串生成数组 split() 分割字符串,并将分割的部分作为一个元素保存在一个新建的数组中. var str1 = "this is an emample to using the ...

  4. js 执行机制

    浏览器中每个一个窗口都是一个单独的进程.这就需要分析浏览器与Javascript解释引擎之间的关系.先给出结论,浏览器本身是多线程的,Javascript解释引擎是单线程的. 先说说浏览器有哪些线程, ...

  5. JAVA 重写equals和重写hashCode

    面试官可能会问你:“你重写过 hashcode 和 equals 么,为什么重写equals时必须重写hashCode方法?” 首先你需要了解: hashCode()的作用是获取哈希码(散列码) 它实 ...

  6. 什么是CONTAINERD?

    之前我们已经围绕containerd的不同功能,设计方式,以及解决的一些问题进行了几次讨论. Containerd由Docker,Kubernetes CRI和其他几个项目使用,不过这个帖子是写给可能 ...

  7. 在 CentOS7 上安装 Zookeeper服务

    1.创建 /usr/local/services/zookeeper 文件夹: mkdir -p /usr/local/services/zookeeper 2.进入到 /usr/local/serv ...

  8. oracle 死锁

    oracle 死锁 --查用户名,查客户端机器 SELECT distinct s.username,s.MACHINE, s.sid||','||s.serial# FROM gv$session ...

  9. js 图片无缝滚动

    html部分 <div id="roll"> <a href="javascript:void(0)" class="prev&qu ...

  10. MySQL5.7 Dockerfile

    #Dockerfile for mysql5.7 FROM centos COPY ["src","/src"] RUN groupadd -g 1003 my ...