Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do scatter and gather operations in numpy?

I want to implement scatter and gather operations of Tensorflow or PyTorch in Numpy. I have been scratching my head for a while. Any pointers are greatly appreciated!

  • PyTorch Scatter

  • PyTorch Gather

like image 280
Sia Rezaei Avatar asked Sep 06 '17 02:09

Sia Rezaei


People also ask

What are scatter and gather operations?

Gather operators fetch data at the indexed locations and provide this as the output, while scatter operators usually update the output data at the indexed locations. Gather and scatter operators are often inverse operations of each other.

What is a scatter operation?

The communication collective operation “scatter” distributes data of one process in separate parts to all the processes (including itself) within the scope of a communicator. The communicator of size processes disseminates the data of the source process in size-equal partitions.

What does NumPy array list do?

NumPy arrays are used to store lists of numerical data and to represent vectors, matrices, and even tensors. NumPy arrays are designed to handle large data sets efficiently and with a minimum of fuss. The NumPy library has a large set of routines for creating, manipulating, and transforming NumPy arrays.

How to define a NumPy array?

NumPy arrays can be defined using Python sequences such as lists and tuples. Lists and tuples are defined using [...] and (...) , respectively.


1 Answers

There are two built-in numpy functions that suit your request.

You can use np.take_along_axis to implement torch.gather, and use np.put_along_axis to implement torch.scatter

like image 87
cying Avatar answered Sep 17 '22 16:09

cying