Tensorflow – Dataset 之 repeat(), shuffle(), batch()作用

repeat() – 该函数让数据集重复的次数, 如没有参数,则数据集可以任意获取

shuffle() – 打乱数据集的顺序

batch() – 设置一次操作允许获取的数据个数

例子如下:

import tensorflow as tf

import numpy as np

feature = np.array([1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ], np.float32)

label = np.array([0 ,0 ,0 ,0 ,1 ,1 ,1 ,1 ,1 ])

train_data = tf.data.Dataset.from_tensor_slices((feature, label)) //定义9 个数据的数据集

def print_train_data (data ,cnt ):

it = data.iter()

for i in range (cnt):

x, y = it.next ()

print (x, y)

print_train_data(train_data,9 )

print (“=== after repeat ====”)

train_data = train_data.repeat()//调用该函数后后面可以无限使用该数据集

print_train_data(train_data,9 )

print (“=== after shuffle ====”)

train_data = train_data.shuffle(buffer_size=5 ) //打乱数据集的顺序

print_train_data(train_data,9 )

print (“=== after batch ====”)

dataset_batch = train_data.batch(batch_size=3 )//设置每次回去数据集的数据条数

it = dataset_batch.iter()

for i in range (20 ): //从数据集中取20次数据,由于上面repeat()调用,表面可以无限使用数据集,因此这里的range里的参数可以任意填写。 如果这样调用repeat(2). 则最多只能获取9 * 2个数据. 超出数据会报错

x, y = it.next ()

print (x, y)//由于batch()设置了每次取3条数据,因此,这里的打印X,Y都是3个数据的数组。 所以这个for 循环, 总共获取了20 * 3条数据

打印数据如下:

tf.Tensor(1.0, shape=(), dtype=float32) tf.Tensor(0, shape=(), dtype=int64)
tf.Tensor(2.0, shape=(), dtype=float32) tf.Tensor(0, shape=(), dtype=int64)
tf.Tensor(3.0, shape=(), dtype=float32) tf.Tensor(0, shape=(), dtype=int64)
tf.Tensor(4.0, shape=(), dtype=float32) tf.Tensor(0, shape=(), dtype=int64)
tf.Tensor(5.0, shape=(), dtype=float32) tf.Tensor(1, shape=(), dtype=int64)
tf.Tensor(6.0, shape=(), dtype=float32) tf.Tensor(1, shape=(), dtype=int64)
tf.Tensor(7.0, shape=(), dtype=float32) tf.Tensor(1, shape=(), dtype=int64)
tf.Tensor(8.0, shape=(), dtype=float32) tf.Tensor(1, shape=(), dtype=int64)
tf.Tensor(9.0, shape=(), dtype=float32) tf.Tensor(1, shape=(), dtype=int64)
=== after repeat ====
tf.Tensor(1.0, shape=(), dtype=float32) tf.Tensor(0, shape=(), dtype=int64)
tf.Tensor(2.0, shape=(), dtype=float32) tf.Tensor(0, shape=(), dtype=int64)
tf.Tensor(3.0, shape=(), dtype=float32) tf.Tensor(0, shape=(), dtype=int64)
tf.Tensor(4.0, shape=(), dtype=float32) tf.Tensor(0, shape=(), dtype=int64)
tf.Tensor(5.0, shape=(), dtype=float32) tf.Tensor(1, shape=(), dtype=int64)
tf.Tensor(6.0, shape=(), dtype=float32) tf.Tensor(1, shape=(), dtype=int64)
tf.Tensor(7.0, shape=(), dtype=float32) tf.Tensor(1, shape=(), dtype=int64)
tf.Tensor(8.0, shape=(), dtype=float32) tf.Tensor(1, shape=(), dtype=int64)
tf.Tensor(9.0, shape=(), dtype=float32) tf.Tensor(1, shape=(), dtype=int64)
=== after shuffle ====
tf.Tensor(5.0, shape=(), dtype=float32) tf.Tensor(1, shape=(), dtype=int64)
tf.Tensor(3.0, shape=(), dtype=float32) tf.Tensor(0, shape=(), dtype=int64)
tf.Tensor(6.0, shape=(), dtype=float32) tf.Tensor(1, shape=(), dtype=int64)
tf.Tensor(1.0, shape=(), dtype=float32) tf.Tensor(0, shape=(), dtype=int64)
tf.Tensor(4.0, shape=(), dtype=float32) tf.Tensor(0, shape=(), dtype=int64)
tf.Tensor(2.0, shape=(), dtype=float32) tf.Tensor(0, shape=(), dtype=int64)
tf.Tensor(8.0, shape=(), dtype=float32) tf.Tensor(1, shape=(), dtype=int64)
tf.Tensor(1.0, shape=(), dtype=float32) tf.Tensor(0, shape=(), dtype=int64)
tf.Tensor(2.0, shape=(), dtype=float32) tf.Tensor(0, shape=(), dtype=int64)
=== after batch ====
tf.Tensor([2. 4. 5.], shape=(3,), dtype=float32) tf.Tensor([0 0 1], shape=(3,), dtype=int64)
tf.Tensor([1. 7. 3.], shape=(3,), dtype=float32) tf.Tensor([0 1 0], shape=(3,), dtype=int64)
tf.Tensor([8. 6. 1.], shape=(3,), dtype=float32) tf.Tensor([1 1 0], shape=(3,), dtype=int64)
tf.Tensor([3. 6. 4.], shape=(3,), dtype=float32) tf.Tensor([0 1 0], shape=(3,), dtype=int64)
tf.Tensor([7. 8. 1.], shape=(3,), dtype=float32) tf.Tensor([1 1 0], shape=(3,), dtype=int64)
tf.Tensor([5. 2. 4.], shape=(3,), dtype=float32) tf.Tensor([1 0 0], shape=(3,), dtype=int64)
tf.Tensor([9. 5. 3.], shape=(3,), dtype=float32) tf.Tensor([1 1 0], shape=(3,), dtype=int64)
tf.Tensor([9. 9. 7.], shape=(3,), dtype=float32) tf.Tensor([1 1 1], shape=(3,), dtype=int64)
tf.Tensor([8. 2. 6.], shape=(3,), dtype=float32) tf.Tensor([1 0 1], shape=(3,), dtype=int64)
tf.Tensor([3. 1. 2.], shape=(3,), dtype=float32) tf.Tensor([0 0 0], shape=(3,), dtype=int64)
tf.Tensor([8. 9. 4.], shape=(3,), dtype=float32) tf.Tensor([1 1 0], shape=(3,), dtype=int64)
tf.Tensor([1. 5. 7.], shape=(3,), dtype=float32) tf.Tensor([0 1 1], shape=(3,), dtype=int64)
tf.Tensor([5. 6. 4.], shape=(3,), dtype=float32) tf.Tensor([1 1 0], shape=(3,), dtype=int64)
tf.Tensor([3. 6. 9.], shape=(3,), dtype=float32) tf.Tensor([0 1 1], shape=(3,), dtype=int64)
tf.Tensor([1. 7. 4.], shape=(3,), dtype=float32) tf.Tensor([0 1 0], shape=(3,), dtype=int64)
tf.Tensor([2. 2. 3.], shape=(3,), dtype=float32) tf.Tensor([0 0 0], shape=(3,), dtype=int64)
tf.Tensor([5. 8. 8.], shape=(3,), dtype=float32) tf.Tensor([1 1 1], shape=(3,), dtype=int64)
tf.Tensor([2. 6. 4.], shape=(3,), dtype=float32) tf.Tensor([0 1 0], shape=(3,), dtype=int64)
tf.Tensor([1. 9. 7.], shape=(3,), dtype=float32) tf.Tensor([0 1 1], shape=(3,), dtype=int64)
tf.Tensor([6. 3. 5.], shape=(3,), dtype=float32) tf.Tensor([1 0 1], shape=(3,), dtype=int64)

Original: https://blog.csdn.net/aaronychen/article/details/122879141
Author: aaronychen
Title: Tensorflow – Dataset 之 repeat(), shuffle(), batch()作用

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/509532/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球