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

Add tensor slice indexing #935

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

OrigamiDream
Copy link

This PR aims to provide a Slice(start, end, step). The following illustrates the usage.

use tch::{Tensor, IndexOp, Slice};

let tensor = Tensor::from_slice(&[1, 2, 3, 4, 5, 6]).view((2, 3));
let result = tensor.i((.., Slice::new(None, None, 2)));

It shows exactly same behavior in PyTorch: tensor[:, ::2].

I had considered several ways of using Slice, such as:

let _ = Slice(0, -1, 2); // all exactly `i64`
let _ = Slice(Some(0), None, 2); // uses `Option<i64>` for `start` and `end`
let _ = Slice::new(0, None, 2); // add `new` method and uses `impl Into<Option<i64>>`

I ended up using the third one, which feels more natural to me. But I hope to discuss about this design more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant