tf.data.experimental.sample_from_datasets的用法举例

tf.data.experimental.sample_from_datasets的用法举例

环境

tensorflow版本:2.4

正文

  关于这个函数官网没有给出详细的例程说明,尤其是它返回的函数的元素数量怎么确定,下面的例程可以很好的说明这一问题。

ds_datasets = []
ds1 = tf.data.Dataset.range(0,10)
ds_datasets.append(ds1)
ds2 = tf.data.Dataset.range(10,20)
ds_datasets.append(ds2)
ds3 = tf.data.Dataset.range(20,30)
ds_datasets.append(ds3)
ds4 = tf.data.Dataset.range(30, 40)
ds_datasets.append(ds4)
ds5 = tf.data.Dataset.range(40, 50)
ds_datasets.append(ds5)
ds6 = tf.data.Dataset.range(50, 55)
ds_datasets.append(ds6)
single_ds = tf.data.experimental.sample_from_datasets(ds_datasets)
i = 0
for element in single_ds:
  print(element)
  i += 1
print(i)

打印结果:

tf.Tensor(40, shape=(), dtype=int64)
tf.Tensor(10, shape=(), dtype=int64)
tf.Tensor(11, shape=(), dtype=int64)
tf.Tensor(20, shape=(), dtype=int64)
tf.Tensor(0, shape=(), dtype=int64)
tf.Tensor(21, shape=(), dtype=int64)
tf.Tensor(22, shape=(), dtype=int64)
tf.Tensor(30, shape=(), dtype=int64)
tf.Tensor(12, shape=(), dtype=int64)
tf.Tensor(41, shape=(), dtype=int64)
tf.Tensor(31, shape=(), dtype=int64)
tf.Tensor(32, shape=(), dtype=int64)
tf.Tensor(1, shape=(), dtype=int64)
tf.Tensor(42, shape=(), dtype=int64)
tf.Tensor(13, shape=(), dtype=int64)
tf.Tensor(43, shape=(), dtype=int64)
tf.Tensor(23, shape=(), dtype=int64)
tf.Tensor(50, shape=(), dtype=int64)
tf.Tensor(33, shape=(), dtype=int64)
tf.Tensor(24, shape=(), dtype=int64)
tf.Tensor(2, shape=(), dtype=int64)
tf.Tensor(3, shape=(), dtype=int64)
tf.Tensor(51, shape=(), dtype=int64)
tf.Tensor(34, shape=(), dtype=int64)
tf.Tensor(52, shape=(), dtype=int64)
tf.Tensor(53, shape=(), dtype=int64)
tf.Tensor(54, shape=(), dtype=int64)
tf.Tensor(4, shape=(), dtype=int64)
tf.Tensor(14, shape=(), dtype=int64)
tf.Tensor(5, shape=(), dtype=int64)
tf.Tensor(15, shape=(), dtype=int64)
tf.Tensor(6, shape=(), dtype=int64)
tf.Tensor(16, shape=(), dtype=int64)
tf.Tensor(7, shape=(), dtype=int64)
tf.Tensor(44, shape=(), dtype=int64)
tf.Tensor(8, shape=(), dtype=int64)
tf.Tensor(9, shape=(), dtype=int64)
tf.Tensor(25, shape=(), dtype=int64)
tf.Tensor(26, shape=(), dtype=int64)
tf.Tensor(27, shape=(), dtype=int64)
tf.Tensor(17, shape=(), dtype=int64)
tf.Tensor(18, shape=(), dtype=int64)
tf.Tensor(45, shape=(), dtype=int64)
tf.Tensor(35, shape=(), dtype=int64)
tf.Tensor(28, shape=(), dtype=int64)
tf.Tensor(36, shape=(), dtype=int64)
tf.Tensor(29, shape=(), dtype=int64)
tf.Tensor(37, shape=(), dtype=int64)
tf.Tensor(38, shape=(), dtype=int64)
tf.Tensor(46, shape=(), dtype=int64)
tf.Tensor(39, shape=(), dtype=int64)
tf.Tensor(47, shape=(), dtype=int64)
tf.Tensor(19, shape=(), dtype=int64)
tf.Tensor(48, shape=(), dtype=int64)
tf.Tensor(49, shape=(), dtype=int64)
55

 从打印内容看,该函数随机从数据集容器中的数据集中取元素,并放入一个新的数据集中,直至将所有元素取完为止。
 但是上面程序没有加权重的程序,加上权重,没有发现什么不一样的结果,如果谁知道可以在流言区恢复

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值