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

The following examples show how to use org.nd4j.linalg.factory.Nd4j#copy() . 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: DeepGL.java    From ml-models with Apache License 2.0 6 votes vote down vote up
private void diffuse(List<Pruning.Feature> featuresList) {
    INDArray ndDiffused = Nd4j.create(embedding.shape());
    Nd4j.copy(embedding, ndDiffused);

    featuresList.addAll(featuresList);
    features = featuresList.toArray(new Pruning.Feature[0]);

    for (int i = features.length / 2; i < features.length; i++) {
        features[i] = new Pruning.Feature("diffuse", features[i]);
    }

    for (int diffIteration = 0; diffIteration < diffusionIterations; diffIteration++) {
        INDArray ndDiffusedTemp = Nd4j.create(embedding.shape());
        nodeQueue.set(0);
        final ArrayList<Future<?>> futures = new ArrayList<>();
        for (int i = 0; i < concurrency; i++) {
            futures.add(executorService.submit(new DiffusionTask(ndDiffused, ndDiffusedTemp)));
        }
        ParallelUtil.awaitTermination(futures);
        ndDiffused = ndDiffusedTemp;
    }
    embedding = Nd4j.concat(1, embedding, ndDiffused);
}
 
Example 2
Source File: PruningTest.java    From ml-models with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetCols() {

    INDArray origEmbedding = Nd4j.create(new double[][]{
            {0.00, 1.00, 0.00},
            {0.00, 0.00, 1.00},
            {0.00, 1.00, 1.00},
            {0.00, 2.00, 2.00},
            {1.00, 0.00, 0.00},
            {1.00, 0.00, 0.00},
            {2.00, 0.00, 0.00},
    });

    int[] featIdsToKeep = {2, 1, 0};

    INDArray ndPrunedEmbedding = Nd4j.create(origEmbedding.shape());
    Nd4j.copy(origEmbedding, ndPrunedEmbedding);
    INDArray columns = ndPrunedEmbedding.getColumns(featIdsToKeep);
    System.out.println("columns = \n" + columns);
}
 
Example 3
Source File: Gan4Exemple.java    From dl4j-tutorials with MIT License 6 votes vote down vote up
private static void discCp2GenParam() {
    Map<String, INDArray> paramTableGen = genNet.paramTable();

    //恢复数据
    Set<String> genKeySet = paramTableGen.keySet();
    for (Iterator<String> iter = genKeySet.iterator(); iter.hasNext();) {
        String key = iter.next();
        String[] keys = key.split("_");
        Integer keyIdx = Integer.parseInt(keys[0]);
        if (keyIdx >= startIdx) {
            String discKey = (keyIdx - startIdx) + "_" + keys[1];
            INDArray item = discNet.getParam(discKey);
            Nd4j.copy(item, genNet.getParam(key));
        }
    }
}
 
Example 4
Source File: CudaExecutionerTest.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testSoftmax1D_1() throws Exception {
    INDArray input1T = Nd4j.create(new double[]{ -0.75, 0.58, 0.42, 1.03, -0.61, 0.19, -0.37, -0.40, -1.42, -0.04});
    INDArray input1 = Nd4j.create(new double[]{ -0.75, 0.58, 0.42, 1.03, -0.61, 0.19, -0.37, -0.40, -1.42, -0.04});
    INDArray input2 = Nd4j.zerosLike(input1);
    Nd4j.copy(input1, input2);
    INDArray output1 = Nd4j.create(1, 10);
    INDArray output1T = Nd4j.create(1, 10);

    System.out.println("FA --------------------");
    Nd4j.getExecutioner().exec(new OldSoftMax(input1, output1));
    Nd4j.getExecutioner().exec(new OldSoftMax(input1T, output1T));
    System.out.println("FB --------------------");

    System.out.println("Softmax = " + output1);
    INDArray output2 = Nd4j.create(1,10);
    Nd4j.getExecutioner().exec(new SoftMaxDerivative(input2, output2));
    System.out.println("Softmax Derivative = " + output2);

    INDArray assertion1 = Nd4j.create(new double[]{0.04, 0.16, 0.14, 0.26, 0.05, 0.11, 0.06, 0.06, 0.02, 0.09});

    assertArrayEquals(assertion1.data().asFloat(), output1.data().asFloat(), 0.01f);
    assertArrayEquals(assertion1.data().asFloat(), output1T.data().asFloat(), 0.01f);

}
 
Example 5
Source File: CudaTransformsTests.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testSoftmax1D_1() throws Exception {
    INDArray input1T = Nd4j.create(new double[]{ -0.75, 0.58, 0.42, 1.03, -0.61, 0.19, -0.37, -0.40, -1.42, -0.04}).transpose();
    INDArray input1 = Nd4j.create(new double[]{ -0.75, 0.58, 0.42, 1.03, -0.61, 0.19, -0.37, -0.40, -1.42, -0.04});
    INDArray input2 = Nd4j.zerosLike(input1);
    Nd4j.copy(input1, input2);
    INDArray output1 = Nd4j.create(1, 10);
    INDArray output1T = Nd4j.create(1, 10);

    System.out.println("FA --------------------");
    Nd4j.getExecutioner().exec(new OldSoftMax(input1, output1));
    Nd4j.getExecutioner().exec(new OldSoftMax(input1T, output1T));
    System.out.println("FB --------------------");

    System.out.println("Softmax = " + output1);
    INDArray output2 = Nd4j.create(1,10);
    Nd4j.getExecutioner().exec(new SoftMaxDerivative(input2, output2));
    System.out.println("Softmax Derivative = " + output2);

    INDArray assertion1 = Nd4j.create(new double[]{0.04, 0.16, 0.14, 0.26, 0.05, 0.11, 0.06, 0.06, 0.02, 0.09});

    assertArrayEquals(assertion1.data().asFloat(), output1.data().asFloat(), 0.01f);
    assertArrayEquals(assertion1.data().asFloat(), output1T.data().asFloat(), 0.01f);

}
 
Example 6
Source File: NativeOpExecutionerTest.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testSoftmax1D_1() throws Exception {
    INDArray input1T = Nd4j.create(new double[]{ -0.75, 0.58, 0.42, 1.03, -0.61, 0.19, -0.37, -0.40, -1.42, -0.04});
    INDArray input1 = Nd4j.create(new double[]{ -0.75, 0.58, 0.42, 1.03, -0.61, 0.19, -0.37, -0.40, -1.42, -0.04});
    INDArray input2 = Nd4j.zerosLike(input1);
    Nd4j.copy(input1, input2);
    INDArray output1 = Nd4j.create(1, 10);
    INDArray output1T = Nd4j.create(1, 10);

    System.out.println("FA --------------------");
    Nd4j.getExecutioner().exec(new OldSoftMax(input1, output1));
    Nd4j.getExecutioner().exec(new OldSoftMax(input1T, output1T));
    System.out.println("FB --------------------");

    System.out.println("Softmax = " + output1);
    INDArray output2 = Nd4j.create(1,10);
    Nd4j.getExecutioner().exec(new SoftMaxDerivative(input2, output2));
    System.out.println("Softmax Derivative = " + output2);

    INDArray assertion1 = Nd4j.create(new double[]{0.04, 0.16, 0.14, 0.26, 0.05, 0.11, 0.06, 0.06, 0.02, 0.09});

    assertArrayEquals(assertion1.data().asFloat(), output1.data().asFloat(), 0.01f);
    assertArrayEquals(assertion1.data().asFloat(), output1T.data().asFloat(), 0.01f);

}
 
Example 7
Source File: LapackTest.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testQRSquare() {
    INDArray A = Nd4j.create(new double[] {1, 2, 3, 4, 5, 6, 7, 8, 9});
    A = A.reshape('c', 3, 3);
    INDArray O = Nd4j.create(A.shape());
    Nd4j.copy(A, O);
    INDArray R = Nd4j.create(A.columns(), A.columns());

    Nd4j.getBlasWrapper().lapack().geqrf(A, R);

    A.mmuli(R);
    O.subi(A);
    DataBuffer db = O.data();
    for (int i = 0; i < db.length(); i++) {
        assertEquals(0, db.getFloat(i), 1e-5);
    }
}
 
Example 8
Source File: LapackTest.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testQRRect() {
    INDArray A = Nd4j.create(new double[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12});
    A = A.reshape('f', 4, 3);
    INDArray O = Nd4j.create(A.shape());
    Nd4j.copy(A, O);

    INDArray R = Nd4j.create(A.columns(), A.columns());
    Nd4j.getBlasWrapper().lapack().geqrf(A, R);

    A.mmuli(R);
    O.subi(A);
    DataBuffer db = O.data();
    for (int i = 0; i < db.length(); i++) {
        assertEquals(0, db.getFloat(i), 1e-5);
    }
}
 
Example 9
Source File: LapackTest.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testCholeskyL() {
    INDArray A = Nd4j.create(new double[] {2, -1, 1, -1, 2, -1, 1, -1, 2,});
    A = A.reshape('c', 3, 3);
    INDArray O = Nd4j.create(A.shape());
    Nd4j.copy(A, O);

    Nd4j.getBlasWrapper().lapack().potrf(A, true);

    A.mmuli(A.transpose());
    O.subi(A);
    DataBuffer db = O.data();
    for (int i = 0; i < db.length(); i++) {
        assertEquals(0, db.getFloat(i), 1e-5);
    }
}
 
Example 10
Source File: LapackTest.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testQRSquare() {
    INDArray A = Nd4j.create(new double[] {1, 2, 3, 4, 5, 6, 7, 8, 9});
    A = A.reshape('c', 3, 3);
    INDArray O = Nd4j.create(A.dataType(), A.shape());
    Nd4j.copy(A, O);
    INDArray R = Nd4j.create(A.dataType(), A.columns(), A.columns());

    Nd4j.getBlasWrapper().lapack().geqrf(A, R);

    A.mmuli(R);
    O.subi(A);
    DataBuffer db = O.data();
    for (int i = 0; i < db.length(); i++) {
        assertEquals(0, db.getFloat(i), 1e-5);
    }
}
 
Example 11
Source File: LapackTest.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testQRRect() {
    INDArray A = Nd4j.create(new double[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12});
    A = A.reshape('f', 4, 3);
    INDArray O = Nd4j.create(A.dataType(), A.shape());
    Nd4j.copy(A, O);

    INDArray R = Nd4j.create(A.dataType(), A.columns(), A.columns());
    Nd4j.getBlasWrapper().lapack().geqrf(A, R);

    A.mmuli(R);
    O.subi(A);
    DataBuffer db = O.data();
    for (int i = 0; i < db.length(); i++) {
        assertEquals(0, db.getFloat(i), 1e-5);
    }
}
 
Example 12
Source File: LapackTest.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testCholeskyL() {
    INDArray A = Nd4j.create(new double[] {2, -1, 1, -1, 2, -1, 1, -1, 2,});
    A = A.reshape('c', 3, 3);
    INDArray O = Nd4j.create(A.dataType(), A.shape());
    Nd4j.copy(A, O);

    Nd4j.getBlasWrapper().lapack().potrf(A, true);

    A.mmuli(A.transpose());
    O.subi(A);
    DataBuffer db = O.data();
    for (int i = 0; i < db.length(); i++) {
        assertEquals(0, db.getFloat(i), 1e-5);
    }
}
 
Example 13
Source File: CudaTransformsTests.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testSoftmax2D_T() {
    INDArray input1 = Nd4j.create(1000).transpose();
    INDArray input2 = Nd4j.zerosLike(input1);
    Nd4j.copy(input1, input2);
    INDArray output1 = Nd4j.create(1, 1000);
    Nd4j.getExecutioner().exec(new OldSoftMax(input1, output1));
    System.out.println("Softmax = " + output1);
    INDArray output2 = Nd4j.create(1,1000);
    Nd4j.getExecutioner().exec(new SoftMaxDerivative(input2, output2));
    System.out.println("Softmax Derivative = " + output2);

    assertEquals(1.0f, output1.sumNumber().floatValue(), 0.01f);
    assertEquals(1.0f, output2.sumNumber().floatValue(), 0.01f);
}
 
Example 14
Source File: LapackTest.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testCholeskyU() {
    INDArray A = Nd4j.create(new double[] {2, -1, 2, -1, 2, -1, 2, -1, 2,});
    A = A.reshape('f', 3, 3);
    INDArray O = Nd4j.create(A.shape());
    Nd4j.copy(A, O);

    Nd4j.getBlasWrapper().lapack().potrf(A, false);
    A = A.transpose().mmul(A);
    O.subi(A);
    DataBuffer db = O.data();
    for (int i = 0; i < db.length(); i++) {
        assertEquals(0, db.getFloat(i), 1e-5);
    }
}
 
Example 15
Source File: LapackTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testCholeskyU() {
    INDArray A = Nd4j.create(new double[] {3, -1, 2, -1, 3, -1, 2, -1, 3,});
    A = A.reshape('f', 3, 3);
    INDArray O = Nd4j.create(A.dataType(), A.shape());
    Nd4j.copy(A, O);

    Nd4j.getBlasWrapper().lapack().potrf(A, false);
    A = A.transpose().mmul(A);
    O.subi(A);
    DataBuffer db = O.data();
    for (int i = 0; i < db.length(); i++) {
        assertEquals(0, db.getFloat(i), 1e-5);
    }
}
 
Example 16
Source File: Pruning.java    From ml-models with Apache License 2.0 4 votes vote down vote up
private INDArray pruneEmbedding(INDArray origEmbedding, int... featIdsToKeep) {
    INDArray ndPrunedEmbedding = Nd4j.create(origEmbedding.shape());
    Nd4j.copy(origEmbedding, ndPrunedEmbedding);
    return ndPrunedEmbedding.getColumns(featIdsToKeep);
}