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

The following examples show how to use org.nd4j.linalg.api.ndarray.INDArray#isRowVector() . 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: NadamUpdater.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Override
public void setStateViewArray(INDArray viewArray, long[] gradientShape, char gradientOrder, boolean initialize) {
    if (!viewArray.isRowVector())
        throw new IllegalArgumentException("Invalid input: expect row vector input");
    if (initialize)
        viewArray.assign(0);
    long length = viewArray.length();
    this.m = viewArray.get(NDArrayIndex.point(0), NDArrayIndex.interval(0, length / 2));
    this.v = viewArray.get(NDArrayIndex.point(0), NDArrayIndex.interval(length / 2, length));

    //Reshape to match the expected shape of the input gradient arrays
    this.m = Shape.newShapeNoCopy(this.m, gradientShape, gradientOrder == 'f');
    this.v = Shape.newShapeNoCopy(this.v, gradientShape, gradientOrder == 'f');
    if (m == null || v == null)
        throw new IllegalStateException("Could not correctly reshape gradient view arrays");

    this.gradientReshapeOrder = gradientOrder;
}
 
Example 2
Source File: AdaMaxUpdater.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public void setStateViewArray(INDArray viewArray, long[] gradientShape, char gradientOrder, boolean initialize) {
    if (!viewArray.isRowVector())
        throw new IllegalArgumentException("Invalid input: expect row vector input");
    if (initialize)
        viewArray.assign(0);
    long length = viewArray.length();
    this.m = viewArray.get(NDArrayIndex.point(0), NDArrayIndex.interval(0, length / 2));
    this.u = viewArray.get(NDArrayIndex.point(0), NDArrayIndex.interval(length / 2, length));

    //Reshape to match the expected shape of the input gradient arrays
    this.m = Shape.newShapeNoCopy(this.m, gradientShape, gradientOrder == 'f');
    this.u = Shape.newShapeNoCopy(this.u, gradientShape, gradientOrder == 'f');
    if (m == null || u == null)
        throw new IllegalStateException("Could not correctly reshape gradient view arrays");

    this.gradientReshapeOrder = gradientOrder;
}
 
Example 3
Source File: AdaDeltaUpdater.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public void setStateViewArray(INDArray viewArray, long[] gradientShape, char gradientOrder, boolean initialize) {
    if (!viewArray.isRowVector())
        throw new IllegalArgumentException("Invalid input: expect row vector input");
    if (initialize)
        viewArray.assign(0);
    long length = viewArray.length();
    this.msg = viewArray.get(NDArrayIndex.point(0), NDArrayIndex.interval(0, length / 2));
    this.msdx = viewArray.get(NDArrayIndex.point(0), NDArrayIndex.interval(length / 2, length));

    //Reshape to match the expected shape of the input gradient arrays
    this.msg = Shape.newShapeNoCopy(this.msg, gradientShape, gradientOrder == 'f');
    this.msdx = Shape.newShapeNoCopy(this.msdx, gradientShape, gradientOrder == 'f');
    if (msg == null || msdx == null)
        throw new IllegalStateException("Could not correctly reshape gradient view arrays");
}
 
Example 4
Source File: AMSGradUpdater.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Override
public void setStateViewArray(INDArray viewArray, long[] gradientShape, char gradientOrder, boolean initialize) {
    if (!viewArray.isRowVector())
        throw new IllegalArgumentException("Invalid input: expect row vector input");
    if (initialize)
        viewArray.assign(0);
    val n = viewArray.length() / 3;
    this.m = viewArray.get(NDArrayIndex.point(0), NDArrayIndex.interval(0, n));
    this.v = viewArray.get(NDArrayIndex.point(0), NDArrayIndex.interval(n, 2*n));
    this.vHat = viewArray.get(NDArrayIndex.point(0), NDArrayIndex.interval(2*n, 3*n));

    //Reshape to match the expected shape of the input gradient arrays
    this.m = Shape.newShapeNoCopy(this.m, gradientShape, gradientOrder == 'f');
    this.v = Shape.newShapeNoCopy(this.v, gradientShape, gradientOrder == 'f');
    this.vHat = Shape.newShapeNoCopy(this.vHat, gradientShape, gradientOrder == 'f');
    if (m == null || v == null || vHat == null)
        throw new IllegalStateException("Could not correctly reshape gradient view arrays");

    this.gradientReshapeOrder = gradientOrder;
}
 
Example 5
Source File: AdamUpdater.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public void setStateViewArray(INDArray viewArray, long[] gradientShape, char gradientOrder, boolean initialize) {
    if (!viewArray.isRowVector())
        throw new IllegalArgumentException("Invalid input: expect row vector input");
    if (initialize)
        viewArray.assign(0);
    long length = viewArray.length();
    this.m = viewArray.get(NDArrayIndex.point(0), NDArrayIndex.interval(0, length / 2));
    this.v = viewArray.get(NDArrayIndex.point(0), NDArrayIndex.interval(length / 2, length));

    //Reshape to match the expected shape of the input gradient arrays
    this.m = Shape.newShapeNoCopy(this.m, gradientShape, gradientOrder == 'f');
    this.v = Shape.newShapeNoCopy(this.v, gradientShape, gradientOrder == 'f');
    if (m == null || v == null)
        throw new IllegalStateException("Could not correctly reshape gradient view arrays");

    this.gradientReshapeOrder = gradientOrder;
}
 
Example 6
Source File: AdaDeltaUpdater.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Override
public void setStateViewArray(INDArray viewArray, long[] gradientShape, char gradientOrder, boolean initialize) {
    if (!viewArray.isRowVector())
        throw new IllegalArgumentException("Invalid input: expect row vector input");
    if (initialize)
        viewArray.assign(0);
    long length = viewArray.length();
    this.msg = viewArray.get(NDArrayIndex.point(0), NDArrayIndex.interval(0, length / 2));
    this.msdx = viewArray.get(NDArrayIndex.point(0), NDArrayIndex.interval(length / 2, length));

    //Reshape to match the expected shape of the input gradient arrays
    this.msg = Shape.newShapeNoCopy(this.msg, gradientShape, gradientOrder == 'f');
    this.msdx = Shape.newShapeNoCopy(this.msdx, gradientShape, gradientOrder == 'f');
    if (msg == null || msdx == null)
        throw new IllegalStateException("Could not correctly reshape gradient view arrays");
}
 
Example 7
Source File: AdamUpdater.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Override
public void setStateViewArray(INDArray viewArray, long[] gradientShape, char gradientOrder, boolean initialize) {
    if (!viewArray.isRowVector())
        throw new IllegalArgumentException("Invalid input: expect row vector input");
    if (initialize)
        viewArray.assign(0);
    long length = viewArray.length();
    this.m = viewArray.get(NDArrayIndex.point(0), NDArrayIndex.interval(0, length / 2));
    this.v = viewArray.get(NDArrayIndex.point(0), NDArrayIndex.interval(length / 2, length));

    //Reshape to match the expected shape of the input gradient arrays
    this.m = Shape.newShapeNoCopy(this.m, gradientShape, gradientOrder == 'f');
    this.v = Shape.newShapeNoCopy(this.v, gradientShape, gradientOrder == 'f');
    if (m == null || v == null)
        throw new IllegalStateException("Could not correctly reshape gradient view arrays");

    this.gradientReshapeOrder = gradientOrder;
}
 
Example 8
Source File: NadamUpdater.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public void setStateViewArray(INDArray viewArray, long[] gradientShape, char gradientOrder, boolean initialize) {
    if (!viewArray.isRowVector())
        throw new IllegalArgumentException("Invalid input: expect row vector input");
    if (initialize)
        viewArray.assign(0);
    long length = viewArray.length();
    this.m = viewArray.get(NDArrayIndex.point(0), NDArrayIndex.interval(0, length / 2));
    this.v = viewArray.get(NDArrayIndex.point(0), NDArrayIndex.interval(length / 2, length));

    //Reshape to match the expected shape of the input gradient arrays
    this.m = Shape.newShapeNoCopy(this.m, gradientShape, gradientOrder == 'f');
    this.v = Shape.newShapeNoCopy(this.v, gradientShape, gradientOrder == 'f');
    if (m == null || v == null)
        throw new IllegalStateException("Could not correctly reshape gradient view arrays");

    this.gradientReshapeOrder = gradientOrder;
}
 
Example 9
Source File: PLNetLoss.java    From AILibs with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Computes the gradient of the NLL for PL networks w.r.t. the k-th dyad according to equation (28) in [1].
 * @param plNetOutputs  The outputs for M_n dyads generated by a PLNet's output layer in order of their ranking (from best to worst).
 * @param k				The ranking position with respect to which the gradient should be computed. Assumes zero-based indices, unlike the paper.
 * @return				The gradient of the NLL loss w.r.t. the k-th dyad in the ranking.
 */
public static INDArray computeLossGradient(INDArray plNetOutputs, int k) {
	if (!(plNetOutputs.isRowVector()) || plNetOutputs.size(1) < 2 || k < 0 || k >= plNetOutputs.size(1)) {
		throw new IllegalArgumentException("Input has to be a row vector of 2 or more elements. And k has to be a valid index of plNetOutputs.");
	}
	long dyadRankingLength = plNetOutputs.size(1);
	double errorGradient = 0;
	for (int m = 0; m <= k; m++) {
		INDArray innerSumSlice = plNetOutputs.get(NDArrayIndex.interval(m, dyadRankingLength));
		innerSumSlice = Transforms.exp(innerSumSlice);
		double innerSum = innerSumSlice.sum(1).getDouble(0);
		errorGradient += Math.exp(plNetOutputs.getDouble(k)) / innerSum;
	}
	errorGradient -= 1;
	return Nd4j.create(new double[] {errorGradient});
}
 
Example 10
Source File: AdaGrad.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public void setStateViewArray(INDArray viewArray, long[] gradientShape, char gradientOrder, boolean initialize) {
    if (!viewArray.isRowVector() && !(viewArray.rank() == 2 && viewArray.columns() == 1 && viewArray.rows() == 1))
        throw new IllegalArgumentException("Invalid input: expect row vector input");
    if (initialize)
        viewArray.assign(epsilon);
    this.historicalGradient = viewArray;
    //Reshape to match the expected shape of the input gradient arrays
    this.historicalGradient = Shape.newShapeNoCopy(this.historicalGradient, gradientShape, gradientOrder == 'f');
    if (historicalGradient == null)
        throw new IllegalStateException("Could not correctly reshape gradient view array");

    this.gradientReshapeOrder = gradientOrder;
}
 
Example 11
Source File: AdaGradUpdater.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public void setStateViewArray(INDArray viewArray, long[] gradientShape, char gradientOrder, boolean initialize) {
    if (!viewArray.isRowVector())
        throw new IllegalArgumentException("Invalid input: expect row vector input");
    if (initialize)
        viewArray.assign(epsilon);
    this.historicalGradient = viewArray;
    //Reshape to match the expected shape of the input gradient arrays
    this.historicalGradient = Shape.newShapeNoCopy(this.historicalGradient, gradientShape, gradientOrder == 'f');
    if (historicalGradient == null)
        throw new IllegalStateException("Could not correctly reshape gradient view array");

    this.gradientReshapeOrder = gradientOrder;
}
 
Example 12
Source File: RmsPropUpdater.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public void setStateViewArray(INDArray viewArray, long[] gradientShape, char gradientOrder, boolean initialize) {
    if (!viewArray.isRowVector())
        throw new IllegalArgumentException("Invalid input: expect row vector input");
    if (initialize)
        viewArray.assign(config.getEpsilon());
    this.lastGradient = viewArray;

    //Reshape to match the expected shape of the input gradient arrays
    this.lastGradient = Shape.newShapeNoCopy(this.lastGradient, gradientShape, gradientOrder == 'f');
    if (lastGradient == null)
        throw new IllegalStateException("Could not correctly reshape gradient view array");

    gradientReshapeOrder = gradientOrder;
}
 
Example 13
Source File: LossMCXENT.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Multi-Class Cross Entropy loss function where each the output is (optionally) weighted/scaled by a fixed scalar value.
 * Note that the weights array must be a row vector, of length equal to the labels/output dimension 1 size.
 * A weight vector of 1s should give identical results to no weight vector.
 *
 * @param weights Weights array (row vector). May be null.
 */
public LossMCXENT(@JsonProperty("softmaxClipEps") double softmaxClipEps, @JsonProperty("weights") INDArray weights) {
    if (weights != null && !weights.isRowVector()) {
        throw new IllegalArgumentException("Weights array must be a row vector");
    }
    if(softmaxClipEps < 0 || softmaxClipEps > 0.5){
        throw new IllegalArgumentException("Invalid clipping epsilon: epsilon should be >= 0 (but near zero). Got: "
                + softmaxClipEps);
    }
    this.weights = weights;
    this.softmaxClipEps = softmaxClipEps;
}
 
Example 14
Source File: LossMCXENT.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Multi-Class Cross Entropy loss function where each the output is (optionally) weighted/scaled by a fixed scalar value.
 * Note that the weights array must be a row vector, of length equal to the labels/output dimension 1 size.
 * A weight vector of 1s should give identical results to no weight vector.
 *
 * @param weights Weights array (row vector). May be null.
 */
public LossMCXENT(@JsonProperty("softmaxClipEps") double softmaxClipEps, @JsonProperty("weights") INDArray weights) {
    if (weights != null && !weights.isRowVector()) {
        throw new IllegalArgumentException("Weights array must be a row vector");
    }
    if(softmaxClipEps < 0 || softmaxClipEps > 0.5){
        throw new IllegalArgumentException("Invalid clipping epsilon: epsilon should be >= 0 (but near zero). Got: "
                + softmaxClipEps);
    }
    this.weights = weights;
    this.softmaxClipEps = softmaxClipEps;
}
 
Example 15
Source File: LossBinaryXENT.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Binary cross entropy where each the output is
 * (optionally) weighted/scaled by a fixed scalar value.
 * Note that the weights array must be a row vector, of length equal to
 * the labels/output dimension 1 size.
 * A weight vector of 1s should give identical results to no weight vector.
 *
 * @param clipEps Epsilon value for clipping. Probabilities are clipped in range of [eps, 1-eps]. Default eps: 1e-5
 * @param weights Weights array (row vector). May be null.
 */
public LossBinaryXENT(@JsonProperty("clipEps") double clipEps, @JsonProperty("weights") INDArray weights){
    if (weights != null && !weights.isRowVector()) {
        throw new IllegalArgumentException("Weights array must be a row vector");
    }
    if(clipEps < 0 || clipEps > 0.5){
        throw new IllegalArgumentException("Invalid clipping epsilon value: epsilon should be >= 0 (but near zero)."
                + "Got: " + clipEps);
    }

    this.clipEps = clipEps;
    this.weights = weights;
}
 
Example 16
Source File: DotAggregation.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray getAccumulatedResult() {
    INDArray stack = super.getAccumulatedResult();

    if (aggregationWidth == 1)
        return stack;

    if (stack.isRowVector()) {
        return Nd4j.scalar(stack.sumNumber().doubleValue());
    } else {
        return stack.sum(1);
    }
}
 
Example 17
Source File: LossMAPE.java    From deeplearning4j with Apache License 2.0 3 votes vote down vote up
/**
 * Mean Absolute Percentage Error loss function where each the output is (optionally) weighted/scaled by a flags scalar value.
 * Note that the weights array must be a row vector, of length equal to the labels/output dimension 1 size.
 * A weight vector of 1s should give identical results to no weight vector.
 *
 * @param weights Weights array (row vector). May be null.
 */
public LossMAPE(INDArray weights) {
    if (weights != null && !weights.isRowVector()) {
        throw new IllegalArgumentException("Weights array must be a row vector");
    }
    this.weights = weights;
}
 
Example 18
Source File: LossL1.java    From deeplearning4j with Apache License 2.0 3 votes vote down vote up
/**
 * L1 loss function where each the output is (optionally) weighted/scaled by a flags scalar value.
 * Note that the weights array must be a row vector, of length equal to the labels/output dimension 1 size.
 * A weight vector of 1s should give identical results to no weight vector.
 *
 * @param weights Weights array (row vector). May be null.
 */
public LossL1(INDArray weights) {
    if (weights != null && !weights.isRowVector()) {
        throw new IllegalArgumentException("Weights array must be a row vector");
    }
    this.weights = weights;
}
 
Example 19
Source File: LossMAPE.java    From nd4j with Apache License 2.0 3 votes vote down vote up
/**
 * Mean Absolute Percentage Error loss function where each the output is (optionally) weighted/scaled by a flags scalar value.
 * Note that the weights array must be a row vector, of length equal to the labels/output dimension 1 size.
 * A weight vector of 1s should give identical results to no weight vector.
 *
 * @param weights Weights array (row vector). May be null.
 */
public LossMAPE(INDArray weights) {
    if (weights != null && !weights.isRowVector()) {
        throw new IllegalArgumentException("Weights array must be a row vector");
    }
    this.weights = weights;
}
 
Example 20
Source File: LossMSLE.java    From nd4j with Apache License 2.0 3 votes vote down vote up
/**
 * Mean Squared Logarithmic Error loss function where each the output is (optionally) weighted/scaled by a flags scalar value.
 * Note that the weights array must be a row vector, of length equal to the labels/output dimension 1 size.
 * A weight vector of 1s should give identical results to no weight vector.
 *
 * @param weights Weights array (row vector). May be null.
 */
public LossMSLE(INDArray weights) {
    if (weights != null && !weights.isRowVector()) {
        throw new IllegalArgumentException("Weights array must be a row vector");
    }
    this.weights = weights;
}