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

The following examples show how to use org.nd4j.linalg.factory.Nd4j#eye() . 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: NDBaseTest.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testMatchConditionCount() {
    NDBase base = new NDBase();
    INDArray x = Nd4j.createFromArray(1.0, 1.0, 1.0, 0.0, 1.0, 1.0);
    INDArray y = base.matchConditionCount(x, Conditions.epsEquals(0.0));
    assertEquals(Nd4j.scalar(1L), y);

    x = Nd4j.eye(3);
    y = base.matchConditionCount(x, Conditions.epsEquals(1.0), true, 1);
    INDArray y_exp = Nd4j.createFromArray(new Long[][]{{1L}, {1L}, {1L}});
    assertEquals(y_exp, y);

    y = base.matchConditionCount(x, Conditions.epsEquals(1.0), true, 0);
    y_exp = Nd4j.createFromArray(new Long[][]{{1L, 1L, 1L}});
    assertEquals(y_exp, y);

    y = base.matchConditionCount(x, Conditions.epsEquals(1.0), false, 1);
    y_exp = Nd4j.createFromArray(1L, 1L, 1L);
    assertEquals(y_exp, y);

    y = base.matchConditionCount(x, Conditions.epsEquals(1.0), false, 0);
    assertEquals(y_exp, y);
}
 
Example 2
Source File: WeightInitIdentity.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
private INDArray setIdentity2D(long[] shape, char order, INDArray paramView) {
    INDArray ret;
    if (order == Nd4j.order()) {
        ret = Nd4j.eye(shape[0]);
    } else {
        ret = Nd4j.createUninitialized(shape, order).assign(Nd4j.eye(shape[0]));
    }

    if(scale != null){
        ret.muli(scale);
    }

    INDArray flat = Nd4j.toFlattened(order, ret);
    paramView.assign(flat);
    return paramView.reshape(order, shape);
}
 
Example 3
Source File: NDArrayTestsFortran.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testIdentity() {
    INDArray eye = Nd4j.eye(5);
    assertTrue(Arrays.equals(new long[] {5, 5}, eye.shape()));
    eye = Nd4j.eye(5);
    assertTrue(Arrays.equals(new long[] {5, 5}, eye.shape()));


}
 
Example 4
Source File: IdentityInitScheme.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray doCreate(long[] shape, INDArray paramsView) {
    if(shape.length != 2 || shape[0] != shape[1]){
        throw new IllegalStateException("Cannot use IDENTITY init with parameters of shape "
                + Arrays.toString(shape) + ": weights must be a square matrix for identity");
    }
    if(order() == Nd4j.order()){
        return Nd4j.eye(shape[0]);
    } else {
        return  Nd4j.createUninitialized(shape, order()).assign(Nd4j.eye(shape[0]));
    }
}
 
Example 5
Source File: BaseLapack.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray getPFactor(int M, INDArray ipiv) {
    // The simplest permutation is the identity matrix
    INDArray P = Nd4j.eye(M); // result is a square matrix with given size
    for (int i = 0; i < ipiv.length(); i++) {
        int pivot = ipiv.getInt(i) - 1; // Did we swap row #i with anything?
        if (pivot > i) { // don't reswap when we get lower down in the vector
            INDArray v1 = P.getColumn(i).dup(); // because of row vs col major order we'll ...
            INDArray v2 = P.getColumn(pivot); // ... make a transposed matrix immediately
            P.putColumn(i, v2);
            P.putColumn(pivot, v1); // note dup() above is required - getColumn() is a 'view'
        }
    }
    return P; // the permutation matrix - contains a single 1 in any row and column
}
 
Example 6
Source File: ShapeOpValidation.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testEye(){
    int[] rows = new int[]{3,3,3,3};
    int[] cols = new int[]{3,2,2,2};
    int[][] batch = new int[][]{null, null, {4}, {3,3}};
    INDArray[] expOut = new INDArray[4];

    expOut[0] = Nd4j.eye(3);
    expOut[1] = Nd4j.create(new double[][]{{1,0},{0,1},{0,0}});
    expOut[2] = Nd4j.create(4,3,2);
    for( int i=0; i<4; i++ ){
        expOut[2].get(NDArrayIndex.point(i), NDArrayIndex.all(), NDArrayIndex.all()).assign(expOut[1]);
    }
    expOut[3] = Nd4j.create(3,3,3,2);
    for( int i=0; i<3; i++ ){
        for( int j=0; j<3; j++ ) {
            expOut[3].get(NDArrayIndex.point(i), NDArrayIndex.point(j), NDArrayIndex.all(), NDArrayIndex.all()).assign(expOut[1]);
        }
    }


    for(int i=0; i<3; i++ ) {
        log.info("Starting: " + i);
        INDArray out = Nd4j.create(expOut[i].shape());

        DynamicCustomOp.DynamicCustomOpsBuilder op = DynamicCustomOp.builder("eye")
                .addOutputs(out)
                .addIntegerArguments(rows[i], cols[i]);
        if(batch[i] != null){
            op.addIntegerArguments(batch[i]);
        }

        Nd4j.getExecutioner().exec(op.build());

        assertEquals(expOut[i], out);
    }
}
 
Example 7
Source File: NDBaseTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testRank() {
    NDBase base = new NDBase();
    INDArray x = Nd4j.eye(3);
    INDArray y = base.rank(x);
    INDArray y_exp = Nd4j.scalar(2);
    System.out.println(y);
    assertEquals(y_exp, y);
}
 
Example 8
Source File: NDArrayTestsFortran.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testIdentity() {
    INDArray eye = Nd4j.eye(5);
    assertTrue(Arrays.equals(new long[] {5, 5}, eye.shape()));
    eye = Nd4j.eye(5);
    assertTrue(Arrays.equals(new long[] {5, 5}, eye.shape()));


}
 
Example 9
Source File: IdentityInitScheme.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray doCreate(DataType dataType, long[] shape, INDArray paramsView) {
    if(shape.length != 2 || shape[0] != shape[1]){
        throw new IllegalStateException("Cannot use IDENTITY init with parameters of shape "
                + Arrays.toString(shape) + ": weights must be a square matrix for identity");
    }
    if(order() == Nd4j.order()){
        return Nd4j.eye(shape[0]);
    } else {
        return  Nd4j.createUninitialized(dataType, shape, order()).assign(Nd4j.eye(shape[0]));
    }
}
 
Example 10
Source File: BaseLapack.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray getPFactor(int M, INDArray ipiv) {
    // The simplest permutation is the identity matrix
    INDArray P = Nd4j.eye(M); // result is a square matrix with given size
    for (int i = 0; i < ipiv.length(); i++) {
        int pivot = ipiv.getInt(i) - 1; // Did we swap row #i with anything?
        if (pivot > i) { // don't reswap when we get lower down in the vector
            INDArray v1 = P.getColumn(i).dup(); // because of row vs col major order we'll ...
            INDArray v2 = P.getColumn(pivot); // ... make a transposed matrix immediately
            P.putColumn(i, v2);
            P.putColumn(pivot, v1); // note dup() above is required - getColumn() is a 'view'
        }
    }
    return P; // the permutation matrix - contains a single 1 in any row and column
}
 
Example 11
Source File: Transforms.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Raises a square matrix to a power <i>n</i>, which can be positive, negative, or zero.
 * The behavior is similar to the numpy matrix_power() function.  The algorithm uses
 * repeated squarings to minimize the number of mmul() operations needed
 * <p>If <i>n</i> is zero, the identity matrix is returned.</p>
 * <p>If <i>n</i> is negative, the matrix is inverted and raised to the abs(n) power.</p>
 *
 * @param in  A square matrix to raise to an integer power, which will be changed if dup is false.
 * @param n   The integer power to raise the matrix to.
 * @param dup If dup is true, the original input is unchanged.
 * @return The result of raising <i>in</i> to the <i>n</i>th power.
 */
public static INDArray mpow(INDArray in, int n, boolean dup) {
    Preconditions.checkState(in.isMatrix() && in.isSquare(), "Input must be a square matrix: got input with shape %s", in.shape());
    if (n == 0) {
        if (dup)
            return Nd4j.eye(in.rows());
        else
            return in.assign(Nd4j.eye(in.rows()));
    }
    INDArray temp;
    if (n < 0) {
        temp = InvertMatrix.invert(in, !dup);
        n = -n;
    } else
        temp = in.dup();
    INDArray result = temp.dup();
    if (n < 4) {
        for (int i = 1; i < n; i++) {
            result.mmuli(temp);
        }
        if (dup)
            return result;
        else
            return in.assign(result);
    } else {
        // lets try to optimize by squaring itself a bunch of times
        int squares = (int) (Math.log(n) / Math.log(2.0));
        for (int i = 0; i < squares; i++)
            result = result.mmul(result);
        int diff = (int) Math.round(n - Math.pow(2.0, squares));
        for (int i = 0; i < diff; i++)
            result.mmuli(temp);
        if (dup)
            return result;
        else
            return in.assign(result);
    }
}
 
Example 12
Source File: WeightInitUtil.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
public static INDArray initWeights(double fanIn, double fanOut, long[] shape, WeightInit initScheme,
                Distribution dist, char order, INDArray paramView) {
    switch (initScheme) {
        case DISTRIBUTION:
            if (dist instanceof OrthogonalDistribution) {
                dist.sample(paramView.reshape(order, shape));
            } else {
                dist.sample(paramView);
            }
            break;
        case RELU:
            Nd4j.randn(paramView).muli(FastMath.sqrt(2.0 / fanIn)); //N(0, 2/nIn)
            break;
        case RELU_UNIFORM:
            double u = Math.sqrt(6.0 / fanIn);
            Nd4j.rand(paramView, Nd4j.getDistributions().createUniform(-u, u)); //U(-sqrt(6/fanIn), sqrt(6/fanIn)
            break;
        case SIGMOID_UNIFORM:
            double r = 4.0 * Math.sqrt(6.0 / (fanIn + fanOut));
            Nd4j.rand(paramView, Nd4j.getDistributions().createUniform(-r, r));
            break;
        case UNIFORM:
            double a = 1.0 / Math.sqrt(fanIn);
            Nd4j.rand(paramView, Nd4j.getDistributions().createUniform(-a, a));
            break;
        case LECUN_UNIFORM:
            double b = 3.0 / Math.sqrt(fanIn);
            Nd4j.rand(paramView, Nd4j.getDistributions().createUniform(-b, b));
            break;
        case XAVIER:
            Nd4j.randn(paramView).muli(FastMath.sqrt(2.0 / (fanIn + fanOut)));
            break;
        case XAVIER_UNIFORM:
            //As per Glorot and Bengio 2010: Uniform distribution U(-s,s) with s = sqrt(6/(fanIn + fanOut))
            //Eq 16: http://jmlr.org/proceedings/papers/v9/glorot10a/glorot10a.pdf
            double s = Math.sqrt(6.0) / Math.sqrt(fanIn + fanOut);
            Nd4j.rand(paramView, Nd4j.getDistributions().createUniform(-s, s));
            break;
        case LECUN_NORMAL:  //Fall through: these 3 are equivalent
        case NORMAL:
        case XAVIER_FAN_IN:
            Nd4j.randn(paramView).divi(FastMath.sqrt(fanIn));
            break;
        case XAVIER_LEGACY:
            Nd4j.randn(paramView).divi(FastMath.sqrt(shape[0] + shape[1]));
            break;
        case ZERO:
            paramView.assign(0.0);
            break;
        case ONES:
            paramView.assign(1.0);
            break;
        case IDENTITY:
            if(shape.length != 2 || shape[0] != shape[1]){
                throw new IllegalStateException("Cannot use IDENTITY init with parameters of shape "
                        + Arrays.toString(shape) + ": weights must be a square matrix for identity");
            }
            INDArray ret;
            if(order == Nd4j.order()){
                ret = Nd4j.eye(shape[0]);
            } else {
                ret = Nd4j.createUninitialized(shape, order).assign(Nd4j.eye(shape[0]));
            }
            INDArray flat = Nd4j.toFlattened(order, ret);
            paramView.assign(flat);
            break;
        case VAR_SCALING_NORMAL_FAN_IN:
            Nd4j.exec(new TruncatedNormalDistribution(paramView, 0.0, Math.sqrt(1.0 / fanIn)));
            break;
        case VAR_SCALING_NORMAL_FAN_OUT:
            Nd4j.exec(new TruncatedNormalDistribution(paramView, 0.0, Math.sqrt(1.0 / fanOut)));
            break;
        case VAR_SCALING_NORMAL_FAN_AVG:
            Nd4j.exec(new TruncatedNormalDistribution(paramView, 0.0, Math.sqrt(2.0 / (fanIn + fanOut))));
            break;
        case VAR_SCALING_UNIFORM_FAN_IN:
            double scalingFanIn = 3.0 / Math.sqrt(fanIn);
            Nd4j.rand(paramView, Nd4j.getDistributions().createUniform(-scalingFanIn, scalingFanIn));
            break;
        case VAR_SCALING_UNIFORM_FAN_OUT:
            double scalingFanOut = 3.0 / Math.sqrt(fanOut);
            Nd4j.rand(paramView, Nd4j.getDistributions().createUniform(-scalingFanOut, scalingFanOut));
            break;
        case VAR_SCALING_UNIFORM_FAN_AVG:
            double scalingFanAvg = 3.0 / Math.sqrt((fanIn + fanOut) / 2);
            Nd4j.rand(paramView, Nd4j.getDistributions().createUniform(-scalingFanAvg, scalingFanAvg));
            break;
        default:
            throw new IllegalStateException("Illegal weight init value: " + initScheme);
    }

    return paramView.reshape(order, shape);
}
 
Example 13
Source File: PCA.java    From deeplearning4j with Apache License 2.0 3 votes vote down vote up
/**
 * Calculates the principal component vectors and their eigenvalues (lambda) for the covariance matrix.
 * The result includes two things: the eigenvectors (modes) as result[0] and the eigenvalues (lambda)
 * as result[1].
 *
 * @param cov The covariance matrix (calculated with the covarianceMatrix(in) method)
 * @return Array INDArray[2] "result".  The principal component vectors in decreasing flexibility are
 *      the columns of element 0 and the eigenvalues are element 1.
 */
public static INDArray[] principalComponents(INDArray cov) {
    Preconditions.checkArgument(cov.isMatrix() && cov.isSquare(), "Convariance matrix must be a square matrix: has shape %s", cov.shape());
    INDArray[] result = new INDArray[2];
    result[0] = Nd4j.eye(cov.rows());
    result[1] = Eigen.symmetricGeneralizedEigenvalues(result[0], cov, true);
    return result;
}