mindspore.dataset.transforms.Fill

class mindspore.dataset.transforms.Fill(fill_value)[源代码]

将Tensor的所有元素都赋值为指定的值。输出Tensor将与输入Tensor具有相同的shape和数据类型。

参数:
  • fill_value (Union[str, bytes, int, float, bool]) - 用于填充Tensor的值。

异常:
  • TypeError - 参数 fill_value 类型不为str、float、bool、int或bytes。

支持平台:

CPU

样例:

>>> import numpy as np
>>> import mindspore.dataset as ds
>>> import mindspore.dataset.transforms as transforms
>>>
>>> # Use the transform in dataset pipeline mode
>>> # generate a 1D integer numpy array from 0 to 4
>>> def generator_1d():
...     for i in range(5):
...         yield (np.array([i]),)
>>> generator_dataset = ds.GeneratorDataset(generator_1d, column_names="col1")
>>> # [[0], [1], [2], [3], [4]]
>>> fill_op = transforms.Fill(3)
>>> generator_dataset = generator_dataset.map(operations=fill_op)
>>> for item in generator_dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
...     print(item["col1"].shape, item["col1"].dtype)
...     break
(1,) int64
>>>
>>> # Use the transform in eager mode
>>> data = np.array([1, 2, 3])
>>> output = transforms.Fill(100)(data)
>>> print(output.shape, output.dtype)
(3,) int64