Error on torch.Tensor.scatter

For the document example:

x = torch.rand(2, 5)
torch.zeros(3, 5).scatter_(0, torch.tensor([[0, 1, 2, 0, 0], [2, 0, 0, 1, 2]]), x)

there is no problem, however if the code snippet is changed to

x = torch.rand(2, 5)
torch.Tensor.scatter(0, torch.tensor([[0, 1, 2, 0, 0], [2, 0, 0, 1, 2]]), x)

Error raised as TypeError: descriptor 'scatter' requires a 'torch._C._TensorBase' object but received a 'int'

You need to give it x as the Tensor to scatter the values into :slight_smile:

Sorry I don’t get it. According to the documentation, https://pytorch.org/docs/stable/tensors.html#torch.Tensor.scatter, the syntax is
scatter(dim, index, source) → Tensor, so where should I put the Tensor to scatter the values into?

Are you saying z.scatter(0, torch.tensor([[0, 1, 2, 0, 0], [2, 0, 0, 1, 2]]), x)?

The torch.Tensor elements in the docs are methods on Tensors.
So they should be called on a tensor: x.scatter(xxx).

If you want to use the version in torch., then you need to provide the tensor as first argument: torch.scatter(x, foo).