Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tensor flip along dimension #1099

Closed
rMazeiks opened this issue Dec 26, 2023 · 5 comments · Fixed by #1468
Closed

Tensor flip along dimension #1099

rMazeiks opened this issue Dec 26, 2023 · 5 comments · Fixed by #1468
Labels
feature The feature request good first issue Good for newcomers

Comments

@rMazeiks
Copy link

Thanks for the amazing library! 🦀

Feature description

I would like to flip a tensor along a given dimension. This would be like pytorch's flip function.

Basically it just creates a "mirror" image of the tensor:

Reverse the order of an n-D tensor along given axis in dims.

Parameters

input (Tensor) – the input tensor.

dims (a list or tuple) – axis to flip on

Feature motivation

I want to create a reverse diagonal, but I'm sure there are many more usecases, as this seems like a very basic operation.

Forgive me if there's a way to do this that I missed.

@rMazeiks
Copy link
Author

If anyone else runs into this, here's code for creating a reverse-diagonal square matrix:

fn reverse_diagonal<B: Backend>(size: usize) -> Tensor<B, 2> {
    let t = Tensor::zeros([size * size]);
    let indices = Tensor::arange(1..(size + 1)).mul_scalar(size as i64 - 1);
    let values = Tensor::ones([size]);
    t.select_assign(0, indices, values).reshape([size, size])
}

You can left-multiply with this to flip the rows, or right-multiply with it to flip the columns. It should work with any matrix, even non-square ones.

However, this feels like a workaround, and a native flip operation would be nice I think.

@antimora antimora added feature The feature request good first issue Good for newcomers labels Dec 26, 2023
@dush-t
Copy link

dush-t commented Feb 8, 2024

Hi, I'd like to take a shot at this, if nobody is working on it already.

@carrotflakes
Copy link
Contributor

@dush-t
Hi, Are you still working on it?

@antimora
Copy link
Collaborator

@carrotflakes it has been a month and we don't we any open PR related. You can give a try. We will review your quickly.

@jungerm2
Copy link

jungerm2 commented Jun 9, 2024

It seems there's a bug in this implementation as it relates to transpose:

let device = &Default::default();
type B = burn::backend::wgpu::JitBackend<WgpuRuntime<AutoGraphicsApi, f32, i32>>;
let arr: Tensor<B, 2> = Tensor::from_ints([[1, 2], [3, 4], [5, 6]], device).float();
let arr2 = arr.flip([0]).transpose(); // equivalent to rotating 90 degrees

// This correctly rotates the matrix and will print out
// [[5, 3, 1], [6, 4, 2]] 
println!("{arr2}");

// But the following should yield the same result and doesn't:
let arr2 = arr.transpose().flip([1]); // equivalent to rotating 90 degrees

// This will print out an unexpected result:
// [[5.0, 6.0, 3.0], [4.0, 1.0, 2.0]]
println!("{arr2}");

Can anyone confirm this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature The feature request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants