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

The following examples show how to use org.nd4j.linalg.api.ndarray.INDArray#subiRowVector() . 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 testPinnedSubiRowVector() throws Exception {
        // simple way to stop test if we're not on CUDA backend here
        INDArray array1 = Nd4j.zeros(1500,150);
        INDArray array2 = Nd4j.linspace(1,150,150);

        AtomicAllocator.getInstance().getPointer(array1, (CudaContext) AtomicAllocator.getInstance().getDeviceContext().getContext());
        AtomicAllocator.getInstance().getPointer(array2, (CudaContext) AtomicAllocator.getInstance().getDeviceContext().getContext());

        long time1 = System.currentTimeMillis();
        array1.subiRowVector(array2);
        long time2 = System.currentTimeMillis();

        System.out.println("Execution time: " + (time2 - time1));

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

        assertEquals(-1.0f, array1.getRow(0).getFloat(0), 0.01);
        assertEquals(-3.0f, array1.getRow(0).getFloat(2), 0.01);
        assertEquals(-10.0f, array1.getRow(0).getFloat(9), 0.01);
    }
 
Example 2
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 3
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 4
Source File: HalfOpsTests.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
    public void testBroadcasts1() throws Exception {
        INDArray array1 = Nd4j.zeros(1500,150);
        INDArray array2 = Nd4j.linspace(1,150,150);

        AtomicAllocator.getInstance().getPointer(array1, (CudaContext) AtomicAllocator.getInstance().getDeviceContext().getContext());
        AtomicAllocator.getInstance().getPointer(array2, (CudaContext) AtomicAllocator.getInstance().getDeviceContext().getContext());

        long time1 = System.currentTimeMillis();
        array1.subiRowVector(array2);
        long time2 = System.currentTimeMillis();

        System.out.println("Execution time: " + (time2 - time1));

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

        assertEquals(-1.0f, array1.getRow(0).getFloat(0), 0.01);
        assertEquals(-3.0f, array1.getRow(0).getFloat(2), 0.01);
        assertEquals(-10.0f, array1.getRow(0).getFloat(9), 0.01);
    }
 
Example 5
Source File: TestPCA.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testFactorDims() {
    int m = 13;
    int n = 4;

    double f[] = new double[] {7, 1, 11, 11, 7, 11, 3, 1, 2, 21, 1, 11, 10, 26, 29, 56, 31, 52, 55, 71, 31, 54, 47,
                    40, 66, 68, 6, 15, 8, 8, 6, 9, 17, 22, 18, 4, 23, 9, 8, 60, 52, 20, 47, 33, 22, 6, 44, 22, 26,
                    34, 12, 12};

    INDArray A = Nd4j.create(f, new int[] {m, n}, 'f');

    INDArray A1 = A.dup('f');
    INDArray Factor = org.nd4j.linalg.dimensionalityreduction.PCA.pca_factor(A1, 3, true);
    A1 = A.subiRowVector(A.mean(0));

    INDArray Reduced = A1.mmul(Factor);
    INDArray Reconstructed = Reduced.mmul(Factor.transpose());
    INDArray Diff = Reconstructed.sub(A1);
    for (int i = 0; i < m * n; i++) {
        assertEquals("Reconstructed matrix is very different from the original.", 0.0, Diff.getDouble(i), 1.0);
    }
}
 
Example 6
Source File: TestPCA.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testFactorVariance() {
    int m = 13;
    int n = 4;

    double f[] = new double[] {7, 1, 11, 11, 7, 11, 3, 1, 2, 21, 1, 11, 10, 26, 29, 56, 31, 52, 55, 71, 31, 54, 47,
                    40, 66, 68, 6, 15, 8, 8, 6, 9, 17, 22, 18, 4, 23, 9, 8, 60, 52, 20, 47, 33, 22, 6, 44, 22, 26,
                    34, 12, 12};

    INDArray A = Nd4j.create(f, new int[] {m, n}, 'f');

    INDArray A1 = A.dup('f');
    INDArray Factor1 = org.nd4j.linalg.dimensionalityreduction.PCA.pca_factor(A1, 0.95, true);
    A1 = A.subiRowVector(A.mean(0));
    INDArray Reduced1 = A1.mmul(Factor1);
    INDArray Reconstructed1 = Reduced1.mmul(Factor1.transpose());
    INDArray Diff1 = Reconstructed1.sub(A1);
    for (int i = 0; i < m * n; i++) {
        assertEquals("Reconstructed matrix is very different from the original.", 0.0, Diff1.getDouble(i), 0.1);
    }
    INDArray A2 = A.dup('f');
    INDArray Factor2 = org.nd4j.linalg.dimensionalityreduction.PCA.pca_factor(A2, 0.50, true);
    assertTrue("Variance differences should change factor sizes.", Factor1.columns() > Factor2.columns());
}
 
Example 7
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 8
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 9
Source File: TestPCA.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testFactorDims() {
    int m = 13;
    int n = 4;

    double f[] = new double[] {7, 1, 11, 11, 7, 11, 3, 1, 2, 21, 1, 11, 10, 26, 29, 56, 31, 52, 55, 71, 31, 54, 47,
                    40, 66, 68, 6, 15, 8, 8, 6, 9, 17, 22, 18, 4, 23, 9, 8, 60, 52, 20, 47, 33, 22, 6, 44, 22, 26,
                    34, 12, 12};

    INDArray A = Nd4j.create(f, new int[] {m, n}, 'f');

    INDArray A1 = A.dup('f');
    INDArray Factor = org.nd4j.linalg.dimensionalityreduction.PCA.pca_factor(A1, 3, true);
    A1 = A.subiRowVector(A.mean(0));

    INDArray Reduced = A1.mmul(Factor);
    INDArray Reconstructed = Reduced.mmul(Factor.transpose());
    INDArray Diff = Reconstructed.sub(A1);
    for (int i = 0; i < m * n; i++) {
        assertEquals("Reconstructed matrix is very different from the original.", 0.0, Diff.getDouble(i), 1.0);
    }
}
 
Example 10
Source File: PCA.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Calculates pca factors of a matrix, for a flags number of reduced features
 * returns the factors to scale observations 
 *
 * The return is a factor matrix to reduce (normalized) feature sets
 *
 * @see pca(INDArray, int, boolean)
 *
 * @param A the array of features, rows are results, columns are features - will be changed
 * @param nDims the number of components on which to project the features 
 * @param normalize whether to normalize (adjust each feature to have zero mean)
 * @return the reduced feature set
 */
public static INDArray pca_factor(INDArray A, int nDims, boolean normalize) {

    if (normalize) {
        // Normalize to mean 0 for each feature ( each column has 0 mean )
        INDArray mean = A.mean(0);
        A.subiRowVector(mean);
    }

    long m = A.rows();
    long n = A.columns();

    // The prepare SVD results, we'll decomp A to UxSxV'
    INDArray s = Nd4j.create(m < n ? m : n);
    INDArray VT = Nd4j.create(n, n, 'f');

    // Note - we don't care about U 
    Nd4j.getBlasWrapper().lapack().gesvd(A, s, null, VT);

    // for comparison k & nDims are the equivalent values in both methods implementing PCA

    // So now let's rip out the appropriate number of left singular vectors from
    // the V output (note we pulls rows since VT is a transpose of V)
    INDArray V = VT.transpose();
    INDArray factor = Nd4j.create(n, nDims, 'f');
    for (int i = 0; i < nDims; i++) {
        factor.putColumn(i, V.getColumn(i));
    }

    return factor;
}
 
Example 11
Source File: PCA.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Calculates pca factors of a matrix, for a flags number of reduced features
 * returns the factors to scale observations 
 *
 * The return is a factor matrix to reduce (normalized) feature sets
 *
 * @see pca(INDArray, int, boolean)
 *
 * @param A the array of features, rows are results, columns are features - will be changed
 * @param nDims the number of components on which to project the features 
 * @param normalize whether to normalize (adjust each feature to have zero mean)
 * @return the reduced feature set
 */
public static INDArray pca_factor(INDArray A, int nDims, boolean normalize) {

    if (normalize) {
        // Normalize to mean 0 for each feature ( each column has 0 mean )
        INDArray mean = A.mean(0);
        A.subiRowVector(mean);
    }

    long m = A.rows();
    long n = A.columns();

    // The prepare SVD results, we'll decomp A to UxSxV'
    INDArray s = Nd4j.create(A.dataType(), m < n ? m : n);
    INDArray VT = Nd4j.create(A.dataType(), new long[]{n, n}, 'f');

    // Note - we don't care about U 
    Nd4j.getBlasWrapper().lapack().gesvd(A, s, null, VT);

    // for comparison k & nDims are the equivalent values in both methods implementing PCA

    // So now let's rip out the appropriate number of left singular vectors from
    // the V output (note we pulls rows since VT is a transpose of V)
    INDArray V = VT.transpose();
    INDArray factor = Nd4j.create(A.dataType(),new long[]{n, nDims}, 'f');
    for (int i = 0; i < nDims; i++) {
        factor.putColumn(i, V.getColumn(i));
    }

    return factor;
}
 
Example 12
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 13
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 14
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 15
Source File: Nd4jMatrix.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
@Override
public MathMatrix subtractRowVector(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.subiRowVector(thatArray);
            return this;
        }
    } else {
        return MathMatrix.super.subtractRowVector(vector);
    }
}
 
Example 16
Source File: CudaPairwiseTrainformsTests.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testPinnedSubiRowVector() 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});

    array1.subiRowVector(array2);

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

    assertEquals(-0.5f, array1.getRow(0).getFloat(0), 0.01);
}
 
Example 17
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 18
Source File: PCA.java    From nd4j with Apache License 2.0 4 votes vote down vote up
/**
 * Calculates pca vectors of a matrix, for a given variance. A larger variance (99%)
 * will result in a higher order feature set.
 *
 * To use the returned factor: multiply feature(s) by the factor to get a reduced dimension
 *
 * INDArray Areduced = A.mmul( factor ) ;
 * 
 * The array Areduced is a projection of A onto principal components
 *
 * @see pca(INDArray, double, boolean)
 *
 * @param A the array of features, rows are results, columns are features - will be changed
 * @param variance the amount of variance to preserve as a float 0 - 1
 * @param normalize whether to normalize (set features to have zero mean)
 * @return the matrix to mulitiply a feature by to get a reduced feature set
 */
public static INDArray pca_factor(INDArray A, double variance, boolean normalize) {
    if (normalize) {
        // Normalize to mean 0 for each feature ( each column has 0 mean )
        INDArray mean = A.mean(0);
        A.subiRowVector(mean);
    }

    long m = A.rows();
    long n = A.columns();

    // The prepare SVD results, we'll decomp A to UxSxV'
    INDArray s = Nd4j.create(m < n ? m : n);
    INDArray VT = Nd4j.create(n, n, 'f');

    // Note - we don't care about U 
    Nd4j.getBlasWrapper().lapack().gesvd(A, s, null, VT);

    // Now convert the eigs of X into the eigs of the covariance matrix
    for (int i = 0; i < s.length(); i++) {
        s.putScalar(i, Math.sqrt(s.getDouble(i)) / (m - 1));
    }

    // Now find how many features we need to preserve the required variance
    // Which is the same percentage as a cumulative sum of the eigenvalues' percentages
    double totalEigSum = s.sumNumber().doubleValue() * variance;
    int k = -1; // we will reduce to k dimensions
    double runningTotal = 0;
    for (int i = 0; i < s.length(); i++) {
        runningTotal += s.getDouble(i);
        if (runningTotal >= totalEigSum) { // OK I know it's a float, but what else can we do ?
            k = i + 1; // we will keep this many features to preserve the reqd. variance
            break;
        }
    }
    if (k == -1) { // if we need everything
        throw new RuntimeException("No reduction possible for reqd. variance - use smaller variance");
    }
    // So now let's rip out the appropriate number of left singular vectors from
    // the V output (note we pulls rows since VT is a transpose of V)
    INDArray V = VT.transpose();
    INDArray factor = Nd4j.create(n, k, 'f');
    for (int i = 0; i < k; i++) {
        factor.putColumn(i, V.getColumn(i));
    }

    return factor;
}
 
Example 19
Source File: BarnesHutTsne.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
public static void zeroMean(INDArray input) {
    INDArray means = input.mean(0);
    input.subiRowVector(means);
}
 
Example 20
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);
}