Java Code Examples for org.nd4j.linalg.api.ndarray.INDArray#std()

The following examples show how to use org.nd4j.linalg.api.ndarray.INDArray#std() . 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: PreProcessor3D4DTest.java    From nd4j with Apache License 2.0 6 votes vote down vote up
public Construct4dDataSet(int nExamples, int nChannels, int height, int width) {

            INDArray allImages = Nd4j.rand(new int[] {nExamples, nChannels, height, width});
            allImages.get(NDArrayIndex.all(), NDArrayIndex.point(1), NDArrayIndex.all(), NDArrayIndex.all()).muli(100)
                            .addi(200);
            allImages.get(NDArrayIndex.all(), NDArrayIndex.point(2), NDArrayIndex.all(), NDArrayIndex.all()).muli(0.001)
                            .subi(10);

            INDArray labels = Nd4j.linspace(1, nChannels, nChannels).reshape(nChannels, 1);
            sampleDataSet = new DataSet(allImages, labels);

            expectedMean = allImages.mean(0, 2, 3);
            expectedStd = allImages.std(0, 2, 3);

            expectedLabelMean = labels.mean(0);
            expectedLabelStd = labels.std(0);

            expectedMin = allImages.min(0, 2, 3);
            expectedMax = allImages.max(0, 2, 3);
        }
 
Example 2
Source File: TransformOpValidation.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testStandardizeNoDeviation() {
    final INDArray random = Nd4j.rand(new int[]{10, 4});
    for (int i = 0; i < 4; i++) {
        random.putScalar(1, i, 7);
    }

    final int[] axis = new int[]{1};
    final INDArray means = random.mean(axis);
    final INDArray std = random.std(false, axis);
    std.addi(std.eq(0).castTo(DataType.DOUBLE));

    final INDArray res = random.subColumnVector(means).divColumnVector(std);
    final INDArray expOut = res.norm1();

    SameDiff sd = SameDiff.create();
    SDVariable sdA = sd.var("a", random);
    SDVariable t = sd.math.standardize(sdA, axis);
    t.norm1("out");

    String err = OpValidation.validate(new TestCase(sd)
            .expectedOutput("out", expOut)
            .gradientCheck(true));
    assertNull(err, err);
}
 
Example 3
Source File: TransformOpValidation.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testStandardize() {
    final INDArray random = Nd4j.rand(new int[]{10, 4});

    final int[] axis = new int[]{1};
    final INDArray means = random.mean(axis);
    final INDArray std = random.std(false, axis);
    final INDArray res = random.subColumnVector(means).divColumnVector(std);
    final INDArray expOut = res.norm1();

    SameDiff sd = SameDiff.create();
    SDVariable sdA = sd.var("a", random);
    SDVariable t = sd.math.standardize(sdA, axis);
    t.norm1("out");

    String err = OpValidation.validate(new TestCase(sd)
            .expectedOutput("out", expOut)
            .gradientCheck(true));
    assertNull(err, err);
}
 
Example 4
Source File: TestAnalyzeLocal.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testAnalysisBasic() throws Exception {

    RecordReader rr = new CSVRecordReader();
    rr.initialize(new FileSplit(new ClassPathResource("iris.txt").getFile()));

    Schema s = new Schema.Builder()
            .addColumnsDouble("0", "1", "2", "3")
            .addColumnInteger("label")
            .build();

    DataAnalysis da = AnalyzeLocal.analyze(s, rr);

    System.out.println(da);

    //Compare:
    List<List<Writable>> list = new ArrayList<>();
    rr.reset();
    while(rr.hasNext()){
        list.add(rr.next());
    }

    INDArray arr = RecordConverter.toMatrix(DataType.DOUBLE, list);
    INDArray mean = arr.mean(0);
    INDArray std = arr.std(0);

    for( int i=0; i<5; i++ ){
        double m = ((NumericalColumnAnalysis)da.getColumnAnalysis().get(i)).getMean();
        double stddev = ((NumericalColumnAnalysis)da.getColumnAnalysis().get(i)).getSampleStdev();
        assertEquals(mean.getDouble(i), m, 1e-3);
        assertEquals(std.getDouble(i), stddev, 1e-3);
    }

}
 
Example 5
Source File: Transforms.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Normalize data to zero mean and unit variance
 * substract by the mean and divide by the standard deviation
 *
 * @param toNormalize the ndarray to normalize
 * @return the normalized ndarray
 */
public static INDArray normalizeZeroMeanAndUnitVariance(INDArray toNormalize) {
    INDArray columnMeans = toNormalize.mean(0);
    INDArray columnStds = toNormalize.std(0);

    toNormalize.subiRowVector(columnMeans);
    //padding for non zero
    columnStds.addi(Nd4j.EPS_THRESHOLD);
    toNormalize.diviRowVector(columnStds);
    return toNormalize;
}
 
Example 6
Source File: ShapeTestsC.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testRowStd() {
    INDArray twoByThree = Nd4j.linspace(1, 4, 4, DataType.DOUBLE).reshape(2, 2);
    INDArray rowStd = twoByThree.std(1);
    INDArray assertion = Nd4j.create(new double[] {0.7071067811865476f, 0.7071067811865476f});
    assertEquals(getFailureMessage(), assertion, rowStd);

}
 
Example 7
Source File: BatchNormalizationTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testCnnForwardPass() {
    int nOut = 10;
    Layer l = getLayer(nOut, 0.0, false, -1, -1);
    assertEquals(4 * nOut, l.numParams()); //Gamma, beta, global mean, global var
    int hw = 15;

    Nd4j.getRandom().setSeed(12345);
    INDArray randInput = Nd4j.rand(new int[]{100, nOut, hw, hw});
    INDArray output = l.activate(randInput, true, LayerWorkspaceMgr.noWorkspaces());

    assertEquals(4, output.rank());

    INDArray mean = output.mean(0, 2, 3);
    INDArray stdev = output.std(false, 0, 2, 3);

    assertArrayEquals(new float[nOut], mean.data().asFloat(), 1e-6f);
    assertArrayEquals(Nd4j.ones(1, nOut).data().asFloat(), stdev.data().asFloat(), 1e-6f);

    //If we fix gamma/beta: expect different mean and variance...
    double gamma = 2.0;
    double beta = 3.0;
    l = getLayer(nOut, 0.0, true, gamma, beta);
    assertEquals(2 * nOut, l.numParams()); //Should have only global mean/var parameters
    output = l.activate(randInput, true, LayerWorkspaceMgr.noWorkspaces());
    mean = output.mean(0, 2, 3);
    stdev = output.std(false, 0, 2, 3);

    assertEquals(Nd4j.valueArrayOf(mean.shape(), beta), mean);
    assertEquals(Nd4j.valueArrayOf(stdev.shape(), gamma), stdev);
}
 
Example 8
Source File: StandardScalerTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Ignore
@Test
public void testScale() {
    StandardScaler scaler = new StandardScaler();
    DataSetIterator iter = new IrisDataSetIterator(10, 150);
    scaler.fit(iter);
    INDArray featureMatrix = new IrisDataSetIterator(150, 150).next().getFeatures();
    INDArray mean = featureMatrix.mean(0);
    INDArray std = featureMatrix.std(0);
    System.out.println(mean);
}
 
Example 9
Source File: TransformOpValidation.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testStandardizeOP() {
    final INDArray random = Nd4j.rand(new int[]{10, 4});

    final int[] axis = new int[]{1};
    final INDArray means = random.mean(axis);
    final INDArray std = random.std(false, axis);
    final INDArray res = random.subColumnVector(means).divColumnVector(std);

    final INDArray output = Nd4j.zerosLike(res);
    Nd4j.getExecutioner().exec(new Standardize(random, output, 1));

    assertEquals(res, output);
}
 
Example 10
Source File: BatchNormalizationTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
    public void testDnnForwardPass() {
        int nOut = 10;
        Layer l = getLayer(nOut, 0.0, false, -1, -1);
        assertEquals(4 * nOut, l.numParams()); //Gamma, beta, global mean, global var

        INDArray randInput = Nd4j.rand(100, nOut);
        INDArray output = l.activate(randInput, true, LayerWorkspaceMgr.noWorkspaces());

        INDArray mean = output.mean(0);
        INDArray stdev = output.std(false, 0);

//        System.out.println(Arrays.toString(mean.data().asFloat()));

        assertArrayEquals(new float[nOut], mean.data().asFloat(), 1e-6f);
        assertEquals(Nd4j.ones(nOut), stdev);

        //If we fix gamma/beta: expect different mean and variance...
        double gamma = 2.0;
        double beta = 3.0;
        l = getLayer(nOut, 0.0, true, gamma, beta);
        assertEquals(2 * nOut, l.numParams()); //Should have only global mean/var parameters
        output = l.activate(randInput, true, LayerWorkspaceMgr.noWorkspaces());
        mean = output.mean(0);
        stdev = output.std(false, 0);

        assertEquals(Nd4j.valueArrayOf(mean.shape(), beta), mean);
        assertEquals(Nd4j.valueArrayOf(stdev.shape(), gamma), stdev);
    }
 
Example 11
Source File: FeatureUtil.java    From nd4j with Apache License 2.0 5 votes vote down vote up
public static void normalizeMatrix(INDArray toNormalize) {
    INDArray columnMeans = toNormalize.mean(0);
    toNormalize.subiRowVector(columnMeans);
    INDArray std = toNormalize.std(0);
    std.addi(Nd4j.scalar(1e-12));
    toNormalize.diviRowVector(std);
}
 
Example 12
Source File: FeatureUtil.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public static void normalizeMatrix(INDArray toNormalize) {
    INDArray columnMeans = toNormalize.mean(0);
    toNormalize.subiRowVector(columnMeans);
    INDArray std = toNormalize.std(0);
    std.addi(Nd4j.scalar(1e-12));
    toNormalize.diviRowVector(std);
}
 
Example 13
Source File: ShapeTestsC.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testRowStd() {
    INDArray twoByThree = Nd4j.linspace(1, 4, 4).reshape(2, 2);
    INDArray rowStd = twoByThree.std(1);
    INDArray assertion = Nd4j.create(new float[] {0.7071067811865476f, 0.7071067811865476f});
    assertEquals(getFailureMessage(), assertion, rowStd);

}
 
Example 14
Source File: OpExecutionerTestsC.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testColumnStd() {
    Nd4j.MAX_ELEMENTS_PER_SLICE = Integer.MAX_VALUE;
    Nd4j.MAX_SLICES_TO_PRINT = Integer.MAX_VALUE;
    INDArray twoByThree = Nd4j.linspace(1, 600, 600).reshape(150, 4);
    INDArray columnStd = twoByThree.std(0);
    INDArray assertion = Nd4j.create(new float[] {173.78147196982766f, 173.78147196982766f, 173.78147196982766f,
                    173.78147196982766f});
    assertEquals(assertion, columnStd);
}
 
Example 15
Source File: StandardScalerTest.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Ignore
@Test
public void testScale() {
    StandardScaler scaler = new StandardScaler();
    DataSetIterator iter = new IrisDataSetIterator(10, 150);
    scaler.fit(iter);
    INDArray featureMatrix = new IrisDataSetIterator(150, 150).next().getFeatureMatrix();
    INDArray mean = featureMatrix.mean(0);
    INDArray std = featureMatrix.std(0);
    System.out.println(mean);
}
 
Example 16
Source File: CudaAccumTests.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testStdev1(){
    double[][] ind = {{5.1, 3.5, 1.4}, {4.9, 3.0, 1.4}, {4.7, 3.2, 1.3}};
    INDArray in = Nd4j.create(ind);
    INDArray stdev = in.std(1);

    INDArray exp = Nd4j.create(new double[]{1.8556220880, 1.7521415468, 1.7039170559});

    assertEquals(exp,stdev);
}
 
Example 17
Source File: CudaAccumTests.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testStdev0(){
    double[][] ind = {{5.1, 3.5, 1.4}, {4.9, 3.0, 1.4}, {4.7, 3.2, 1.3}};
    INDArray in = Nd4j.create(ind);
    INDArray stdev = in.std(0);

    INDArray exp = Nd4j.create(new double[]{0.2, 0.25166114784, 0.05773502692});

    System.out.println("Exp dtype: " + exp.data().dataType());
    System.out.println("Exp dtype: " + exp.data().dataType());

    System.out.println("Array: " + Arrays.toString(exp.data().asFloat()));
    assertEquals(exp,stdev);
}
 
Example 18
Source File: ReductionBpOpValidation.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testStdevAlongDimensionBP() {
    //If out = stdev(in) then:
    //dL/dIn = dL/dOut * dOut/dIn
    //dOut/dIn_i = (in_i-mean)/(stdev * (n-1))
    //OR: n instead of n-1, if not bias corrected

    for (boolean biasCorrected : new boolean[]{false, true}) {
        for (boolean keepDims : new boolean[]{false, true}) {
            long[] reducedShape_0 = (keepDims ? new long[]{1, 4} : new long[]{4});
            INDArray preReduceInput = Nd4j.linspace(1, 12, 12).reshape(3, 4);
            long divisor = biasCorrected ? 2 : 3;
            INDArray mean_0 = preReduceInput.mean(0);
            INDArray stdev_0 = preReduceInput.std(biasCorrected, 0);
            INDArray dLdOut_0 = Nd4j.create(new double[]{1, 2, 3, 4}, reducedShape_0);

            INDArray dLdInExpected_0 = preReduceInput.dup();
            dLdInExpected_0.subiRowVector(mean_0)
                    .diviRowVector(stdev_0.mul(divisor))
                    .muliRowVector(dLdOut_0);

            INDArray dLdIn = Nd4j.createUninitialized(3, 4);
            String err = OpValidation.validate(new OpTestCase(new StandardDeviationBp(preReduceInput, dLdOut_0, dLdIn, biasCorrected, keepDims, 0))
                    .expectedOutput(0, dLdInExpected_0));
            assertNull(err);


            divisor = biasCorrected ? 3 : 4;
            long[] reducedShape_1 = (keepDims ? new long[]{3, 1} : new long[]{3});
            INDArray dLdOut_1 = Nd4j.create(new double[]{1, 2, 3}, reducedShape_1);
            INDArray mean_1 = preReduceInput.mean(1);
            INDArray stdev_1 = preReduceInput.std(biasCorrected, 1);
            INDArray dLdInExpected_1 = preReduceInput.dup();
            dLdInExpected_1.subiColumnVector(mean_1)
                    .diviColumnVector(stdev_1.mul(divisor))
                    .muliColumnVector(dLdOut_1.reshape(3, 1));

            dLdIn = Nd4j.createUninitialized(3, 4);
            err = OpValidation.validate(new OpTestCase(new StandardDeviationBp(preReduceInput, dLdOut_1, dLdIn, biasCorrected, keepDims, 1))
                    .expectedOutput(0, dLdInExpected_1));
            assertNull(err, err);
        }
    }
}
 
Example 19
Source File: CrashTest.java    From nd4j with Apache License 2.0 2 votes vote down vote up
protected void op(INDArray x, INDArray y, int i) {
    // broadcast along row & column
    INDArray row = Nd4j.ones(64);
    INDArray column = Nd4j.ones(1024, 1);

    x.addiRowVector(row);
    x.addiColumnVector(column);

    // casual scalar
    x.addi(i * 2);

    // reduction along all dimensions
    float sum = x.sumNumber().floatValue();

    // index reduction
    Nd4j.getExecutioner().exec(new IMax(x), Integer.MAX_VALUE);

    // casual transform
    Nd4j.getExecutioner().exec(new Sqrt(x, x));

    //  dup
    INDArray x1 = x.dup(x.ordering());
    INDArray x2 = x.dup(x.ordering());
    INDArray x3 = x.dup('c');
    INDArray x4 = x.dup('f');


    // vstack && hstack
    INDArray vstack = Nd4j.vstack(x, x1, x2, x3, x4);

    INDArray hstack = Nd4j.hstack(x, x1, x2, x3, x4);

    // reduce3 call
    Nd4j.getExecutioner().exec(new ManhattanDistance(x, x2));


    // flatten call
    INDArray flat = Nd4j.toFlattened(x, x1, x2, x3, x4);


    // reduction along dimension: row & column
    INDArray max_0 = x.max(0);
    INDArray max_1 = x.max(1);


    // index reduction along dimension: row & column
    INDArray imax_0 = Nd4j.argMax(x, 0);
    INDArray imax_1 = Nd4j.argMax(x, 1);


    // logisoftmax, softmax & softmax derivative
    Nd4j.getExecutioner().exec(new OldSoftMax(x));
    Nd4j.getExecutioner().exec(new SoftMaxDerivative(x));
    Nd4j.getExecutioner().exec(new LogSoftMax(x));


    // BooleanIndexing
    BooleanIndexing.replaceWhere(x, 5f, Conditions.lessThan(8f));

    // assing on view
    BooleanIndexing.assignIf(x, x1, Conditions.greaterThan(-1000000000f));

    // std var along all dimensions
    float std = x.stdNumber().floatValue();

    // std var along row & col
    INDArray xStd_0 = x.std(0);
    INDArray xStd_1 = x.std(1);

    // blas call
    float dot = (float) Nd4j.getBlasWrapper().dot(x, x1);

    // mmul
    for (boolean tA : paramsA) {
        for (boolean tB : paramsB) {

            INDArray xT = tA ? x.dup() : x.dup().transpose();
            INDArray yT = tB ? y.dup() : y.dup().transpose();

            Nd4j.gemm(xT, yT, tA, tB);
        }
    }

    // specially for views, checking here without dup and rollover
    Nd4j.gemm(x, y, false, false);

    log.debug("Iteration passed: " + i);
}
 
Example 20
Source File: CrashTest.java    From deeplearning4j with Apache License 2.0 2 votes vote down vote up
protected void op(INDArray x, INDArray y, int i) {
    // broadcast along row & column
    INDArray row = Nd4j.ones(64);
    INDArray column = Nd4j.ones(1024, 1);

    x.addiRowVector(row);
    x.addiColumnVector(column);

    // casual scalar
    x.addi(i * 2);

    // reduction along all dimensions
    float sum = x.sumNumber().floatValue();

    // index reduction
    Nd4j.getExecutioner().exec(new ArgMax(x));

    // casual transform
    Nd4j.getExecutioner().exec(new Sqrt(x, x));

    //  dup
    INDArray x1 = x.dup(x.ordering());
    INDArray x2 = x.dup(x.ordering());
    INDArray x3 = x.dup('c');
    INDArray x4 = x.dup('f');


    // vstack && hstack
    INDArray vstack = Nd4j.vstack(x, x1, x2, x3, x4);

    INDArray hstack = Nd4j.hstack(x, x1, x2, x3, x4);

    // reduce3 call
    Nd4j.getExecutioner().exec(new ManhattanDistance(x, x2));


    // flatten call
    INDArray flat = Nd4j.toFlattened(x, x1, x2, x3, x4);


    // reduction along dimension: row & column
    INDArray max_0 = x.max(0);
    INDArray max_1 = x.max(1);


    // index reduction along dimension: row & column
    INDArray imax_0 = Nd4j.argMax(x, 0);
    INDArray imax_1 = Nd4j.argMax(x, 1);


    // logisoftmax, softmax & softmax derivative
    Nd4j.getExecutioner().exec((CustomOp) new SoftMax(x));
    Nd4j.getExecutioner().exec((CustomOp) new LogSoftMax(x));


    // BooleanIndexing
    BooleanIndexing.replaceWhere(x, 5f, Conditions.lessThan(8f));

    // assing on view
    BooleanIndexing.assignIf(x, x1, Conditions.greaterThan(-1000000000f));

    // std var along all dimensions
    float std = x.stdNumber().floatValue();

    // std var along row & col
    INDArray xStd_0 = x.std(0);
    INDArray xStd_1 = x.std(1);

    // blas call
    float dot = (float) Nd4j.getBlasWrapper().dot(x, x1);

    // mmul
    for (boolean tA : paramsA) {
        for (boolean tB : paramsB) {

            INDArray xT = tA ? x.dup() : x.dup().transpose();
            INDArray yT = tB ? y.dup() : y.dup().transpose();

            Nd4j.gemm(xT, yT, tA, tB);
        }
    }

    // specially for views, checking here without dup and rollover
    Nd4j.gemm(x, y, false, false);

    log.debug("Iteration passed: " + i);
}