Java Code Examples for org.nd4j.linalg.factory.Nd4j#arange()

The following examples show how to use org.nd4j.linalg.factory.Nd4j#arange() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TimeSeriesGenerator.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
public Pair<INDArray, INDArray> next(int index) {
    INDArray rows;
    if (shuffle) {
        rows = Nd4j.getRandom().nextInt(endIndex, new int[] {batchSize});
        rows.addi(startIndex);
    } else {
        int i = startIndex + batchSize + stride * index;
        // TODO: add stride arg to arange
        rows = Nd4j.arange(i, Math.min(i + batchSize * stride, endIndex + 1));
    }
    INDArray samples = Nd4j.create(rows.length(), length / samplingRate, data.columns());
    INDArray targets = Nd4j.create(rows.length(), this.targets.columns());

    for (int j = 0; j < rows.rows(); j++) {
        long idx = (long) rows.getDouble(j);
        INDArrayIndex indices = NDArrayIndex.interval(idx - this.length, this.samplingRate, idx);
        samples.putSlice(j, this.data.get(indices));
        INDArrayIndex point = NDArrayIndex.point((long) rows.getDouble(j));
        targets.putSlice(j, this.targets.get(point));
    }
    if (reverse)
        samples = Nd4j.reverse(samples);

    return new Pair<>(samples, targets);
}
 
Example 2
Source File: TestWritablesToNDArrayFunction.java    From DataVec with Apache License 2.0 5 votes vote down vote up
@Test
public void testWritablesToNDArrayAllScalars() throws Exception {
    List<Writable> l = new ArrayList<>();
    for (int i = 0; i < 5; i++)
        l.add(new IntWritable(i));
    INDArray expected = Nd4j.arange(5);
    assertEquals(expected, new WritablesToNDArrayFunction().apply(l));
}
 
Example 3
Source File: TestWritablesToNDArrayFunction.java    From DataVec with Apache License 2.0 5 votes vote down vote up
@Test
public void testWritablesToNDArrayMixed() throws Exception {
    List<Writable> l = new ArrayList<>();
    l.add(new IntWritable(0));
    l.add(new IntWritable(1));
    INDArray arr = Nd4j.arange(2, 5);
    l.add(new NDArrayWritable(arr));
    l.add(new IntWritable(5));
    arr = Nd4j.arange(6, 9);
    l.add(new NDArrayWritable(arr));
    l.add(new IntWritable(9));

    INDArray expected = Nd4j.arange(10);
    assertEquals(expected, new WritablesToNDArrayFunction().apply(l));
}
 
Example 4
Source File: TestNDArrayToWritablesFunction.java    From DataVec with Apache License 2.0 5 votes vote down vote up
@Test
public void testNDArrayToWritablesScalars() throws Exception {
    INDArray arr = Nd4j.arange(5);
    List<Writable> expected = new ArrayList<>();
    for (int i = 0; i < 5; i++)
        expected.add(new DoubleWritable(i));
    List<Writable> actual = new NDArrayToWritablesFunction().apply(arr);
    assertEquals(expected, actual);
}
 
Example 5
Source File: TestNDArrayToWritablesFunction.java    From DataVec with Apache License 2.0 5 votes vote down vote up
@Test
public void testNDArrayToWritablesArray() throws Exception {
    INDArray arr = Nd4j.arange(5);
    List<Writable> expected = Arrays.asList((Writable) new NDArrayWritable(arr));
    List<Writable> actual = new NDArrayToWritablesFunction(true).apply(arr);
    assertEquals(expected, actual);
}
 
Example 6
Source File: TestWritablesToNDArrayFunction.java    From DataVec with Apache License 2.0 5 votes vote down vote up
@Test
public void testWritablesToNDArrayAllScalars() throws Exception {
    List<Writable> l = new ArrayList<>();
    for (int i = 0; i < 5; i++)
        l.add(new IntWritable(i));
    INDArray expected = Nd4j.arange(5);
    assertEquals(expected, new WritablesToNDArrayFunction().call(l));
}
 
Example 7
Source File: TestWritablesToNDArrayFunction.java    From DataVec with Apache License 2.0 5 votes vote down vote up
@Test
public void testWritablesToNDArrayMixed() throws Exception {
    List<Writable> l = new ArrayList<>();
    l.add(new IntWritable(0));
    l.add(new IntWritable(1));
    INDArray arr = Nd4j.arange(2, 5);
    l.add(new NDArrayWritable(arr));
    l.add(new IntWritable(5));
    arr = Nd4j.arange(6, 9);
    l.add(new NDArrayWritable(arr));
    l.add(new IntWritable(9));

    INDArray expected = Nd4j.arange(10);
    assertEquals(expected, new WritablesToNDArrayFunction().call(l));
}
 
Example 8
Source File: TestNDArrayToWritablesFunction.java    From DataVec with Apache License 2.0 5 votes vote down vote up
@Test
public void testNDArrayToWritablesScalars() throws Exception {
    INDArray arr = Nd4j.arange(5);
    List<Writable> expected = new ArrayList<>();
    for (int i = 0; i < 5; i++)
        expected.add(new DoubleWritable(i));
    List<Writable> actual = new NDArrayToWritablesFunction().call(arr);
    assertEquals(expected, actual);
}
 
Example 9
Source File: TestNDArrayToWritablesFunction.java    From DataVec with Apache License 2.0 5 votes vote down vote up
@Test
public void testNDArrayToWritablesArray() throws Exception {
    INDArray arr = Nd4j.arange(5);
    List<Writable> expected = Arrays.asList((Writable) new NDArrayWritable(arr));
    List<Writable> actual = new NDArrayToWritablesFunction(true).call(arr);
    assertEquals(expected, actual);
}
 
Example 10
Source File: TestNDArrayToWritablesFunction.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testNDArrayToWritablesScalars() throws Exception {
    INDArray arr = Nd4j.arange(5);
    List<Writable> expected = new ArrayList<>();
    for (int i = 0; i < 5; i++)
        expected.add(new DoubleWritable(i));
    List<Writable> actual = new NDArrayToWritablesFunction().apply(arr);
    assertEquals(expected, actual);
}
 
Example 11
Source File: TestNDArrayToWritablesFunction.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testNDArrayToWritablesArray() throws Exception {
    INDArray arr = Nd4j.arange(5);
    List<Writable> expected = Arrays.asList((Writable) new NDArrayWritable(arr));
    List<Writable> actual = new NDArrayToWritablesFunction(true).apply(arr);
    assertEquals(expected, actual);
}
 
Example 12
Source File: TestWritablesToNDArrayFunction.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testWritablesToNDArrayMixed() throws Exception {
    List<Writable> l = new ArrayList<>();
    l.add(new IntWritable(0));
    l.add(new IntWritable(1));
    INDArray arr = Nd4j.arange(2, 5);
    l.add(new NDArrayWritable(arr));
    l.add(new IntWritable(5));
    arr = Nd4j.arange(6, 9);
    l.add(new NDArrayWritable(arr));
    l.add(new IntWritable(9));

    INDArray expected = Nd4j.arange(10).castTo(DataType.FLOAT).reshape(1,10);
    assertEquals(expected, new WritablesToNDArrayFunction().call(l));
}
 
Example 13
Source File: TestNDArrayToWritablesFunction.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testNDArrayToWritablesScalars() throws Exception {
    INDArray arr = Nd4j.arange(5);
    List<Writable> expected = new ArrayList<>();
    for (int i = 0; i < 5; i++)
        expected.add(new DoubleWritable(i));
    List<Writable> actual = new NDArrayToWritablesFunction().call(arr);
    assertEquals(expected, actual);
}
 
Example 14
Source File: TestNDArrayToWritablesFunction.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testNDArrayToWritablesArray() throws Exception {
    INDArray arr = Nd4j.arange(5);
    List<Writable> expected = Arrays.asList((Writable) new NDArrayWritable(arr));
    List<Writable> actual = new NDArrayToWritablesFunction(true).call(arr);
    assertEquals(expected, actual);
}