How to implement tf.scatter_nd() function of tensorflow in pytorch

I want to implement the function called tf.scatter_nd() in pytorch. And I do not find a suitable function in pytorch. Does someone know how to do this?
Thank you!

2 Likes

Hi,

You can use the scatter_() function.

2 Likes

However, scatter_() is just a tensor operation. It does not have a autograd function. So maybe it cannot be used in network training.

All operations are actually autograd functions. You’ll get an error if the backward function is not implemented.

Hi, can you help me with a minimal examples of converting a tf.scatter_nd using torch.Tensor.scatter_add_.
The closest I’ve found is using the fancy indexing in pytorch, https://gist.github.com/airalcorn2/c7846d6fcb58a30b25ea6d97e16fe025.
But the fancy indexing technique does not match with tf.scatter_nd when there is duplication in the index tensor, because in tf the behavior is accumulating, while fancy indexing will just replace the value at that index.
Thanks.

index_add_ might be of help. I spent several hours looking for scatter_nd like function in pytorch, but all the links pointed to scatter_add_. The tensorflow like scatter_nd is more similar to index_add_.

1 Like