/** * @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; }
/** * Get the feature wise * range for the statistics. * Note that this is a lazy getter. * It is only computed when needed. * @return the feature wise range * given the min and max */ public INDArray getRange() { if (range == null) { try (MemoryWorkspace ws = Nd4j.getMemoryManager().scopeOutOfWorkspaces()) { range = upper.sub(lower); } } return range; }
/** * 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); }
INDArray dLdZAlpha = mdc.alpha.sub(pi);
public static void main(String[] args) { //Nd4j.dtype = DataBuffer.DOUBLE; INDArray nd1 = Nd4j.create(new double[]{1,2,3,4,5,6},new int[]{2,3}); System.out.println("nd1:\n"+nd1); //create nd-array variable ndv to be able to print result of nondestructive operations. add scalar to matrix and assign ndv the sum. INDArray ndv = nd1.add(1); System.out.println("nd1.add(1):\n"+ndv); ndv = nd1.mul(5); System.out.println("nd1.mul(5):\n"+ndv); ndv = nd1.sub(3); System.out.println("nd1.sub(3):\n"+ndv); ndv = nd1.div(2); System.out.println("nd1.div(2):\n"+ndv); //add column vector to matrix INDArray nd2 = Nd4j.create(new double[]{10,20},new int[]{2,1}); //vector as column System.out.println("nd2:\n"+nd2); ndv = nd1.addColumnVector(nd2); System.out.println("nd1.addColumnVector(nd2):\n"+ndv); // add row vector to matrix INDArray nd3 = Nd4j.create(new double[]{30,40,50},new int[]{1, 3}); //vector as row System.out.println("nd3:\n"+nd3); ndv = nd1.addRowVector(nd3); System.out.println("nd1.addRowVector(nd3):\n"+ndv); //multiply two matrices of equal dimensions elementwise. INDArray nd4 = Nd4j.create(new double[]{1,2,1,2,1,2},new int[]{2,3}); System.out.println("nd4:\n"+nd4); ndv = nd1.mul(nd4); System.out.println("nd1.mul(nd4):\n"+ndv); }
grad = output.mulColumnVector(col).sub(temp); } else { grad = output.subi(labels);
/** Same as checkMmul, but for matrix subtraction */ public static boolean checkSubtract(INDArray first, INDArray second, double maxRelativeDifference, double minAbsDifference) { RealMatrix rmFirst = convertToApacheMatrix(first); RealMatrix rmSecond = convertToApacheMatrix(second); INDArray result = first.sub(second); RealMatrix rmResult = rmFirst.subtract(rmSecond); if (!checkShape(rmResult, result)) return false; boolean ok = checkEntries(rmResult, result, maxRelativeDifference, minAbsDifference); if (!ok) { INDArray onCopies = Shape.toOffsetZeroCopy(first).sub(Shape.toOffsetZeroCopy(second)); printFailureDetails(first, second, rmResult, result, onCopies, "sub"); } return ok; }
/** * Estimate the variance of a single record with reduced # of dimensions. * @param data A single record with the same <i>N</i> features as the constructing data set * @param ndims The number of dimensions to include in calculation * @return The fraction (0 to 1) of the total variance covered by the <i>ndims</i> basis set. */ public double estimateVariance(INDArray data, int ndims) { INDArray dx = data.sub(mean); INDArray v = eigenvectors.transpose().mmul(dx.reshape(dx.columns(), 1)); INDArray t2 = Transforms.pow(v, 2); double fraction = t2.get(NDArrayIndex.interval(0, ndims)).sumNumber().doubleValue(); double total = t2.sumNumber().doubleValue(); return fraction / total; }
private INDArray scoreArray(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask) { INDArray scoreArr; INDArray output = activationFn.getActivation(preOutput.dup(), true); INDArray yMinusyHat = Transforms.abs(labels.sub(output)); scoreArr = yMinusyHat.mul(yMinusyHat); scoreArr = scoreArr.mul(trainMask); if (mask != null) { scoreArr.muliColumnVector(mask); } return scoreArr; }
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) + ") "); } /* mean of (yhat - y * log(yhat)) */ //INDArray postOutput = Nd4j.utioner().execAndReturn(Nd4j.getOpFactory().createTransform(activationFn, preOutput.dup())); INDArray postOutput = activationFn.getActivation(preOutput.dup(), true); INDArray scoreArr = Transforms.log(postOutput); scoreArr.muli(labels); scoreArr = postOutput.sub(scoreArr); if (mask != null) { LossUtil.applyMask(scoreArr, mask); } return scoreArr; }
@Override public INDArray computeGradient(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask) { INDArray output = activationFn.getActivation(preOutput.dup(), true); INDArray yMinusyHat = labels.sub(output); INDArray dldyhat = yMinusyHat.mul(-2); INDArray gradients = activationFn.backprop(preOutput.dup(), dldyhat).getFirst(); gradients = gradients.mul(trainMask); //multiply with masks, always if (mask != null) { gradients.muliColumnVector(mask); } return gradients; }
/** * * @param func * @param x0 * @param f0 * @param h * @param oneSided * @return */ public static INDArray denseDifference(Function<INDArray,INDArray> func, INDArray x0,INDArray f0, INDArray h,INDArray oneSided) { INDArray hVecs = Nd4j.diag(h.reshape(1,h.length())); INDArray dx,df,x; INDArray jTransposed = Nd4j.create(x0.length(),f0.length()); for(int i = 0; i < h.length(); i++) { INDArray hVecI = hVecs.slice(i); x = (x0.add(hVecI)); dx = x.slice(i).sub(x0.slice(i)); df = func.apply(x).sub(f0); INDArray div = df.div(dx); jTransposed.putSlice(i,div); } if(f0.length() == 1) jTransposed = jTransposed.ravel(); return jTransposed; }
/** * 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}; }
@Override public INDArray computeGradient(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 output = activationFn.getActivation(preOutput.dup(), true); INDArray outSubLabels = output.sub(labels); INDArray dLda = Nd4j.getExecutioner().execAndReturn(new Sign(outSubLabels)); if (weights != null) { dLda.muliRowVector(weights); } if (mask != null && LossUtil.isPerOutputMasking(dLda, mask)) { //For *most* activation functions: we don't actually need to mask dL/da in addition to masking dL/dz later //but: some, like softmax, require both (due to dL/dz_i being a function of dL/da_j, for i != j) //We could add a special case for softmax (activationFn instanceof ActivationSoftmax) but that would be // error prone - but buy us a tiny bit of performance LossUtil.applyMask(dLda, mask); } //dL/dz INDArray gradients = activationFn.backprop(preOutput, dLda).getFirst(); //TODO activation function param gradients if (mask != null) { LossUtil.applyMask(gradients, mask); } return gradients; }
@Override public INDArray computeGradient(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 output = activationFn.getActivation(preOutput.dup(), true); INDArray actSubPredicted = labels.sub(output); INDArray dLda = Nd4j.getExecutioner().execAndReturn(new Sign(actSubPredicted)); INDArray absLabels = Nd4j.getExecutioner().execAndReturn(new Abs(labels.dup())); dLda.divi(absLabels).muli(-100.0 / labels.size(1)); //Weighted loss function if (weights != null) { dLda.muliRowVector(weights); } if (mask != null && LossUtil.isPerOutputMasking(dLda, mask)) { //For *most* activation functions: we don't actually need to mask dL/da in addition to masking dL/dz later //but: some, like softmax, require both (due to dL/dz_i being a function of dL/da_j, for i != j) //We could add a special case for softmax (activationFn instanceof ActivationSoftmax) but that would be // error prone - but buy us a tiny bit of performance LossUtil.applyMask(dLda, mask); } INDArray gradient = activationFn.backprop(preOutput, dLda).getFirst(); //TODO activation functions with params if (mask != null) { LossUtil.applyMask(gradient, mask); } return gradient; }
INDArray numerator = output.sub(labels); INDArray denominator = Nd4j.getExecutioner().execAndReturn(new TimesOneMinus(output)); // output * (1-output) INDArray dLda = numerator.divi(denominator);
INDArray currShape = NDArrayUtil.toNDArray(arr.shape()); INDArray startIndex = Transforms.floor(currShape.sub(shapeMatrix).divi(Nd4j.scalar(2))); INDArray endIndex = startIndex.add(shapeMatrix); INDArrayIndex[] indexes = Indices.createFromStartAndEnd(startIndex, endIndex);
final INDArray operandB = operandA.transpose(); final INDArray pairwiseSub = Transforms.exp(operandA.sub(operandB));