@Override public Double set(int i, Double aDouble) { container.putScalar(i,aDouble); return aDouble; }
@Override public INDArray put(INDArrayIndex[] indices, Number element) { INDArray get = get(indices); for (int i = 0; i < get.length(); i++) get.putScalar(i, element.doubleValue()); return this; }
@Override public void process(long[]... coord) { if (condition.apply(to.getDouble(coord[0]))) to.putScalar(coord[0], dynamic.apply(to.getDouble(coord[0])).doubleValue()); } });
@Override public void process(long[]... coord) { if (condition.apply(to.getDouble(coord[0]))) to.putScalar(coord[0], function.apply(to.getDouble(coord[0])).doubleValue()); } });
static INDArray find(INDArray arr1, Predicate<Double> predicate) { List<Double> values = new ArrayList<>(); for(int i = 0; i < arr1.length(); i++) { Double value = arr1.getDouble(i); if(predicate.test(value)) { values.add(value); } } INDArray result = Nd4j.create(new int[]{values.size()}); for(int i = 0; i < values.size(); i++) { result.putScalar(i, values.get(i)); } return result; }
@Override public INDArray put(INDArrayIndex[] indices, Number element) { Nd4j.getCompressor().autoDecompress(this); INDArray get = get(indices); for (int i = 0; i < get.length(); i++) get.putScalar(i, element.doubleValue()); return this; }
@Override public void add(int i, Double aDouble) { rangeCheck(i); growCapacity(i); moveForward(i); container.putScalar(i,aDouble); size++; }
@Override public INDArray sample(INDArray target) { Iterator<long[]> idxIter = new NdIndexIterator(target.shape()); //For consistent values irrespective of c vs. fortran ordering long len = target.length(); for (long i = 0; i < len; i++) { target.putScalar(idxIter.next(), sample()); } return target; } }
/** * Copy real numbers to arr * @param arr the arr to copy to */ protected void copyRealTo(INDArray arr) { INDArray linear = arr.linearView(); IComplexNDArray thisLinear = linearView(); if (arr.isScalar()) arr.putScalar(0, getReal(0)); else for (int i = 0; i < linear.length(); i++) { arr.putScalar(i, thisLinear.getReal(i)); } }
public static INDArray convertFromApacheMatrix(RealMatrix matrix) { int[] shape = new int[] {matrix.getRowDimension(), matrix.getColumnDimension()}; INDArray out = Nd4j.create(shape); for (int i = 0; i < shape[0]; i++) { for (int j = 0; j < shape[1]; j++) { double value = matrix.getEntry(i, j); out.putScalar(new int[] {i, j}, value); } } return out; }
/** * Binarizes the dataset such that any number greater than cutoff is 1 otherwise zero * * @param cutoff the cutoff point */ @Override public void binarize(double cutoff) { INDArray linear = getFeatureMatrix().linearView(); for (int i = 0; i < getFeatures().length(); i++) { double curr = linear.getDouble(i); if (curr > cutoff) getFeatures().putScalar(i, 1); else getFeatures().putScalar(i, 0); } }
@Override public INDArray condi(Condition condition) { Nd4j.getCompressor().autoDecompress(this); INDArray linear = this; for (int i = 0; i < length(); i++) { boolean met = condition.apply(linear.getDouble(i)); linear.putScalar(i, met ? 1 : 0); } return this; }
@Override public INDArray getReal() { INDArray result = Nd4j.create(shape()); IComplexNDArray linearView = linearView(); INDArray linearRet = result.linearView(); for (int i = 0; i < linearView.length(); i++) { linearRet.putScalar(i, linearView.getReal(i)); } return result; }
static INDArray compare(INDArray arr1, INDArray arr2, Predicate<Boolean []> predicate) { INDArray result = Nd4j.create(arr1.shape()); for (int i = 0; i < arr1.length(); i++) { boolean answer = predicate.test(new Boolean[]{arr1.getDouble(i) == 1.0, arr2.getDouble(i) == 1.0}); result.putScalar(i, answer ? 1.0 : 0.0); } return result; }
@Override public boolean add(Double aDouble) { if(container == null) { container = Nd4j.create(10); } else if(size == container.length()) { INDArray newContainer = Nd4j.create(container.length() * 2); newContainer.put(new INDArrayIndex[]{NDArrayIndex.interval(0,container.length())},container); container = newContainer; } container.putScalar(size++,aDouble); return true; }
@Override public INDArray percentile(Number quantile, int... dimension) { if (quantile.doubleValue() < 0 || quantile.doubleValue() > 100) throw new ND4JIllegalStateException("Percentile value should be in 0...100 range"); if (isScalar()) return Nd4j.scalar(this.getDouble(0)); INDArray sorted = Nd4j.getNDArrayFactory().sort(this.dup(this.ordering()), false, dimension); // there's no practical sense doing this on GPU, stride will be just size of TAD. INDArray ret = Nd4j.createUninitialized(sorted.tensorssAlongDimension(dimension)); for (int i = 0; i < ret.length(); i++) { ret.putScalar(i, getPercentile(quantile, sorted.tensorAlongDimension(i, dimension))); } return ret; }
@Override public INDArray percentile(Number quantile, int... dimension) { if (quantile.doubleValue() < 0 || quantile.doubleValue() > 100) throw new ND4JIllegalStateException("Percentile value should be in 0...100 range"); if (isScalar()) return Nd4j.scalar(this.getDouble(0)); INDArray sorted = Nd4j.getNDArrayFactory().sort(this.dup(this.ordering()), false, dimension); // there's no practical sense doing this on GPU, stride will be just size of TAD. INDArray ret = Nd4j.createUninitialized(sorted.tensorssAlongDimension(dimension)); for (int i = 0; i < ret.length(); i++) { ret.putScalar(i, getPercentile(quantile, sorted.tensorAlongDimension(i, dimension))); } return ret; }