import numpy as np
input_data = np.arange(16)
input_data
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15])
Dataset操作,转换为tenser格式
dataset = tf.data.Dataset.from_tensor_slices(input_data)
for data in dataset:
print (data)
tf.Tensor(0, shape=(), dtype=int32) tf.Tensor(1, shape=(),
dtype=int32) tf.Tensor(2, shape=(), dtype=int32) tf.Tensor(3,
shape=(), dtype=int32) tf.Tensor(4, shape=(), dtype=int32)
tf.Tensor(5, shape=(), dtype=int32) tf.Tensor(6, shape=(),
dtype=int32) tf.Tensor(7, shape=(), dtype=int32) tf.Tensor(8,
shape=(), dtype=int32) tf.Tensor(9, shape=(), dtype=int32)
tf.Tensor(10, shape=(), dtype=int32) tf.Tensor(11, shape=(),
dtype=int32) tf.Tensor(12, shape=(), dtype=int32) tf.Tensor(13,
shape=(), dtype=int32) tf.Tensor(14, shape=(), dtype=int32)
tf.Tensor(15, shape=(), dtype=int32)
repeat操作,重复构建数据集
dataset = tf.data.Dataset.from_tensor_slices(input_data)
dataset = dataset.repeat(2)
for data in dataset:
print (data)
tf.Tensor(0, shape=(), dtype=int32) tf.Tensor(1, shape=(),
dtype=int32) tf.Tensor(2, shape=(), dtype=int32) tf.Tensor(3,
shape=(), dtype=int32) tf.Tensor(4, shape=(), dtype=int32)
tf.Tensor(5, shape=(), dtype=int32) tf.Tensor(6, shape=(),
dtype=int32) tf.Tensor(7, shape=(), dtype=int32) tf.Tensor(8,
shape=(), dtype=int32)