Giter Club home page Giter Club logo

Comments (12)

jerry73204 avatar jerry73204 commented on May 29, 2024 1

Overall, we have the chaining pattern tensor.i(...).i(...) and macro args pattern tensor.i(ti!(..., ..., ...)). The variable args pattern tensor.(..., ..., ...) is not possible because Rust does not support this feature. So let's see how they could be done.

Channing pattern tensor.i(...).i(...)

The accepting type can be polymorphic so it should be simple to approach. The signature of i() cannot simply return Tensor. Otherwise tensor.i(..2).i(1..) will stuck in first dimension.

It would require an intermediate type SlicedTensor, such that both Tensor::i(T) -> SliceRecord and SliceRecord::i(T) -> SliceRecord can record indexes, and finally call into() to Tensor. Hence, it would look like this:

// long
let new_tensor = tensor
    .i(3)  // 1st dim select()
    .i(2..5)  // 2nd dim narrow()
    .i(&[1, 3, 5])  // 3rd dim index_select()
    .into();  // convertion

// short
let new_tensor = tensor.i(3).i(2..5).i(&[1, 3, 5]).into();

Macro arguments pattern tensor.i(ti!(..., ..., ...))

It setups a macro ti! as a proxy, which converts user arguments into single type. Actually, I found ndarray crate ad opts this pattern. IMO, it looks more compact than chaining pattern. Unfortunately, it plays the risk of name ambiguity among crates.

let new_tensor = tensor.i(ti![3, 2..5, &[1, 3, 5]]);

from tch-rs.

LaurentMazare avatar LaurentMazare commented on May 29, 2024

That sounds like a very good idea! I looked a bit in the past at using Index and IndexMut but as they return references these are a bit hard to leverage, the syntax would be nicer but your tensor.i(...) suggestion seems reasonable too.
Handling multiple dimensions via macros sound cool too. Maybe writing tensor.i(...).i(...) would be ok to start with even if tensor.i(..., ...) would feel a bit better.
Anyway happy to get some PR for this or more suggestions and I may have a look at it when I find some time.

from tch-rs.

LaurentMazare avatar LaurentMazare commented on May 29, 2024

Thanks for all the details!
One thing I'm not sure to understand is why would we need a ti! macro ? I get that we cannot do tensor.i(..., ..., ...) but what about tensor.i((..., ..., ...)) supporting tuples say up to length 4 or 5. If this was possible maybe this could avoid needing to introduce SlicedTensor and the whole chaining pattern with the final into conversion but I'm probably missing something here.

from tch-rs.

jerry73204 avatar jerry73204 commented on May 29, 2024

Suppose it accepts up to five parameters in a tuple tensor.i((A, B, C, D, E)). That means you implement a tuple of combinations of Range*, scalar and &[integer] types up to fifth power. It seems impractical to enumerate all functions signatures. So I'm thinking fo workaround for this.

from tch-rs.

LaurentMazare avatar LaurentMazare commented on May 29, 2024

Couldn't traits be used to avoid the combinatorial explosion ? E.g. something like the following pseudo-code and then i is a generic function that would work for any types implementing Indexer and would call to_index and do the dispatch according to the returned variant. There are certainly added difficulties that I haven't seen, maybe I should try implementing it to see what the problems would be.

enum Indexer {
    Range(...),
    Scalar(...),
    Slice(...),
}

trait Indexer {
    fn to_index(&self) -> Indexer;
}

impl Indexer for usize {
    fn to_index(&self) -> Indexer {
        Scalar(self)
    }
}

from tch-rs.

jerry73204 avatar jerry73204 commented on May 29, 2024

Nice idea. I ever wondered this though but stucked at gathering them into one type.

Here is a proof-of-concept on my fork. I left the tensor-as-index part blank until understanding how NumPy-style indexing actually does this.
https://github.com/jerry73204/tch-rs/blob/tensor_indexer/src/tensor/index.rs

from tch-rs.

jerry73204 avatar jerry73204 commented on May 29, 2024

I can work on it occasionally because of errands these days.
@LaurentMazare if you already work it out, it's fine to pick the code from above comment.

from tch-rs.

LaurentMazare avatar LaurentMazare commented on May 29, 2024

Looks pretty nice. I haven't done anything on it yet so very happy if you can push it further and make a PR out of it. If you don't have the time I'll have a look at it on my side but there is no rush at all.

from tch-rs.

jerry73204 avatar jerry73204 commented on May 29, 2024

I studied numpy's manual on indexing. It seems a bunch of work there. The state the tldr, there are several distinct indexing scenarios handled differently. So I decided to make it simple and implement only basic indexing in #60 . Please take a look.

from tch-rs.

LaurentMazare avatar LaurentMazare commented on May 29, 2024

Fwiw, I've added some documentation + used the i(...) operator in a couple examples. Overall I find it very neat to use.

from tch-rs.

LaurentMazare avatar LaurentMazare commented on May 29, 2024

Closing this as I think the current state is quite nice. Feel free to repoen if you want to discuss this more.

from tch-rs.

dbsxdbsx avatar dbsxdbsx commented on May 29, 2024

Fwiw, I've added some documentation + used the i(...) operator in a couple examples. Overall I find it very neat to use.

Now I wonder how to do it, there is no i(...) found in version 0.7.0.

from tch-rs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.