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
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.
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.
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.
NumPy arrays can be defined using Python sequences such as lists and tuples. Lists and tuples are defined using [...] and (...) , respectively.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With