/** * 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; }
/** * 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; }
/** * 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; }
/** * L2 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 LossL2(INDArray weights) { if (weights != null && !weights.isRowVector()) { throw new IllegalArgumentException("Weights array must be a row vector"); } this.weights = weights; }
@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); this.v = viewArray; //Reshape to match the expected shape of the input gradient arrays this.v = Shape.newShapeNoCopy(this.v, gradientShape, gradientOrder == 'f'); if (v == null) throw new IllegalStateException("Could not correctly reshape gradient view array"); this.gradientReshapeOrder = gradientOrder; }
@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; }
/** * 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; }
@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; }
/** * 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; }
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; }
/** * This method stacks vertically examples with the same shape, increasing result dimensionality. I.e. if you provide bunch of 3D tensors, output will be 4D tensor. Alignment is always applied to axis 0. * * @return */ public static INDArray pile(INDArray... arrays) { // if we have vectors as input, it's just vstack use case if (arrays[0].isRowVector() && arrays[0].rank() == 2) { return Nd4j.vstack(arrays); } long[] shape = arrays[0].shape(); long[] newShape = ArrayUtils.add(shape, 0, 1); boolean shouldReshape = true; if (arrays[0].size(0) == 1) shouldReshape = false; List<INDArray> reshaped = new ArrayList<>(); for(INDArray array: arrays) { if (!shouldReshape) reshaped.add(array); else reshaped.add(array.reshape(array.ordering(), newShape)); } return Nd4j.vstack(reshaped); }
@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"); }
@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; }
@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; }
@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; }
@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; }
else if(indices.isRowVector()) { for(int i = 0; i < indices.length(); i++) { arrList.add(slice(indices.getInt(i)));
else if(indices.isRowVector()) { for(int i = 0; i < indices.length(); i++) { arrList.add(slice(indices.getInt(i)));
return arr; if (arr.isRowVector()) { if (arr instanceof IComplexNDArray) {
/** * Get the vector along a particular dimension * * @param index the index of the vector to get * @param dimension the dimension to get the vector from * @return the vector along a particular dimension */ @Override public INDArray vectorAlongDimension(int index, int dimension) { if (dimension < 0) dimension = Shape.rank(javaShapeInformation) + dimension; //return the whole thing if (dimension == Shape.rank(javaShapeInformation) - 1 && size(dimension) == 1 && rank() > 2 || rank() > 2 && dimension == 0 && size(dimension) == 1) { return this; } INDArray ret = tensorAlongDimension(index, dimension); if (isMatrix() && ret.isVector() && dimension == 1 && !ret.isRowVector()) return ret.reshape(ArrayUtil.reverseCopy(ret.shape())); else if (isMatrix() && ret.isVector() && dimension == 0 && !ret.isColumnVector()) return ret.reshape(ArrayUtil.reverseCopy(ret.shape())); return ret; }