@Override public INDArray sample(INDArray target) { return target.assign(value); } }
public LogSoftMax(INDArray x, INDArray y, INDArray z, long n) { super(x, y, z, n); //ensure the result is the same //do a reference check here because it's cheaper if (x != z) z.assign(x); }
/** * Copy a to b * * @param a the origin matrix * @param b the destination matrix */ @Override public void copy(INDArray a, INDArray b) { b.assign(a); }
@Override public void exec() { z.assign(x); }
@Override public INDArray ones(long[] shape) { //ensure shapes that wind up being scalar end up with the write shape INDArray ret = create(shape); ret.assign(1); return ret; }
@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; }
@Override public INDArray sample(INDArray target){ return target.assign(sample(target.shape())); } }
@Override public void exec() { if (x != z) { z.assign(x.transpose()); } else { this.z = x.transpose(); } }
@Override public INDArray valueArrayOf(long[] shape, double value) { INDArray ret = Nd4j.createUninitialized(shape, Nd4j.order()); ret.assign(value); return ret; }
@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; }
@Override public INDArray doCreate(long[] shape, INDArray paramsView) { return Nd4j.createUninitialized(shape, order()).assign(1.0); }
/** * Creates an ndarray with the specified value * as the only value in the ndarray * * @param shape the shape of the ndarray * @param value the value to assign * @return the created ndarray */ @Override public INDArray valueArrayOf(int[] shape, double value) { INDArray ret = Nd4j.createUninitialized(shape, Nd4j.order()); ret.assign(value); return ret; }
/** * Creates a row vector with the specified number of columns * * @param rows the number of rows in the matrix * @param columns the columns of the ndarray * @param value the value to assign * @return the created ndarray */ @Override public INDArray valueArrayOf(long rows, long columns, double value) { INDArray create = createUninitialized(new long[] {rows, columns}, Nd4j.order()); create.assign(value); return create; }
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; }
@Override public INDArray doCreate(long[] shape, INDArray paramsView) { if(shape == null) { throw new ND4JIllegalStateException("Shape must not be null!"); } return Nd4j.createUninitialized(shape, order()).assign(0.0); }
@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])); } }
static INDArray resize(INDArray arr, int [] shape) { INDArray resized = Nd4j.create(shape); resized.get(NDArrayIndex.createCoveringShape(arr.shape())).assign(arr); return resized; }
@Override public INDArray noOp() { if (z != null && x != z) return z().assign(x); else return x().dup(x().ordering()); }
protected INDArray handleParamsView(INDArray outputArray, INDArray paramView) { //minor optimization when the views are the same, just return if(paramView == null || paramView == outputArray) return outputArray; INDArray flat = Nd4j.toFlattened(order(), outputArray); if (flat.length() != paramView.length()) throw new RuntimeException("ParamView length does not match initialized weights length (view length: " + paramView.length() + ", view shape: " + Arrays.toString(paramView.shape()) + "; flattened length: " + flat.length()); paramView.assign(flat); return paramView.reshape(order(), outputArray.shape()); }