比较与tf.data.Dataset.from_tensor_slices的功能差异

tf.data.Dataset.from_tensor_slices

@staticmethod
tf.data.Dataset.from_tensor_slices(
    tensors
)

更多内容详见tf.data.Dataset.from_tensor_slices

mindspore.dataset.NumpySlicesDataset

class mindspore.dataset.NumpySlicesDataset(
    data,
    column_names=None,
    num_samples=None,
    num_parallel_workers=1,
    shuffle=None,
    sampler=None,
    num_shards=None,
    shard_id=None
)

更多内容详见mindspore.dataset.NumpySlicesDataset

使用方式

TensorFlow:一个静态方法,使用指定的 tf.Tensor 创建数据集。

MindSpore:一个数据集类,使用指定的 listtupledictnumpy.ndarray 创建数据集。

代码示例

# The following implements NumpySlicesDataset with MindSpore.
import numpy as np
import mindspore.dataset as ds

data = np.array([[1, 2], [3, 4], [5, 6]])
dataset = ds.NumpySlicesDataset(data=data, column_names=["data"], shuffle=False)

for item in dataset.create_dict_iterator():
    print(item["data"])
# [1 2]
# [3 4]
# [5 6]

# The following implements from_tensor_slices with TensorFlow.
import tensorflow as tf
tf.compat.v1.enable_eager_execution()

data = tf.constant([[1, 2], [3, 4], [5, 6]])
dataset = tf.data.Dataset.from_tensor_slices(data)

for value in dataset:
    print(value)
# [1 2]
# [3 4]
# [5 6]