tf.data.Dataset.from_tensor_slices()

函数原型:

tf.data.Dataset.from_tensor_slices(tensors)

作用是对传入的tensor在第一个维度进行切分,生成相应的dataset

实例一
import tensorflow as tf
import numpy as np
x = np.random.uniform(size=(5, 2))
print(x)
dataset = tf.data.Dataset.from_tensor_slices(x)
for ele in dataset:
	print(ele)

输出结果:

[[0.12831178 0.79162472]
 [0.35773058 0.47912444]
 [0.82993602 0.27870491]
 [0.77596611 0.2247823 ]
 [0.74720115 0.44322264]]

tf.Tensor([0.12831178 0.79162472], shape=(2,), dtype=float64)
tf.Tensor([0.35773058 0.47912444], shape=(2,), dtype=float64)
tf.Tensor([0.82993602 0.27870491], shape=(2,), dtype=float64)
tf.Tensor([0.77596611 0.2247823 ], shape=(2,), dtype=float64)
tf.Tensor([0.74720115 0.44322264], shape=(2,), dtype=float64)

可以看到ndarray 类型的x被在第0维切分成了5个不同tensor

实例二
import tensorflow as tf
import numpy as np
x = np.random.uniform(size=(5, 2))
print(x)
y = np.random.uniform(size=(5,))
print(y)
dataset = tf.data.Dataset.from_tensor_slices((x, y))
for ele in dataset:
	print(ele)
dataset

输出结果:

[[0.12831178 0.79162472]
 [0.35773058 0.47912444]
 [0.82993602 0.27870491]
 [0.77596611 0.2247823 ]
 [0.74720115 0.44322264]]

[0.80429699 0.66643469 0.43370019 0.68620174 0.62197102]

(<tf.Tensor: id=21, shape=(2,), dtype=float64, numpy=array([0.12831178, 0.79162472])>, <tf.Tensor: id=22, shape=(), dtype=float64, numpy=0.8042969945563568>)
(<tf.Tensor: id=23, shape=(2,), dtype=float64, numpy=array([0.35773058, 0.47912444])>, <tf.Tensor: id=24, shape=(), dtype=float64, numpy=0.6664346851579367>)
(<tf.Tensor: id=25, shape=(2,), dtype=float64, numpy=array([0.82993602, 0.27870491])>, <tf.Tensor: id=26, shape=(), dtype=float64, numpy=0.4337001933185155>)
(<tf.Tensor: id=27, shape=(2,), dtype=float64, numpy=array([0.77596611, 0.2247823 ])>, <tf.Tensor: id=28, shape=(), dtype=float64, numpy=0.6862017421778791>)
(<tf.Tensor: id=29, shape=(2,), dtype=float64, numpy=array([0.74720115, 0.44322264])>, <tf.Tensor: id=30, shape=(), dtype=float64, numpy=0.6219710239792507>)

<TensorSliceDataset shapes: ((2,), ()), types: (tf.float64, tf.float64)>

xy均在第0维被切分成了5个tensor,并且相应位置的元素在dataset中组成了一组。

实例三:使用字典+tf.data.Dataset.slices()创建dataset
import tensorflow as tf
import numpy as np
dict_data = dict([('a', [1,2]), ('b', [3, 4]), ('c', [5, 6])])
dict_data
dataset = tf.data.Dataset.from_tensor_slices(dict_data)
for ele in dataset:
	print(ele)
dataset

输出结果:

{'a': [1, 2], 'b': [3, 4], 'c': [5, 6]}

{'a': <tf.Tensor: id=40, shape=(), dtype=int32, numpy=1>, 'b': <tf.Tensor: id=41, shape=(), dtype=int32, numpy=3>, 'c': <tf.Tensor: id=42, shape=(), dtype=int32, numpy=5>}
{'a': <tf.Tensor: id=43, shape=(), dtype=int32, numpy=2>, 'b': <tf.Tensor: id=44, shape=(), dtype=int32, numpy=4>, 'c': <tf.Tensor: id=45, shape=(), dtype=int32, numpy=6>}

<TensorSliceDataset shapes: (2,), types: tf.float64>

这里我也不太明白为什么是这样一个结果?

打印使用tf.data.Dataset.from_tensor_slices()生成的dataset的方法
使用for循环
for ele in dataset:
	print(ele)
使用dataset.take,打印固定个数的元素
for ele in dataset.take(5):
	print(ele)
打印dataset中的下一个元素
next(iter(dataset.take(1)))
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值