Differences with torch.Tensor.masked_scatter

View Source On Gitee

torch.Tensor.masked_scatter

torch.Tensor.masked_scatter(mask, tensor) -> Tensor

For more information, see torch.Tensor.masked_scatter.

mindspore.Tensor.masked_scatter

mindspore.Tensor.masked_scatter(mask, tensor) -> Tensor

For more information, see mindspore.Tensor.masked_scatter.

Differences

PyTorch: Returns a Tensor. Updates the value in the “self Tensor” with the tensor value according to the mask.

MindSpore: MindSpore API Basically achieves the same function as PyTorch. But PyTorch supports bidirectional broadcast of mask and “self Tensor”, MindSpore only supports mask broadcasting to “self Tensor”.

Categories

Subcategories

PyTorch

MindSpore

Difference

Parameters

Parameter 1

mask

mask

PyTorch supports bidirectional broadcast of mask and “self Tensor”, MindSpore only supports mask broadcasting to “self Tensor”.

Parameter 2

tensor

tensor

-

Code Example 1

# PyTorch
import torch

self = torch.tensor([0, 0, 0, 0, 0])
mask = torch.tensor([[0, 0, 0, 1, 1], [1, 1, 0, 1, 1]])
source = torch.tensor([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]])
output = self.masked_scatter(mask, source)
print(output)
# tensor([[0, 0, 0, 0, 1],
#         [2, 3, 0, 4, 5]])

# MindSpore
import mindspore
from mindspore import Tensor
import numpy as np

self = Tensor(np.array([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]), mindspore.int32)
mask = Tensor(np.array([[False, False, False, True, True], [True, True, False, True, True]]), mindspore.bool_)
source = Tensor(np.array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]), mindspore.int32)
output = self.masked_scatter(mask, source)
print(output)
# [[0 0 0 0 1]
#  [2 3 0 4 5]]

Code Example 2

import torch

self = torch.tensor([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0]])
mask = torch.tensor([0, 0, 0, 1, 1])
source = torch.tensor([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]])
output = self.masked_scatter(mask, source)
print(output)
# tensor([[0, 0, 0, 0, 1],
#         [0, 0, 0, 2, 3]])

# MindSpore
import mindspore
from mindspore import Tensor
import numpy as np

self = Tensor(np.array([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]), mindspore.int32)
mask = Tensor(np.array([False, False, False, True, True]), mindspore.bool_)
source = Tensor(np.array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]), mindspore.int32)
output = self.masked_scatter(mask, source)
print(output)
# [[0 0 0 0 1]
#  [0 0 0 2 3]]