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

Way for proper Integer array indexing? #248

Closed
HuaMuLanChina opened this issue Oct 25, 2020 · 5 comments
Closed

Way for proper Integer array indexing? #248

HuaMuLanChina opened this issue Oct 25, 2020 · 5 comments
Labels
enhancement New feature or request

Comments

@HuaMuLanChina
Copy link

Question

In numpy:

>>> array = np.ones((5,2))
>>> sel = np.array([1,2,4])
>>> array[sel].shape
(3, 2)

what i've tried:

NDArray array = manager.ones(new Shape(5,2));
long[] d = {1,2,4};
NDArray sel = manager.create(d);
array.get(sel)//error
@HuaMuLanChina HuaMuLanChina added the question Further information is requested label Oct 25, 2020
@HuaMuLanChina HuaMuLanChina changed the title Waty for proper Integer array indexing? Way for proper Integer array indexing? Oct 25, 2020
@lanking520
Copy link
Contributor

lanking520 commented Oct 26, 2020

There is a way, try to do following the javadoc: https://javadoc.io/doc/ai.djl/api/latest/ai/djl/ndarray/index/NDIndex.html

NDIndex index = new NDIndex().addIndices(0) // pick first dim
                                                      .addPickDim(sel);
array.get(index);

@HuaMuLanChina
Copy link
Author

import ai.djl.ndarray.NDArray;
import ai.djl.ndarray.NDManager;
import ai.djl.ndarray.types.Shape;
import ai.djl.ndarray.index.NDIndex;

NDManager manager = NDManager.newBaseManager();
NDArray array = manager.arange(10).reshape(-1,2);
long[] d = {1,2,4};
NDArray sel = manager.create(d);
NDIndex index = new NDIndex().addIndices(0).addPickDim(sel);
array.get(index);

can't pass block runner.

jdk.jshell.EvalException: get() currently supports all, fixed, and slices indices
	at ai.djl.ndarray.index.NDArrayIndexer.get(NDArrayIndexer.java:75)
	at ai.djl.ndarray.NDArray.get(NDArray.java:498)
	at .(#10:1)

@zachgk
Copy link
Contributor

zachgk commented Oct 27, 2020

We don't support this right now.

For clarification, there are two types of indexing with numeric indices: pick and gather. We have support for pick, but need to add gather to support this.

@zachgk zachgk added enhancement New feature or request and removed question Further information is requested labels Oct 27, 2020
@ManuLasker
Copy link

@HuaMuLanChina Hello, what you can do and it functions for me with the pytorch engine is:

NDManager manager = NDManager.newBaseManager();
NDArray array = manager.arange(10).reshape(-1,2);
long[] d = {1,2,4};
NDArray sel = manager.create(d).repeat(2).reshape(-1, 2);
NDIndex index = new NDIndex().addPickDim(sel);
array.get(index);

The results are:

ND: (3, 2) cpu() int32
[[ 2,  3],
 [ 4,  5],
 [ 8,  9],
]

The same results for numpy python:

array = np.arange(10).reshape(-1,2)
sel = np.array([1,2,4])
array[sel]
array([[2, 3],
       [4, 5],
       [8, 9]])

Hope this can help you :D

@HuaMuLanChina
Copy link
Author

'repeat' works .

Lokiiiiii pushed a commit to Lokiiiiii/djl that referenced this issue Oct 10, 2023
This has two changes. First, it adds the serving to the list of modules to
publish to maven central. The reason for this is that various types of projects
depend on code within serving: plugins, workflow operations, and web clients.

I tried to move the requested components to a new module, but found that almost
the entirety of the serving module was used in one of those three and would have
to be moved. It may be possible to split more out with greater usage of
interfaces, but I think it makes more sense to just distribute serving as a
whole.

The second change made was to create a shared publish.gradle helper similarly to
the one in the main DJL repo. As there were 3 (now four) different modules
published, I figured it was time for adding the full helper.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants