TensorFlow2.0(6):利用data模块进行数据预处理(下)

简介: TensorFlow2.0(6):利用data模块进行数据预处理(下)

2 功能函数


(1)take()


功能:用于返回一个新的Dataset对象,新的Dataset对象包含的数据是原Dataset对象的子集。


参数:

  • count:整型,用于指定前count条数据用于创建新的Dataset对象,如果count为-1或大于原Dataset对象的size,则用原Dataset对象的全部数据创建新的对象。


dataset = tf.data.Dataset.range(10)dataset_take = dataset.take(5)


for i in dataset_take:    print(i)


tf.Tensor(0, shape=(), dtype=int64)
tf.Tensor(1, shape=(), dtype=int64)
tf.Tensor(2, shape=(), dtype=int64)
tf.Tensor(3, shape=(), dtype=int64)
tf.Tensor(4, shape=(), dtype=int64)


(2)batch()


功能:将Dataset中连续的数据分割成批。


参数:


  • batch_size:在单个批次中合并的此数据集的连续元素数。
  • drop_remainder:如果最后一批的数据量少于指定的batch_size,是否抛弃最后一批,默认为False,表示不抛弃。


dataset = tf.data.Dataset.range(11)dataset_batch = dataset.batch(3)


for i in dataset_batch:    print(i)


tf.Tensor([0 1 2], shape=(3,), dtype=int64)
tf.Tensor([3 4 5], shape=(3,), dtype=int64)
tf.Tensor([6 7 8], shape=(3,), dtype=int64)
tf.Tensor([ 9 10], shape=(2,), dtype=int64)


dataset_batch = dataset.batch(3,drop_remainder=True)


for i in dataset_batch:    print(i)


tf.Tensor([0 1 2], shape=(3,), dtype=int64)
tf.Tensor([3 4 5], shape=(3,), dtype=int64)
tf.Tensor([6 7 8], shape=(3,), dtype=int64)


train_x = tf.random.uniform((10,3),maxval=100, dtype=tf.int32)train_y = tf.range(10)


dataset = tf.data.Dataset.from_tensor_slices((train_x, train_y))


for i in dataset.take(3):    print(i)


(<tf.Tensor: id=236, shape=(3,), dtype=int32, numpy=array([81, 53, 85], dtype=int32)>, <tf.Tensor: id=237, shape=(), dtype=int32, numpy=0>)
(<tf.Tensor: id=238, shape=(3,), dtype=int32, numpy=array([13,  7, 25], dtype=int32)>, <tf.Tensor: id=239, shape=(), dtype=int32, numpy=1>)
(<tf.Tensor: id=240, shape=(3,), dtype=int32, numpy=array([83, 25, 55], dtype=int32)>, <tf.Tensor: id=241, shape=(), dtype=int32, numpy=2>)


dataset_batch = dataset.batch(4)


for i in dataset_batch:    print(i)


(<tf.Tensor: id=250, shape=(4, 3), dtype=int32, numpy=
array([[81, 53, 85],
       [13,  7, 25],
       [83, 25, 55],
       [53, 41, 11]], dtype=int32)>, <tf.Tensor: id=251, shape=(4,), dtype=int32, numpy=array([0, 1, 2, 3], dtype=int32)>)
(<tf.Tensor: id=252, shape=(4, 3), dtype=int32, numpy=
array([[41, 58, 39],
       [44, 68, 55],
       [52, 34, 22],
       [66, 39,  5]], dtype=int32)>, <tf.Tensor: id=253, shape=(4,), dtype=int32, numpy=array([4, 5, 6, 7], dtype=int32)>)
(<tf.Tensor: id=254, shape=(2, 3), dtype=int32, numpy=
array([[73,  8, 20],
       [67, 71, 98]], dtype=int32)>, <tf.Tensor: id=255, shape=(2,), dtype=int32, numpy=array([8, 9], dtype=int32)>)


为什么在训练模型时要将Dataset分割成一个个batch呢?


  • 对于小数据集是否使用batch关系不大,但是对于大数据集如果不分割成batch意味着将这个数据集一次性输入模型中,容易造成内存爆炸。
  • 通过并行化提高内存的利用率。就是尽量让你的GPU满载运行,提高训练速度。
  • 单个epoch的迭代次数减少了,参数的调整也慢了,假如要达到相同的识别精度,需要更多的epoch。
  • 适当Batch Size使得梯度下降方向更加准确。


(3)padded_batch()


功能:batch()的进阶版,可以对shape不一致的连续元素进行分批。


参数:


  • batch_size:在单个批次中合并的此数据集的连续元素个数。
  • padded_shapes:tf.TensorShape或其他描述tf.int64矢量张量对象,表示在批处理之前每个输入元素的各个组件应填充到的形状。如果参数中有None,则表示将填充为每个批次中该尺寸的最大尺寸。
  • padding_values:要用于各个组件的填充值。默认值0用于数字类型,字符串类型则默认为空字符。
  • drop_remainder:如果最后一批的数据量少于指定的batch_size,是否抛弃最后一批,默认为False,表示不抛弃。


dataset = tf.data.Dataset.range(10)


dataset = dataset.map(lambda x: tf.fill([tf.cast(x, tf.int32)], x))


dataset_padded = dataset.padded_batch(4, padded_shapes=(None,))


for batch in dataset_padded:    print(batch.numpy())    print('---------------------')


[[0 0 0]
 [1 0 0]
 [2 2 0]
 [3 3 3]]
---------------------
[[4 4 4 4 0 0 0]
 [5 5 5 5 5 0 0]
 [6 6 6 6 6 6 0]
 [7 7 7 7 7 7 7]]
---------------------
[[8 8 8 8 8 8 8 8 0]
 [9 9 9 9 9 9 9 9 9]]
---------------------


dataset_padded = dataset.padded_batch(4, padded_shapes=(10,),padding_values=tf.constant(9,dtype=tf.int64))  # 修改填充形状和填充元素


for batch in dataset_padded:    print(batch.numpy())    print('---------------------')


[[9 9 9 9 9 9 9 9 9 9]
 [1 9 9 9 9 9 9 9 9 9]
 [2 2 9 9 9 9 9 9 9 9]
 [3 3 3 9 9 9 9 9 9 9]]
---------------------
[[4 4 4 4 9 9 9 9 9 9]
 [5 5 5 5 5 9 9 9 9 9]
 [6 6 6 6 6 6 9 9 9 9]
 [7 7 7 7 7 7 7 9 9 9]]
---------------------
[[8 8 8 8 8 8 8 8 9 9]
 [9 9 9 9 9 9 9 9 9 9]]
---------------------


(4)map()


功能:以dataset中每一位元素为参数执行pap_func()方法,这一功能在数据预处理中修改dataset中元素是很实用。


参数:

  • map_func:回调方法。


def change_dtype(t):  # 将类型修改为int32    return tf.cast(t,dtype=tf.int32)


dataset = tf.data.Dataset.range(3)


for i in dataset:    print(i)


tf.Tensor(0, shape=(), dtype=int64)
tf.Tensor(1, shape=(), dtype=int64)
tf.Tensor(2, shape=(), dtype=int64)


dataset_map = dataset.map(change_dtype)


for i in dataset_map:    print(i)


tf.Tensor(0, shape=(), dtype=int32)
tf.Tensor(1, shape=(), dtype=int32)
tf.Tensor(2, shape=(), dtype=int32)


map_func的参数必须对应dataset中的元素类型,例如,如果dataset中元素是tuple,map_func可以这么定义:


def change_dtype_2(t1,t2):    return t1/10,tf.cast(t2,dtype=tf.int32)*(-1)  # 第一位元素除以10,第二为元素乘以-1


dataset = tf.data.Dataset.from_tensor_slices((tf.range(3),tf.range(3)))


dataset_map = dataset.map(change_dtype_2)


for i in dataset_map:    print(i)


(<tf.Tensor: id=347, shape=(), dtype=float64, numpy=0.0>, <tf.Tensor: id=348, shape=(), dtype=int32, numpy=0>)
(<tf.Tensor: id=349, shape=(), dtype=float64, numpy=0.1>, <tf.Tensor: id=350, shape=(), dtype=int32, numpy=-1>)
(<tf.Tensor: id=351, shape=(), dtype=float64, numpy=0.2>, <tf.Tensor: id=352, shape=(), dtype=int32, numpy=-2>)


(5)filter()


功能:对Dataset中每一个执行指定过滤方法进行过滤,返回过滤后的Dataset对象

参数:

  • predicate:过滤方法,返回值必须为True或False


dataset = tf.data.Dataset.range(5)


def filter_func(t):  # 过滤出值为偶数的元素    if t % 2 == 0:        return True    else:        return False


dataset_filter = dataset.filter(filter_func)


for i in dataset_filter:    print(i)


tf.Tensor(0, shape=(), dtype=int64)
tf.Tensor(2, shape=(), dtype=int64)
tf.Tensor(4, shape=(), dtype=int64)


(6)shuffle()


功能:随机打乱数据


参数:


  • buffer_size:缓冲区大小,姑且认为是混乱程度吧,当值为1时,完全不打乱,当值为整个Dataset元素总数时,完全打乱。
  • seed:将用于创建分布的随机种子。
  • reshuffle_each_iteration:如果为true,则表示每次迭代数据集时都应进行伪随机重排,默认为True。


dataset = tf.data.Dataset.range(5)


dataset_s = dataset.shuffle(1)


for i in dataset_s:    print(i)


tf.Tensor(0, shape=(), dtype=int64)
tf.Tensor(1, shape=(), dtype=int64)
tf.Tensor(2, shape=(), dtype=int64)
tf.Tensor(3, shape=(), dtype=int64)
tf.Tensor(4, shape=(), dtype=int64)


dataset_s = dataset.shuffle(5)


for i in dataset_s:    print(i)


tf.Tensor(0, shape=(), dtype=int64)
tf.Tensor(4, shape=(), dtype=int64)
tf.Tensor(1, shape=(), dtype=int64)
tf.Tensor(2, shape=(), dtype=int64)
tf.Tensor(3, shape=(), dtype=int64)


(7)repeat()


功能:对Dataset中的数据进行重复,以创建新的Dataset

参数:

  • count:重复次数,默认为None,表示不重复,当值为-1时,表示无限重复。


dataset = tf.data.Dataset.range(3)


dataset_repeat = dataset.repeat(3)


for i in dataset_repeat:    print(i)


tf.Tensor(0, shape=(), dtype=int64)
tf.Tensor(1, shape=(), dtype=int64)
tf.Tensor(2, shape=(), dtype=int64)
tf.Tensor(0, shape=(), dtype=int64)
tf.Tensor(1, shape=(), dtype=int64)
tf.Tensor(2, shape=(), dtype=int64)
tf.Tensor(0, shape=(), dtype=int64)
tf.Tensor(1, shape=(), dtype=int64)
tf.Tensor(2, shape=(), dtype=int64
相关文章
|
5月前
|
机器学习/深度学习 自然语言处理 算法
【Tensorflow深度学习】优化算法、损失计算、模型评估、向量嵌入、神经网络等模块的讲解(超详细必看)
【Tensorflow深度学习】优化算法、损失计算、模型评估、向量嵌入、神经网络等模块的讲解(超详细必看)
58 1
|
机器学习/深度学习 TensorFlow 算法框架/工具
tensorflow.keras常用模块介绍
tensorflow.keras常用模块介绍
|
数据采集 大数据 测试技术
TensorFlow2.0(6):利用data模块进行数据预处理(上)
TensorFlow2.0(6):利用data模块进行数据预处理(上)
uiu
|
机器学习/深度学习 PyTorch TensorFlow
TensorFlow 2 基础概念语法与常用模块
TensorFlow 2 基础概念语法与常用模块
uiu
100 0
TensorFlow 2 基础概念语法与常用模块
|
TensorFlow 算法框架/工具 计算机视觉
二、cmd、pycharm下同时导入tensorflow,cv2模块,Anaconda导入tensorflow(超级详细步骤)
二、cmd、pycharm下同时导入tensorflow,cv2模块,Anaconda导入tensorflow(超级详细步骤)
二、cmd、pycharm下同时导入tensorflow,cv2模块,Anaconda导入tensorflow(超级详细步骤)
|
机器学习/深度学习 大数据 TensorFlow
TensorFlow Hub介绍:TensorFlow中可重用的机器学习模块库
本文对TensorFlow Hub库的介绍,并举例说明其用法。
7476 0
|
机器学习/深度学习 TensorFlow API
|
Web App开发 自然语言处理 TensorFlow
《TensorFlow技术解析与实战》——2.5 依赖的其他模块
本节书摘来自异步社区《TensorFlow技术解析与实战》一书中的第2章,第2.5节,作者李嘉璇,更多章节内容可以访问云栖社区“异步社区”公众号查看 第2章 TensorFlow环境的准备 2.5依赖的其他模块 TensorFlow在运行中需要做一些矩阵运算,时常会用到一些第三方模块,此外,在处理音频、自然语言时需要也要用到一些模块,建议一并安装好。
2518 0