/** * copy addition of two matrices * * @param other the second ndarray to add * @return the result of the addition */ @Override public INDArray add(INDArray other) { return dup().addi(other); }
/** * copy addition of two matrices * * @param other the second ndarray to add * @param result the result ndarray * @return the result of the addition */ @Override public INDArray add(INDArray other, INDArray result) { return dup().addi(other, result); }
@Override public IComplexNDArray add(IComplexNumber n, IComplexNDArray result) { return dup().addi(n, result); }
@Override public IComplexNDArray add(IComplexNumber n) { return dup().addi(n); }
public void preProcess(INDArray features) { features.divi(this.maxPixelVal); //Scaled to 0->1 if (this.maxRange - this.minRange != 1) features.muli(this.maxRange - this.minRange); //Scaled to minRange -> maxRange if (this.minRange != 0) features.addi(this.minRange); //Offset by minRange }
/** * @param lower row vector of lower bounds * @param upper row vector of upper bounds */ public MinMaxStats(@NonNull INDArray lower, @NonNull INDArray upper) { // Check for 0 differences and round up to epsilon INDArray diff = upper.sub(lower); INDArray addedPadding = Transforms.max(diff, Nd4j.EPS_THRESHOLD).subi(diff); // If any entry in `addedPadding` is not 0, then we had to add something to prevent 0 difference, Add this same // value to the upper bounds to actually apply the padding, and log about it if (addedPadding.sumNumber().doubleValue() > 0) { log.info("API_INFO: max val minus min val found to be zero. Transform will round up to epsilon to avoid nans."); upper.addi(addedPadding); } this.lower = lower; this.upper = upper; }
/** * Gets feature specific learning rates * Adagrad keeps a history of gradients being passed in. * Note that each gradient passed in becomes adapted over time, hence * the opName adagrad * * @param gradient the gradient to get learning rates for * @param iteration * @return the feature specific learning rates */ public INDArray getGradient(INDArray gradient, int iteration) { if (historicalGradient == null) throw new IllegalStateException("Updater has not been initialized with view state"); historicalGradient.addi(gradient.mul(gradient)); INDArray sqrtHistory = sqrt(historicalGradient.dup(gradientReshapeOrder), false).addi(epsilon); // lr * gradient / (sqrt(sumSquaredGradients) + epsilon) INDArray ret = gradient.muli(sqrtHistory.rdivi(learningRate)); numIterations++; return ret; }
@Override public void preProcess(MultiDataSet multiDataSet) { for( int i=0; i<featureIndices.length; i++ ){ INDArray f = multiDataSet.getFeatures(featureIndices[i]); f.divi(this.maxPixelVal); //Scaled to 0->1 if (this.maxRange - this.minRange != 1) f.muli(this.maxRange - this.minRange); //Scaled to minRange -> maxRange if (this.minRange != 0) f.addi(this.minRange); //Offset by minRange } }
/** * Gets feature specific learning rates * Adagrad keeps a history of gradients being passed in. * Note that each gradient passed in becomes adapted over time, hence the opName adagrad * * @param gradient the gradient to get learning rates for * @param iteration */ @Override public void applyUpdater(INDArray gradient, int iteration, int epoch) { if (historicalGradient == null) throw new IllegalStateException("Updater has not been initialized with view state"); double learningRate = config.getLearningRate(iteration, epoch); double epsilon = config.getEpsilon(); historicalGradient.addi(gradient.mul(gradient)); INDArray sqrtHistory = sqrt(historicalGradient.dup(gradientReshapeOrder), false).addi(epsilon); // lr * gradient / (sqrt(sumSquaredGradients) + epsilon) gradient.muli(sqrtHistory.rdivi(learningRate)); } }
@Override public void applyUpdater(INDArray gradient, int iteration, int epoch) { if (lastGradient == null) throw new IllegalStateException("Updater has not been initialized with view state"); double learningRate = config.getLearningRate(iteration, epoch); double rmsDecay = config.getRmsDecay(); double epsilon = config.getEpsilon(); lastGradient.muli(rmsDecay).addi(gradient.mul(gradient).muli(1 - rmsDecay)); // lr * gradient / (sqrt(cache) + 1e-8) gradient.muli(learningRate).divi(Transforms.sqrt(lastGradient.dup(gradientReshapeOrder), false).addi(epsilon)); } }
/** * 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; }
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); }
public void fit(DataSet dataSet) { mean = dataSet.getFeatureMatrix().mean(0); std = dataSet.getFeatureMatrix().std(0); std.addi(Nd4j.scalar(Nd4j.EPS_THRESHOLD)); if (std.min(1) == Nd4j.scalar(Nd4j.EPS_THRESHOLD)) logger.info("API_INFO: Std deviation found to be zero. Transform will round upto epsilon to avoid nans."); }
/** * Scales the ndarray columns * to the given min/max values * * @param min the minimum number * @param max the max number */ public static void scaleMinMax(double min, double max, INDArray toScale) { //X_std = (X - X.min(axis=0)) / (X.max(axis=0) - X.min(axis=0)) X_scaled = X_std * (max - min) + min INDArray min2 = toScale.min(0); INDArray max2 = toScale.max(0); INDArray std = toScale.subRowVector(min2).diviRowVector(max2.sub(min2)); INDArray scaled = std.mul(max - min).addi(min); toScale.assign(scaled); }
/** * @Deprecated * Subtract by the column means and divide by the standard deviation */ @Deprecated @Override public void normalizeZeroMeanZeroUnitVariance() { INDArray columnMeans = getFeatures().mean(0); INDArray columnStds = getFeatureMatrix().std(0); setFeatures(getFeatures().subiRowVector(columnMeans)); columnStds.addi(Nd4j.scalar(Nd4j.EPS_THRESHOLD)); setFeatures(getFeatures().diviRowVector(columnStds)); }
private INDArray calculateBernoulli(INDArray minorityLabels, INDArray labelMask, double targetMinorityDist) { INDArray minorityClass = minorityLabels.dup().muli(labelMask); INDArray majorityClass = Transforms.not(minorityLabels).muli(labelMask); //all minorityLabel class, keep masks as is //presence of minoriy class and donotmask minority windows set to true return label as is if (majorityClass.sumNumber().intValue() == 0 || (minorityClass.sumNumber().intValue() > 0 && donotMaskMinorityWindows)) return labelMask; //all majority class and set to not mask all majority windows sample majority class by 1-targetMinorityDist if (minorityClass.sumNumber().intValue() == 0 && !maskAllMajorityWindows) return labelMask.muli(1 - targetMinorityDist); //Probabilities to be used for bernoulli sampling INDArray minoritymajorityRatio = minorityClass.sum(1).div(majorityClass.sum(1)); INDArray majorityBernoulliP = minoritymajorityRatio.muli(1 - targetMinorityDist).divi(targetMinorityDist); BooleanIndexing.replaceWhere(majorityBernoulliP, 1.0, Conditions.greaterThan(1.0)); //if minority ratio is already met round down to 1.0 return majorityClass.muliColumnVector(majorityBernoulliP).addi(minorityClass); }
public INDArray scoreArray(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask) { if (labels.size(1) != preOutput.size(1)) { throw new IllegalArgumentException( "Labels array numColumns (size(1) = " + labels.size(1) + ") does not match output layer" + " number of outputs (nOut = " + preOutput.size(1) + ") "); } INDArray scoreArr; //INDArray output = Nd4j.getExecutioner().execAndReturn(Nd4j.getOpFactory().createTransform(activationFn, preOutput.dup())); INDArray output = activationFn.getActivation(preOutput.dup(), true); scoreArr = Transforms.log(output.addi(1.0).divi(labels.add(1.0)), false); scoreArr = scoreArr.muli(scoreArr).divi(labels.size(1)); //Weighted loss function if (weights != null) { if (weights.length() != output.size(1)) { throw new IllegalStateException("Weights vector (length " + weights.length() + ") does not match output.size(1)=" + output.size(1)); } scoreArr.muliRowVector(weights); } if (mask != null) { LossUtil.applyMask(scoreArr, mask); } return scoreArr; }
/** * Returns the covariance matrix of a data set of many records, each with N features. * It also returns the average values, which are usually going to be important since in this * version, all modes are centered around the mean. It's a matrix that has elements that are * expressed as average dx_i * dx_j (used in procedure) or average x_i * x_j - average x_i * average x_j * * @param in A matrix of vectors of fixed length N (N features) on each row * @return INDArray[2], an N x N covariance matrix is element 0, and the average values is element 1. */ public static INDArray[] covarianceMatrix(INDArray in) { long dlength = in.rows(); long vlength = in.columns(); INDArray sum = Nd4j.create(vlength); INDArray product = Nd4j.create(vlength, vlength); for (int i = 0; i < vlength; i++) sum.getColumn(i).assign(in.getColumn(i).sumNumber().doubleValue() / dlength); for (int i = 0; i < dlength; i++) { INDArray dx1 = in.getRow(i).sub(sum); product.addi(dx1.reshape(vlength, 1).mmul(dx1.reshape(1, vlength))); } product.divi(dlength); return new INDArray[] {product, sum}; }