Random Shuffle along Axis #71409
Labels
feature
A request for a proper, new feature.
module: random
Related to random number generation in PyTorch (rng generator)
triaged
This issue has been looked at a team member, and triaged and prioritized into an appropriate module
馃殌 The feature, motivation and pitch
Dear PyTorch Devs,
Thank you for your hard work and dedication to creating a great ecosystem of tools and community of users.
This feature request proposes adding a standard lib function to shuffle "rows" across an axis of a tensor. We will consider two ways to do this (use-case 1 and use-case 2). Consider that an axis partitions a tensor into sub-tensors. For instance a tensor of shape (2,3,4) has 3 sub-tensors at axis=1.
Example input:
Use-case 1: Randomly shuffle an axis the same way for all sub-tensors. (This implementation is trivially "solved" below).
Use-case 2: Randomly shuffle an axis differently for each other axes
Where the
shufflerow
function (for use-case 2) could be implemented like thisIt would be convenient to have a simple function like
shuffle(tensor, axis, mode:str)
for both use cases, where for example, we might saymode='same'
(use case 1) ormode='different'
(use case 2).Alternatives
I do not know of a better shuffle implementation for use-case 2, but my proposed implementation seems not efficient. Maybe an improvement would have an implementation in C or cuda that covers use-case 2 without the for loops and without the repeat and gather.
Currently, we have torch.randperm to randomly shuffle one axis the same way across all the same way.
Perhaps off topic comment: I also wish PyTorch (and NumPy) had a toolkit dedicated to sampling, such as reservoir sampling across minibatches. Sampling often introduces subtle bugs.
Additional context
Variations of this feature request on the internet:
When axis=-1:
For 2-d tensor:
ChannelShuffle:
Thank you!
cc @pbelevich
The text was updated successfully, but these errors were encountered: