# How to implement tf.scatter\_nd() function of tensorflow in pytorch

**URL:** <https://discuss.pytorch.org/t/how-to-implement-tf-scatter-nd-function-of-tensorflow-in-pytorch/18358>\
**Category:** Uncategorized\
**Created:** [May 19, 2018, 3:38pm UTC](https://discuss.pytorch.org/t/how-to-implement-tf-scatter-nd-function-of-tensorflow-in-pytorch/18358 "2018-05-19T15:38:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![11196](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/1/dec6dc/32.png) [@11196](https://discuss.pytorch.org/u/11196)\
**Post date:** [May 19, 2018, 3:38pm UTC](https://discuss.pytorch.org/t/how-to-implement-tf-scatter-nd-function-of-tensorflow-in-pytorch/18358/1 "2018-05-19T15:38:02Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![albanD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/32/215_2.png) [@albanD](https://discuss.pytorch.org/u/albanD)\
**Post date:** [May 19, 2018, 3:49pm UTC](https://discuss.pytorch.org/t/how-to-implement-tf-scatter-nd-function-of-tensorflow-in-pytorch/18358/2 "2018-05-19T15:49:39Z")

</div>

Hi,

You can use the [`scatter_()`](https://pytorch.org/docs/stable/tensors.html#torch.Tensor.scatter_) function.

---

<div class="post-metadata">

**Author:** ![11196](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/1/dec6dc/32.png) [@11196](https://discuss.pytorch.org/u/11196)\
**Post date:** [May 19, 2018, 6:20pm UTC](https://discuss.pytorch.org/t/how-to-implement-tf-scatter-nd-function-of-tensorflow-in-pytorch/18358/3 "2018-05-19T18:20:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![albanD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/32/215_2.png) [@albanD](https://discuss.pytorch.org/u/albanD)\
**Post date:** [May 30, 2018, 9:35am UTC](https://discuss.pytorch.org/t/how-to-implement-tf-scatter-nd-function-of-tensorflow-in-pytorch/18358/4 "2018-05-30T09:35:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lkhphuc](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/lkhphuc/32/5122_2.png) [@lkhphuc](https://discuss.pytorch.org/u/lkhphuc)\
**Post date:** [December 3, 2018, 8:36am UTC](https://discuss.pytorch.org/t/how-to-implement-tf-scatter-nd-function-of-tensorflow-in-pytorch/18358/5 "2018-12-03T08:36:37Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![pg2455](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/pg2455/32/4183_2.png) [@pg2455](https://discuss.pytorch.org/u/pg2455)\
**Post date:** [February 6, 2020, 3:58pm UTC](https://discuss.pytorch.org/t/how-to-implement-tf-scatter-nd-function-of-tensorflow-in-pytorch/18358/6 "2020-02-06T15:58:40Z")

</div>

[index\_add\_](https://pytorch.org/docs/stable/tensors.html?highlight=index_add_#torch.Tensor.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\_.
