对传入的tensor在第一个维度进行切分,生成该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个不同tf.Tensor
import tensorflow as tf
import numpy as np
numpy_data = np.arange(16) # 创建numpy数组
numpy_dataset = tf.data.Dataset.from_tensor_slices(numpy_data)
## numpy数组转tf.Tensor数组(即:TensorSliceDataset)
print(numpy_dataset)
for data in numpy_dataset:
print(data)
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
tf.Tensor(0, shape=(), dtype=int32)
tf.Tensor(1, shape=(), dtype=int32)
tf.Tensor(2, shape=(), dtype=int32)
...
tf.Tensor(13, shape=(), dtype=int32)
tf.Tensor(14, shape=(), dtype=int32)
tf.Tensor(15, shape=(), dtype=int32)
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)
[[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]
(,
)
(,
)
(,
)
(,
)
(,
)
x
和y
均在第0维被切分成了5个tensor
,并且相应位置的元素在dataset
中组成了一组。
将python列表和numpy数组转换成tensorflow的dataset,因为只有dataset才能被model.fit函数训练
import tensorflow as tf
import numpy as np
features, labels = (np.random.sample((6, 3)), # 模拟6组数据,每组数据3个特征
np.random.sample((6, 1))) # 模拟6组数据,每组数据对应一个标签,注意两者的维数必须匹配
print((features, labels)) # 输出下组合的数据
data = tf.data.Dataset.from_tensor_slices((features, labels))
print(data) # 输出张量的信息
tf.data.Dataset.from_tensor_slices 的用法_夏华东的博客的博客-CSDN博客
tf.data.Dataset.from_tensor_slices()_我家空空的博客-CSDN博客
tf.data.Dataset.from_tensor_slices的用法_VolCui的博客-CSDN博客_from_tensor_slices