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(datacnt):    

  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)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值