@Override public Object next() { INDArray s = iterateOver.slice(i++); if (s.isScalar()) { return s.getDouble(0); } else { return s; } }
if (arr.rank() == 3 && arr.slice(i).isRowVector()) sb.append("["); sb.append(format(arr.dup('c').slice(i), offset, summarize)); } else if(arr.rank() <= 1 || arr.length() == 1) { sb.append(format(Nd4j.scalar(arr.getDouble(0)),offset,summarize)); sb.append(format(arr.slice(i), offset, summarize)); if (arr.rank() == 3 && arr.slice(i).isRowVector()) sb.append("]"); sb.append(newLineSep + " \n"); sb.append(StringUtils.repeat("\n", rank - 2)); sb.append(StringUtils.repeat(" ", offset)); } else { if (arr.rank() == 3 && arr.slice(i).isRowVector()) sb.append("]");
/** * Gets a copy of example i * * @param i the example to getFromOrigin * @return the example at i (one example) */ @Override public DataSet get(int i) { if (i > numExamples() || i < 0) throw new IllegalArgumentException("invalid example number"); if (i == 0 && numExamples() == 1) return this; if (getFeatureMatrix().rank() == 4) { //ensure rank is preserved INDArray slice = getFeatureMatrix().slice(i); return new DataSet(slice.reshape(ArrayUtil.combine(new long[] {1}, slice.shape())), getLabels().slice(i)); } return new DataSet(getFeatures().slice(i), getLabels().slice(i)); }
/** * Reverses the passed in matrix such that m[0] becomes m[m.length - 1] etc * * @param reverse the matrix to reverse * @return the reversed matrix */ @Override public INDArray rot(INDArray reverse) { INDArray ret = Nd4j.create(reverse.shape()); if (reverse.isVector()) return reverse(reverse); else { for (int i = 0; i < reverse.slices(); i++) { ret.putSlice(i, reverse(reverse.slice(i))); } } return ret.reshape(reverse.shape()); }
INDArray currSlice = this; for(int j = 0; j < curr.length; j++) { currSlice = currSlice.slice(curr[j]);
/** * Returns a column vector where each entry is the nth bilinear * product of the nth slices of the two tensors. */ @Override public INDArray bilinearProducts(INDArray curr, INDArray in) { assert curr.shape().length == 3; if (in.columns() != 1) { throw new AssertionError("Expected a column vector"); } if (in.rows() != curr.size(curr.shape().length - 1)) { throw new AssertionError("Number of rows in the input does not match number of columns in tensor"); } if (curr.size(curr.shape().length - 2) != curr.size(curr.shape().length - 1)) { throw new AssertionError("Can only perform this operation on a SimpleTensor with square slices"); } INDArray ret = Nd4j.create(curr.slices(), 1); INDArray inT = in.transpose(); for (int i = 0; i < curr.slices(); i++) { INDArray slice = curr.slice(i); INDArray inTTimesSlice = inT.mmul(slice); ret.putScalar(i, Nd4j.getBlasWrapper().dot(inTTimesSlice, in)); } return ret; }
public INDArray getGradient(INDArray gradient, int slice, int[] shape) { boolean historicalInitialized = false; INDArray sqrtHistory; if (this.historicalGradient == null) { this.historicalGradient = Nd4j.zeros(shape).add(epsilon); historicalInitialized = true; } else if (!this.historicalGradient.isVector() && this.historicalGradient.slice(slice).length() != gradient.length()) throw new IllegalArgumentException("Illegal gradient"); if (historicalGradient.isVector()) sqrtHistory = sqrt(historicalGradient); else sqrtHistory = !historicalInitialized ? sqrt(historicalGradient.slice(slice)) : historicalGradient; INDArray learningRates; try { learningRates = sqrtHistory.rdivi(learningRate); } catch (ArithmeticException ae) { learningRates = sqrtHistory.rdivi(learningRate + epsilon); } if (gradient.length() != learningRates.length()) gradient.muli(learningRates.slice(slice)); else gradient.muli(learningRates); this.historicalGradient.slice(slice).addi(gradient.mul(gradient)); numIterations++; //ensure no zeros return gradient; }
long sliceIdx = NDArrayMath.sliceOffsetForTensor(index, permuted, tensorShape); INDArray ret2 = permuted.slice(sliceIdx); if (dimension.length == tensorShape.length && ArrayUtil.prodLong(tensorShape) == ret2.length()) { if (dimension.length == 1 && ret2.isRowVector()) ret2 = ret2.slice((int) offset); if (dimension.length == 1 && ret2.isRowVector()) return ret2; ret2 = ret2.slice((int) offset); if (dimension.length == 1 && ret2.isRowVector()) return ret2; sliceIdx = NDArrayMath.sliceOffsetForTensor(index, ret2, tensorShape); sliceIdx -= ret2.slices() * (sliceIdx / ret2.slices()); ret2 = ret2.slice(sliceIdx);
@Override public INDArray put(INDArray indices, INDArray element) { INDArrayIndex[] realIndices = new INDArrayIndex[indices.rank()]; for(int i = 0; i < realIndices.length; i++) { realIndices[i] = new SpecifiedIndex(indices.slice(i).dup().data().asInt()); } return put(realIndices,element); }
INDArray currSlice = this; for(int j = 0; j < curr.length; j++) { currSlice = currSlice.slice(curr[j]);
/** * * @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; }
public AdaGrad createSubset(int index) { if (historicalGradient == null) this.historicalGradient = Nd4j.ones(shape); if (Shape.isMatrix(shape)) { AdaGrad a = new AdaGrad(1, historicalGradient.columns()); //grab only the needed elements INDArray slice = historicalGradient.slice(index).dup(); a.historicalGradient = slice; a.setLearningRate(learningRate); return a; } else { AdaGrad a = new AdaGrad(1, 1); //grab only the needed elements INDArray slice = Nd4j.scalar(historicalGradient.getDouble(index)); a.historicalGradient = slice; a.setLearningRate(learningRate); return a; } } }
@Override public Object next() { INDArray s = iterateOver.slice(i++); if (s.isScalar()) { return s.getDouble(0); } else { return s; } }
/** * * @param matrix * @return */ public static List<Point> toPoints(INDArray matrix) { List<Point> arr = new ArrayList<>(matrix.rows()); for (int i = 0; i < matrix.rows(); i++) { arr.add(new Point(matrix.slice(i))); } return arr; }
private void classify(INDArray output, Map<Integer, Iris> flowers) { for (int i = 0; i < output.rows(); i++) { Iris irs = flowers.get(i); // set the classification from the fitted results irs.setIrisClass(classifiers.get(maxIndex(getFloatArrayFromSlice(output.slice(i))))); } }
private Map<Integer, Iris> objectify(DataSet testData) { Map<Integer, Iris> iFlowers = new HashMap<>(); INDArray features = testData.getFeatureMatrix(); for (int i = 0; i < features.rows(); i++) { INDArray slice = features.slice(i); Iris irs = new Iris(slice.getDouble(0), slice.getDouble(1), slice.getDouble(2), slice.getDouble(3)); iFlowers.put(i, irs); } return iFlowers; }
/** * Returns whether the tree is consistent or not * @return whether the tree is consistent or not */ public boolean isCorrect() { for (int n = 0; n < size; n++) { INDArray point = data.slice(index[n]); if (!boundary.containsPoint(point)) return false; } return isLeaf() || northWest.isCorrect() && northEast.isCorrect() && southWest.isCorrect() && southEast.isCorrect(); }