mindspore.ops.Map

class mindspore.ops.Map(ops=None, reverse=False)[源代码]

Map将对输入序列应用设置的函数操作。

此操作将应用到输入序列的每个元素。

参数:
  • ops (Union[MultitypeFuncGraph, None]) - ops 是要应用的操作。如果 ops 为None,则操作应放在实例的第一个输入中。默认值:None。

  • reverse (bool) - 在某些场景中,优化器需要反转以提高并行性能,一般用户可忽略。 reverse 表示是否为反向应用操作的标志。仅支持图模式。默认值:False。

输入:
  • args (Tuple[sequence]) - 如果 ops 不是None,则所有输入的序列和序列的每一行都应该是相同长度。例如,如果 args 的长度为2,那么每个序列 (args[0][i],args[1][i]) 长度的 i 将作为操作的输入。如果 ops 为None,则第一个输入是操作,另一个输入是输入。

输出:

序列,进行函数操作后的输出序列。例如 operation(args[0][i], args[1][i])

支持平台:

Ascend GPU CPU

样例:

>>> from mindspore import dtype as mstype
>>> from mindspore import Tensor, ops
>>> from mindspore.ops import MultitypeFuncGraph, Map
>>> tensor_list = (Tensor(1, mstype.float32), Tensor(2, mstype.float32), Tensor(3, mstype.float32))
>>> # square all the tensor in the list
>>>
>>> square = MultitypeFuncGraph('square')
>>> @square.register("Tensor")
... def square_tensor(x):
...     return ops.square(x)
>>>
>>> common_map = Map()
>>> output = common_map(square, tensor_list)
>>> print(output)
(Tensor(shape=[], dtype=Float32, value= 1), Tensor(shape=[], dtype=Float32, value= 4),
Tensor(shape=[], dtype=Float32, value= 9))
>>> square_map = Map(square, False)
>>> output = square_map(tensor_list)
>>> print(output)
(Tensor(shape=[], dtype=Float32, value= 1), Tensor(shape=[], dtype=Float32, value= 4),
Tensor(shape=[], dtype=Float32, value= 9))