分类目录:《深入浅出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_remainderFalse,则最后一个元素的batch_sizeN % 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被指定时,如果指定了此布尔值(TrueFalse),则它控制转换生成元素的顺序。如果设置为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相关推荐

  1. 深入浅出TensorFlow2函数——tf.constant

    分类目录:<深入浅出TensorFlow2函数>总目录 相关文章: · 深入浅出TensorFlow2函数--tf.constant · 深入浅出TensorFlow2函数--tf.Ten ...

  2. 深入浅出TensorFlow2函数——tf.reshape

    分类目录:<深入浅出TensorFlow2函数>总目录 语法 tf.reshape(tensor, shape, name=None ) 参数 返回值 返回一个新的形状为shape的tf. ...

  3. 深入浅出TensorFlow2函数——tf.keras.layers.Embedding

    分类目录:<深入浅出TensorFlow2函数>总目录 语法 tf.keras.layers.Embedding(input_dim, output_dim, embeddings_ini ...

  4. 深入浅出TensorFlow2函数——tf.keras.layers.Dense

    分类目录:<深入浅出TensorFlow2函数>总目录 tf.keras.layers.Dense实现操作:output = activation(dot(input, kernel) + ...

  5. tensorflow学习笔记:tf.data.Dataset,from_tensor_slices(),shuffle(),batch()的用法

    tf.data.Dataset.from_tensor_slices: 它的作用是切分传入Tensor的第一个维度,生成相应的dataset. 例1: dataset = tf.data.Datase ...

  6. 记录 之 tensorflow函数:tf.data.Dataset.from_tensor_slices

    tf.data.Dataset.from_tensor_slices(),是常见的数据处理函数,它的作用是将给定的元组(turple).列表(list).张量(tensor)等特征进行特征切片.切片的 ...

  7. tensorflow2数据读取P3: tf.data.Dataset.from_generator通过preprocessing.image.ImageDataGenerator构造Dataset

    tf.data.Dataset.from_generator通过preprocessing.image.ImageDataGenerator构造Dataset 虽然自己定义生成器就可以构建datase ...

  8. TensorFlow数据读取机制:文件队列 tf.train.slice_input_producer和 tf.data.Dataset机制

    TensorFlow数据读取机制:文件队列 tf.train.slice_input_producer和tf.data.Dataset机制 之前写了一篇博客,关于<Tensorflow生成自己的 ...

  9. tensorflow基础:tf.data.Dataset.from_tensor_slices() 与 tf.data.Dataset.from_generator()的异同

    tf.data.Dataset.from_tensor_slices(tensor): -->将tensor沿其第一个维度切片,返回一个含有N个样本的数据集(假设tensor的第一个维度为N). ...

最新文章

  1. JAVA实现QQ聊天气泡
  2. zybo的linux开发教程,Zybo全栈开发入门教程——连载三:创建Linux设备驱动和应用程序...
  3. CGPA的完整形式是什么?
  4. ajax 折叠,ASP.NET AJAX可折叠面板Accordion应用实例
  5. 【Java线程】复盘线程池使用及思考
  6. 剑指offer——面试题41-2:和为S的两个数字
  7. VC++控件加载BMP图片(静态和动态方式)
  8. 数学建模:人口增长模型
  9. Exp4 恶意代码分析 20154301仉鑫烨
  10. betterscroll的使用
  11. 跨越opengl和d3d的鸿沟(四):完结篇,平台和未来
  12. python调用so库
  13. 【MySQL数据库开发之一】Mac下配置安装数据库-MySQL
  14. antd的Table列选择、列拓展
  15. buddypress主题_WordPress Gone Social-BuddyPress
  16. Win10系统下语音识别聆听功能使用方法
  17. 浅谈MySQL数据库备份的几种方法
  18. OpenGL原理介绍
  19. 富基融通:“穷”行业生存术
  20. Rexsee API介绍:Android屏幕锁定,Keyguard函数与扩展源码

热门文章

  1. Spring Batch之读数据—读JSON文件(二十八)
  2. Linux的压缩包管理
  3. Slog71_选取、上传和显示本地图片GET !(微信小程序之云开发-全栈时代3)
  4. java获取字符串hash值,Java 获取字符串Hash值
  5. druid 大量sleep连接
  6. Android 12壁纸,Android12壁纸下载-Android12壁纸高清壁纸大全 v1.0-友情手机站
  7. 大型机学习之具体技术-操作系统
  8. The Thundering Herd accept system call
  9. 业务状态实时监控预警,「告警配置」来帮您
  10. 网站推广88个金牌方法(2008奥运会金牌榜版)