Get an arbitrary slice of a Nd4j array

北战南征 提交于 2020-01-14 14:26:11

问题


I want to perform slicing in Nd4j of arbitrary sizes in the same manner as I am able to do using Numpy.

a = numpy.arange(100)
a[25:50]

The nd4j slice method only takes dimension and index arguments, not length. How can I achieve this?


回答1:


I know it's an old question but I ran across it while googling this problem exactly.

By inspecting the source code for slice I believe it can only return full rows/columns not partial from index to index. You can use method get with an NDArrayIndex instance s argument for that. This code for example is a translation of your numpy code.

import org.nd4j.linalg.api.ndarray.INDArray;
import static org.nd4j.linalg.factory.Nd4j.linspace;
import static org.nd4j.linalg.indexing.NDArrayIndex.interval;

class SliceExample {
    public static void main(String[] args) {
        INDArray a = linspace(0, 99, 100);    // up to 99 inclusive
        INDArray s = a.get(interval(25, 50)); // up to 50th non inclusive
    }
}

NDArrayIndex documentation



来源:https://stackoverflow.com/questions/36973444/get-an-arbitrary-slice-of-a-nd4j-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!