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

The following examples show how to use org.nd4j.linalg.api.ndarray.INDArray#diviRowVector() . 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: CudaBroadcastTests.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testPinnedDivRowVector() throws Exception {
    // simple way to stop test if we're not on CUDA backend here
    assertEquals("JcublasLevel1", Nd4j.getBlasWrapper().level1().getClass().getSimpleName());

    INDArray array1 = Nd4j.zeros(15,15);
    array1.putRow(0, Nd4j.create(new float[]{2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f}));
    array1.putRow(1, Nd4j.create(new float[]{2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f}));
    INDArray array2 = Nd4j.create(new float[]{1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f});

    array1.diviRowVector(array2);

    System.out.println("Array1: " + array1);
    System.out.println("Array2: " + array2);

    assertEquals(2.0f, array1.getRow(0).getFloat(0), 0.01);
}
 
Example 2
Source File: CudaPairwiseTrainformsTests.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testPinnedDiviRowVector() throws Exception {
    // simple way to stop test if we're not on CUDA backend here
    assertEquals("JcublasLevel1", Nd4j.getBlasWrapper().level1().getClass().getSimpleName());

    INDArray array1 = Nd4j.create(new float[]{1.5f, 1.5f, 1.5f, 1.5f, 1.5f, 1.5f, 1.5f, 1.5f, 1.5f, 1.5f, 1.5f, 1.5f, 1.5f, 1.5f, 1.5f});
    INDArray array2 = Nd4j.create(new float[]{2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f});

    INDArray result = array1.diviRowVector(array2);

    System.out.println("Array1: " + array1);
    System.out.println("Array2: " + array2);
    System.out.println("Result: " + result);

    assertEquals(0.75f, array1.getRow(0).getFloat(0), 0.01);
}
 
Example 3
Source File: StandardizeStrategy.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/**
 * Normalize a data array
 *
 * @param array the data to normalize
 * @param stats statistics of the data population
 */
@Override
public void preProcess(INDArray array, INDArray maskArray, DistributionStats stats) {
    if (array.rank() <= 2) {
        array.subiRowVector(stats.getMean());
        array.diviRowVector(filteredStd(stats));
    }
    // if array Rank is 3 (time series) samplesxfeaturesxtimesteps
    // if array Rank is 4 (images) samplesxchannelsxrowsxcols
    // both cases operations should be carried out in dimension 1
    else {
        Nd4j.getExecutioner().execAndReturn(new BroadcastSubOp(array, stats.getMean(), array, 1));
        Nd4j.getExecutioner().execAndReturn(new BroadcastDivOp(array, filteredStd(stats), array, 1));
    }

    if (maskArray != null) {
        DataSetUtil.setMaskedValuesToZero(array, maskArray);
    }
}
 
Example 4
Source File: MinMaxStrategy.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/**
 * Normalize a data array
 *
 * @param array the data to normalize
 * @param stats statistics of the data population
 */
@Override
public void preProcess(INDArray array, INDArray maskArray, MinMaxStats stats) {
    if (array.rank() <= 2) {
        array.subiRowVector(stats.getLower());
        array.diviRowVector(stats.getRange());
    }
    // if feature Rank is 3 (time series) samplesxfeaturesxtimesteps
    // if feature Rank is 4 (images) samplesxchannelsxrowsxcols
    // both cases operations should be carried out in dimension 1
    else {
        Nd4j.getExecutioner().execAndReturn(new BroadcastSubOp(array, stats.getLower(), array, 1));
        Nd4j.getExecutioner().execAndReturn(new BroadcastDivOp(array, stats.getRange(), array, 1));
    }

    // Scale by target range
    array.muli(maxRange - minRange);
    // Add target range minimum values
    array.addi(minRange);

    if (maskArray != null) {
        DataSetUtil.setMaskedValuesToZero(array, maskArray);
    }
}
 
Example 5
Source File: StandardizeStrategy.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 * Normalize a data array
 *
 * @param array the data to normalize
 * @param stats statistics of the data population
 */
@Override
public void preProcess(INDArray array, INDArray maskArray, DistributionStats stats) {
    if (array.rank() <= 2) {
        array.subiRowVector(stats.getMean().castTo(array.dataType()));
        array.diviRowVector(filteredStd(stats).castTo(array.dataType()));
    }
    // if array Rank is 3 (time series) samplesxfeaturesxtimesteps
    // if array Rank is 4 (images) samplesxchannelsxrowsxcols
    // both cases operations should be carried out in dimension 1
    else {
        Nd4j.getExecutioner().execAndReturn(new BroadcastSubOp(array, stats.getMean().castTo(array.dataType()), array, 1));
        Nd4j.getExecutioner().execAndReturn(new BroadcastDivOp(array, filteredStd(stats).castTo(array.dataType()), array, 1));
    }

    if (maskArray != null) {
        DataSetUtil.setMaskedValuesToZero(array, maskArray);
    }
}
 
Example 6
Source File: MinMaxStrategy.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 * Normalize a data array
 *
 * @param array the data to normalize
 * @param stats statistics of the data population
 */
@Override
public void preProcess(INDArray array, INDArray maskArray, MinMaxStats stats) {
    if (array.rank() <= 2) {
        array.subiRowVector(stats.getLower().castTo(array.dataType()));
        array.diviRowVector(stats.getRange().castTo(array.dataType()));
    }
    // if feature Rank is 3 (time series) samplesxfeaturesxtimesteps
    // if feature Rank is 4 (images) samplesxchannelsxrowsxcols
    // both cases operations should be carried out in dimension 1
    else {
        Nd4j.getExecutioner().execAndReturn(new BroadcastSubOp(array, stats.getLower().castTo(array.dataType()), array, 1));
        Nd4j.getExecutioner().execAndReturn(new BroadcastDivOp(array, stats.getRange().castTo(array.dataType()), array, 1));
    }

    // Scale by target range
    array.muli(maxRange - minRange);
    // Add target range minimum values
    array.addi(minRange);

    if (maskArray != null) {
        DataSetUtil.setMaskedValuesToZero(array, maskArray);
    }
}
 
Example 7
Source File: Nd4jMatrix.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
@Override
public MathMatrix divideRowVector(MathVector vector) {
    if (vector instanceof Nd4jVector) {
        Nd4jEnvironmentThread thread = EnvironmentThread.getThread(Nd4jEnvironmentThread.class);
        try (MemoryWorkspace workspace = thread.getSpace()) {
            INDArray thisArray = this.getArray();
            INDArray thatArray = Nd4jVector.class.cast(vector).getArray();
            thisArray.diviRowVector(thatArray);
            return this;
        }
    } else {
        return MathMatrix.super.divideRowVector(vector);
    }
}
 
Example 8
Source File: Transforms.java    From nd4j 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 9
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 10
Source File: MiscOpValidation.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testClipByNorm0(){
    //Expected: if array.norm2(0) is less than 1.0, not modified
    //Otherwise: array.tad(x,1) = array.tad(x,1) * 1.0 / array.tad(x,1).norm2()

    Nd4j.getRandom().setSeed(12345);
    INDArray arr = Nd4j.rand(5,4);
    INDArray norm2_0 = arr.norm2(0);
    arr.diviRowVector(norm2_0);

    INDArray initNorm2 = Nd4j.create(new double[]{2.2, 2.1, 2.0, 1.9}, new int[]{4});     //Initial norm2s along dimension 0
    arr.muliRowVector(initNorm2);
    norm2_0 = arr.norm2(0);

    assertEquals(initNorm2, norm2_0);

    INDArray out = Nd4j.create(arr.shape());

    INDArray norm2_0b = out.norm2(0);
    INDArray expNorm = Nd4j.create(new double[]{2.0, 2.0, 2.0, 1.9}, new int[]{1, 4});  //Post clip norm2s along dimension 0
    INDArray exp = arr.divRowVector(norm2_0b).muliRowVector(expNorm);

    OpTestCase op = new OpTestCase(//Clip to norm2 of 2.0, along dimension 0
            new ClipByNorm(arr, out, 2.0, 0))
            .expectedOutput(0, exp);

    assertNull(OpValidation.validate(op));
}
 
Example 11
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 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: BasicClassifier.java    From audiveris with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Apply the known norms on the provided (raw) features.
 *
 * @param features raw features, to be normalized in situ
 */
private void normalize (INDArray features)
{
    features.subiRowVector(norms.means);
    features.diviRowVector(norms.stds);
}