函数原型:
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)>
x
和y
均在第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)))