深入浅出TensorFlow2函数——tf.data.Dataset.batch
分类目录:《深入浅出TensorFlow2函数》总目录
语法
batch(batch_size, drop_remainder=False, num_parallel_calls=None, deterministic=None,name=None)
该函数可以将此数据集的连续元素合并到batch中。
dataset = tf.data.Dataset.range(8)
dataset = dataset.batch(3)
list(dataset.as_numpy_iterator())
# [array([0, 1, 2], dtype=int64), array([3, 4, 5], dtype=int64)]
函数返回值将有一个额外的外部维度,即batch_size
。如果batch_size
未将输入元素的数量 N N N平均分割,且drop_remainder
为False
,则最后一个元素的batch_size
为N % batch_size
。如果需要依赖于具有相同尺寸的batch,则应将drop_rements
参数设置为True
,以防止生成较小的批。如果程序要求数据具有静态已知形状,则应使用drop_rements=True
。如果没有drop_rements=True
,则输出数据集的形状将具有未知的前导维度,因为最终批次可能更小。
参数
参数 | 意义 |
---|---|
batch_size |
[tf.int64 /tf.Tensor ]表示要在单个批次中组合的此数据集的连续元素数。
|
drop_remainder |
[可选, tf.bool /tf.Tensor ]表示如果最后一批元素少于批次大小,是否应删除最后一批元素,默认为False 。
|
num_parallel_calls |
[可选,tf.int64 /tf.Tensor ]表示异步并行计算的批数。如果未指定,将按顺序计算批次。如果值为tf.data.AUTOTUNE 被使用,则根据可用资源动态设置并行调用的数量。
|
deterministic |
[可选]当num_parallel_calls 被指定时,如果指定了此布尔值(True 或False ),则它控制转换生成元素的顺序。如果设置为False ,则允许转换产生无序的元素,即损失性能的情况下换取确定性。如果未指定,则tf.data.Options.deterministic (默认为True )来控制行为。
|
name |
[可选]tf.data 操作的名称
|
返回值
返回值 | 意义 |
---|---|
Dataset |
一个tf.data.Dataset 的数据集。
|
函数实现
def batch(self,batch_size,drop_remainder=False,num_parallel_calls=None,deterministic=None,name=None):"""Combines consecutive elements of this dataset into batches.>>> dataset = tf.data.Dataset.range(8)>>> dataset = dataset.batch(3)>>> list(dataset.as_numpy_iterator())[array([0, 1, 2]), array([3, 4, 5]), array([6, 7])]>>> dataset = tf.data.Dataset.range(8)>>> dataset = dataset.batch(3, drop_remainder=True)>>> list(dataset.as_numpy_iterator())[array([0, 1, 2]), array([3, 4, 5])]The components of the resulting element will have an additional outerdimension, which will be `batch_size` (or `N % batch_size` for the lastelement if `batch_size` does not divide the number of input elements `N`evenly and `drop_remainder` is `False`). If your program depends on thebatches having the same outer dimension, you should set the `drop_remainder`argument to `True` to prevent the smaller batch from being produced.Note: If your program requires data to have a statically known shape (e.g.,when using XLA), you should use `drop_remainder=True`. Without`drop_remainder=True` the shape of the output dataset will have an unknownleading dimension due to the possibility of a smaller final batch.Args:batch_size: A `tf.int64` scalar `tf.Tensor`, representing the number ofconsecutive elements of this dataset to combine in a single batch.drop_remainder: (Optional.) A `tf.bool` scalar `tf.Tensor`, representingwhether the last batch should be dropped in the case it has fewer than`batch_size` elements; the default behavior is not to drop the smallerbatch.num_parallel_calls: (Optional.) A `tf.int64` scalar `tf.Tensor`,representing the number of batches to compute asynchronously inparallel.If not specified, batches will be computed sequentially. If the value`tf.data.AUTOTUNE` is used, then the number of parallelcalls is set dynamically based on available resources.deterministic: (Optional.) When `num_parallel_calls` is specified, if thisboolean is specified (`True` or `False`), it controls the order in whichthe transformation produces elements. If set to `False`, thetransformation is allowed to yield elements out of order to tradedeterminism for performance. If not specified, the`tf.data.Options.deterministic` option (`True` by default) controls thebehavior.name: (Optional.) A name for the tf.data operation.Returns:Dataset: A `Dataset`."""if num_parallel_calls is None or DEBUG_MODE:if deterministic is not None and not DEBUG_MODE:warnings.warn("The `deterministic` argument has no effect unless the ""`num_parallel_calls` argument is specified.")return BatchDataset(self, batch_size, drop_remainder, name=name)else:return ParallelBatchDataset(self,batch_size,drop_remainder,num_parallel_calls,deterministic,name=name)
深入浅出TensorFlow2函数——tf.data.Dataset.batch相关推荐
- 深入浅出TensorFlow2函数——tf.constant
分类目录:<深入浅出TensorFlow2函数>总目录 相关文章: · 深入浅出TensorFlow2函数--tf.constant · 深入浅出TensorFlow2函数--tf.Ten ...
- 深入浅出TensorFlow2函数——tf.reshape
分类目录:<深入浅出TensorFlow2函数>总目录 语法 tf.reshape(tensor, shape, name=None ) 参数 返回值 返回一个新的形状为shape的tf. ...
- 深入浅出TensorFlow2函数——tf.keras.layers.Embedding
分类目录:<深入浅出TensorFlow2函数>总目录 语法 tf.keras.layers.Embedding(input_dim, output_dim, embeddings_ini ...
- 深入浅出TensorFlow2函数——tf.keras.layers.Dense
分类目录:<深入浅出TensorFlow2函数>总目录 tf.keras.layers.Dense实现操作:output = activation(dot(input, kernel) + ...
- tensorflow学习笔记:tf.data.Dataset,from_tensor_slices(),shuffle(),batch()的用法
tf.data.Dataset.from_tensor_slices: 它的作用是切分传入Tensor的第一个维度,生成相应的dataset. 例1: dataset = tf.data.Datase ...
- 记录 之 tensorflow函数:tf.data.Dataset.from_tensor_slices
tf.data.Dataset.from_tensor_slices(),是常见的数据处理函数,它的作用是将给定的元组(turple).列表(list).张量(tensor)等特征进行特征切片.切片的 ...
- tensorflow2数据读取P3: tf.data.Dataset.from_generator通过preprocessing.image.ImageDataGenerator构造Dataset
tf.data.Dataset.from_generator通过preprocessing.image.ImageDataGenerator构造Dataset 虽然自己定义生成器就可以构建datase ...
- TensorFlow数据读取机制:文件队列 tf.train.slice_input_producer和 tf.data.Dataset机制
TensorFlow数据读取机制:文件队列 tf.train.slice_input_producer和tf.data.Dataset机制 之前写了一篇博客,关于<Tensorflow生成自己的 ...
- tensorflow基础:tf.data.Dataset.from_tensor_slices() 与 tf.data.Dataset.from_generator()的异同
tf.data.Dataset.from_tensor_slices(tensor): -->将tensor沿其第一个维度切片,返回一个含有N个样本的数据集(假设tensor的第一个维度为N). ...
最新文章
- JAVA实现QQ聊天气泡
- zybo的linux开发教程,Zybo全栈开发入门教程——连载三:创建Linux设备驱动和应用程序...
- CGPA的完整形式是什么?
- ajax 折叠,ASP.NET AJAX可折叠面板Accordion应用实例
- 【Java线程】复盘线程池使用及思考
- 剑指offer——面试题41-2:和为S的两个数字
- VC++控件加载BMP图片(静态和动态方式)
- 数学建模:人口增长模型
- Exp4 恶意代码分析 20154301仉鑫烨
- betterscroll的使用
- 跨越opengl和d3d的鸿沟(四):完结篇,平台和未来
- python调用so库
- 【MySQL数据库开发之一】Mac下配置安装数据库-MySQL
- antd的Table列选择、列拓展
- buddypress主题_WordPress Gone Social-BuddyPress
- Win10系统下语音识别聆听功能使用方法
- 浅谈MySQL数据库备份的几种方法
- OpenGL原理介绍
- 富基融通:“穷”行业生存术
- Rexsee API介绍:Android屏幕锁定,Keyguard函数与扩展源码
热门文章
- Spring Batch之读数据—读JSON文件(二十八)
- Linux的压缩包管理
- Slog71_选取、上传和显示本地图片GET !(微信小程序之云开发-全栈时代3)
- java获取字符串hash值,Java 获取字符串Hash值
- druid 大量sleep连接
- Android 12壁纸,Android12壁纸下载-Android12壁纸高清壁纸大全 v1.0-友情手机站
- 大型机学习之具体技术-操作系统
- The Thundering Herd accept system call
- 业务状态实时监控预警,「告警配置」来帮您
- 网站推广88个金牌方法(2008奥运会金牌榜版)