etaler / Etaler

A flexable HTM (Hierarchical Temporal Memory) framework with full GPU support.
BSD 3-Clause "New" or "Revised" License
89 stars 14 forks source link

Providing operator [] for et::Tensor (or at least a better way to handle tensor subscription) #71

Closed marty1885 closed 4 years ago

marty1885 commented 5 years ago

Numpy's way of doing ND array subscription is widely accepted and straight forward.

ex:

>>> a = np.zeros(4,4)
>>> a[:2, :2]

Due to the fact that C++'s operator [] can only take one argument. I ended up implementing a view() method to perform subscription.

ex:

[cling]$ auto a = ones({4,4})
[cling]$ a.view({range(2), range(2)})

But TBH, it gets annoying when I start to write more code with it. It would be great to have a [] equivalent in Etaler. We have a few solution.

  1. make Tensor Tensor::operator [] (svector<Range>)

Which will allow us to do

[cling]$ auto a = ones({4,4})
[cling]$ a[{range(2), range(2)})]

There is an extra bracket around the parameters.

  1. make Tensor Tensor::operator [] (svector<Range>) and overload Range::operator , ()

Which allows us to

[cling]$ auto a = ones({4,4})
[cling]$ a[range(2), range(2))]

It gives us the syntax we want. But messes with how C++ evaluates values.

  1. Use operator ()

This is ArrayFire's solution. It feels weird to subscript using () instead of [].

[cling]$ auto a = ones({4,4})
[cling]$ a(range(2), range(2)))

It feels like calling a function....

Any ideas?

alior101 commented 5 years ago

I'm voting for option 1 :)

marty1885 commented 5 years ago

@alior101 should be implemented now :)