Refine search
/** * Iterate over 2 * coordinate spaces given 2 arrays * @param arr the first array * @param arr2 the second array * @param coordinateFunction the coordinate function to use * */ public static void iterate(INDArray arr, INDArray arr2, CoordinateFunction coordinateFunction) { Shape.iterate(0, arr.rank(), arr.shape(), new long[arr.rank()], 0, arr2.rank(), arr2.shape(), new long[arr2.rank()], coordinateFunction); }
/** * Return the number of vectors for an array * the number of vectors for an array * @param arr the array to calculate the number of vectors for * @return the number of vectors for the given array */ public static long numVectors(INDArray arr) { if (arr.rank() == 1) return 1; else if (arr.rank() == 2) return arr.size(0); else { int prod = 1; for (int i = 0; i < arr.rank() - 1; i++) { prod *= arr.size(i); } return prod; } }
long[] shape = arrays[0].shape(); INDArray[] temp = new INDArray[arrays.length]; boolean hasMasks = false; for (int i = 0; i < arrays.length; i++) { nExamples += arrays[i].size(0); long[] thisShape = arrays[i].shape(); if (thisShape.length != 4) { throw new IllegalStateException("Cannot merge 4d arrays with non 4d arrays"); if (masks != null && masks[i] != null && masks[i] != null) { hasMasks = true; if (masks[i].rank() != 2) { throw new UnsupportedOperationException("Cannot merged 4d arrays with masks that are not rank 2." + " Got mask array with rank: " + masks[i].rank()); INDArray outMask = null; if (hasMasks) { outMask = DataSetUtil.mergePerOutputMasks2d(out.shape(), arrays, masks);
val shape = arr.shape(); if (idx instanceof NDArrayIndexAll) { encounteredAll = true; if (i < arr.rank() && arr.size(i) == 1) oneDimensionWithAllEncountered.add(i); if (Shape.isRowVectorShape(arr.shape())) accumShape.add(0, 1L); else accumStrides.addAll(pointStrides); while (accumOffsets.size() < accumShape.size()) { if (Shape.isRowVectorShape(arr.shape())) accumOffsets.add(0, 0L); else this.offset = 0; if (numIntervals > 0 && arr.rank() > 2) { if (encounteredAll && arr.size(0) != 1 || indexes[0] instanceof PointIndex)
/** * This method stacks vertically examples with the same shape, increasing result dimensionality. I.e. if you provide bunch of 3D tensors, output will be 4D tensor. Alignment is always applied to axis 0. * * @return */ public static INDArray pile(INDArray... arrays) { // if we have vectors as input, it's just vstack use case if (arrays[0].isRowVector() && arrays[0].rank() == 2) { return Nd4j.vstack(arrays); } long[] shape = arrays[0].shape(); long[] newShape = ArrayUtils.add(shape, 0, 1); boolean shouldReshape = true; if (arrays[0].size(0) == 1) shouldReshape = false; List<INDArray> reshaped = new ArrayList<>(); for(INDArray array: arrays) { if (!shouldReshape) reshaped.add(array); else reshaped.add(array.reshape(array.ordering(), newShape)); } return Nd4j.vstack(reshaped); }
/** * Iterate over 2 * coordinate spaces given 2 arrays * @param arr the first array * @param coordinateFunction the coordinate function to use * */ public static void iterate(INDArray arr, CoordinateFunction coordinateFunction) { Shape.iterate(0, arr.rank(), arr.shape(), new long[arr.rank()], coordinateFunction); }
/** * The number of vectors * in each slice of an ndarray. * @param arr the array to * get the number * of vectors per slice for * @return the number of vectors per slice */ public static long matricesPerSlice(INDArray arr) { if (arr.rank() == 3) { return 1; } else if (arr.rank() > 3) { int ret = 1; for (int i = 1; i < arr.rank() - 2; i++) { ret *= arr.size(i); } return ret; } return arr.size(-2); }
System.out.println("Num. Rows: " + myArray.rows()); System.out.println("Num. Columns: " + myArray.columns()); System.out.println("Num. Dimensions: " + myArray.rank()); //2 dimensions -> rank 2 System.out.println("Shape: " + Arrays.toString(myArray.shape())); //[3,5] -> 3 rows, 5 columns System.out.println("Length: " + myArray.length()); // 3 rows * 5 columns = 15 total elements System.out.println("size(0) == nRows: " + myArray.size(0)); //Also equivalent to: .shape()[0] System.out.println("size(1) == nCols: " + myArray.size(1)); //Also equivalent to: .shape()[1] System.out.println("Is a vector: " + myArray.isVector()); System.out.println("Is a scalar: " + myArray.isScalar());
/** * Expand the array dimensions. * This is equivalent to * adding a new axis dimension * @param input the input array * @param dimension the dimension to add the * new axis at * @return the array with the new axis dimension */ public static INDArray expandDims(INDArray input, int dimension) { if (dimension < 0) dimension += input.rank(); long[] shape = input.shape(); long[] indexes = new long[input.rank() + 1]; for (int i = 0; i < indexes.length; i++) indexes[i] = i < dimension ? shape[i] : i == dimension ? 1 : shape[i - 1]; return input.reshape(input.ordering(), indexes); }
/** * The number of vectors * in each slice of an ndarray. * @param arr the array to * get the number * of vectors per slice for * @param rank the dimensions to get the number of vectors per slice for * @return the number of vectors per slice */ public static long vectorsPerSlice(INDArray arr, int... rank) { if (arr.rank() > 2) { return arr.size(-2) * arr.size(-1); } return arr.size(-1); }
newStrides.add(0L); } else { newShape.add(arr.size(currDimension)); newStrides.add(arr.size(currDimension)); currDimension++; while (currDimension < arr.rank()) { newShape.add((long) currDimension); newStrides.add((long) currDimension); long[] newShape = Longs.concat(ArrayUtil.toLongArray(ArrayUtil.nTimes(numNewAxes, 1)), arr.shape()); long[] newStrides = Longs.concat(new long[numNewAxes], arr.stride()); arr.setShape(newShape);
/** * This method produces concatenated array, that consist from tensors, fetched from source array, against some * dimension and specified indexes. * The concatenated arrays are placed in the specified array. * * @param source source tensor * @param destination Destination tensor (result will be placed here) * @param sourceDimension dimension of source tensor * @param indexes indexes from source array * @return Destination array with specified tensors */ public static INDArray pullRows(INDArray source, INDArray destination, int sourceDimension, int[] indexes){ if (sourceDimension >= source.rank()) throw new IllegalStateException("Source dimension can't be higher the rank of source tensor"); if (indexes == null || indexes.length == 0) throw new IllegalStateException("Indexes shouldn't be empty"); for (int idx : indexes) { if (idx < 0 || idx >= source.shape()[source.rank() - sourceDimension - 1]) { throw new IllegalStateException( "Index can't be < 0 and >= " + source.shape()[source.rank() - sourceDimension - 1]); } } INDArray ret = INSTANCE.pullRows(source, destination, sourceDimension, indexes); logCreationIfNecessary(ret); return ret; }
private void validateData(INDArray label, INDArray labelMask) { if (label.rank() != 3) { throw new IllegalArgumentException( "UnderSamplingByMaskingPreProcessor can only be applied to a time series dataset"); } if (label.size(1) > 2) { throw new IllegalArgumentException( "UnderSamplingByMaskingPreProcessor can only be applied to labels that represent binary classes. Label size was found to be " + label.size(1) + ".Expecting size=1 or size=2."); } if (label.size(1) == 2) { //check if label is of size one hot if (!label.sum(1).mul(labelMask).equals(labelMask)) { throw new IllegalArgumentException("Labels of size minibatchx2xtimesteps are expected to be one hot." + label.toString() + "\n is not one-hot"); } } }
/** * This method produces concatenated array, * that consist from tensors, * fetched from source array, * against some dimension and specified indexes * * @param source source tensor * @param sourceDimension dimension of source tensor * @param indexes indexes from source array * @return */ public static INDArray pullRows(INDArray source, int sourceDimension, int[] indexes, char order) { if (sourceDimension >= source.rank()) throw new IllegalStateException("Source dimension can't be higher the rank of source tensor"); if (indexes == null || indexes.length == 0) throw new IllegalStateException("Indexes shouldn't be empty"); if (order != 'c' && order != 'f' && order != 'a') throw new IllegalStateException("Unknown order being passed in [" + order + "]"); for (int idx : indexes) { if (idx < 0 || idx >= source.shape()[source.rank() - sourceDimension - 1]) { throw new IllegalStateException( "Index can't be < 0 and >= " + source.shape()[source.rank() - sourceDimension - 1]); } } INDArray ret = INSTANCE.pullRows(source, sourceDimension, indexes, order); logCreationIfNecessary(ret); return ret; }
/** * The number of vectors * in each slice of an ndarray. * @param arr the array to * get the number * of vectors per slice for * @return the number of vectors per slice */ public static long vectorsPerSlice(INDArray arr) { if (arr.rank() > 2) { return ArrayUtil.prodLong(new long[] {arr.size(-1), arr.size(-2)}); } return arr.slices(); }
this.shapes = arr.shape(); this.strides = arr.stride(); this.offsets = new long[arr.rank()]; return true; } else if (indexes[0] instanceof PointIndex && indexes[1] instanceof NDArrayIndexAll) { this.offsets = new long[arr.rank()]; if(arr.isRowVector()) this.offset = indexes[0].offset() * strides[1]; if (numSpecified > 0 && interval < 1 && newAxis < 1 && numAll > 0 && pointIndex < 1 && arr.rank() == 2) { shapes = new long[arr.rank()]; strides = new long[arr.rank()]; offsets = new long[arr.rank()]; offset = 0; boolean allSpecified = true; for (int i = 0; i < arr.rank(); i++) { if (indexes[i] instanceof SpecifiedIndex) { SpecifiedIndex specifiedIndex = (SpecifiedIndex) indexes[i]; if (specifiedIndex.getIndexes().length >= arr.rank()) return false; shapes[i] = indexes[i].length(); int minDimensions = Math.max(arr.rank() - pointIndex, 2); long[] shape = new long[minDimensions]; Arrays.fill(shape, 1); int minDimensions = Math.max(arr.rank(), 2);
/** * Asserts matrix multiply rules (columns of left == rows of right or rows of left == columns of right) * * @param nd1 the left ndarray * @param nd2 the right ndarray */ public static void assertMultiplies(INDArray nd1, INDArray nd2) { if (nd1.rank() == 2 && nd2.rank() == 2 && nd1.columns() == nd2.rows()) { return; } // 1D edge case if (nd1.rank() == 2 && nd2.rank() == 1 && nd1.columns() == nd2.length()) return; throw new ND4JIllegalStateException("Cannot execute matrix multiplication: " + Arrays.toString(nd1.shape()) + "x" + Arrays.toString(nd2.shape()) + (nd1.rank() != 2 || nd2.rank() != 2 ? ": inputs are not matrices" : ": Column of left array " + nd1.columns() + " != rows of right " + nd2.rows())); }
int validationLength = Math.min(axes[0].length, axes[1].length); for (int i = 0; i < validationLength; i++) { if (a.size(axes[0][i]) != b.size(axes[1][i])) throw new IllegalArgumentException("Size of the given axes at each dimension must be the same size."); if (axes[0][i] < 0) axes[0][i] += a.rank(); if (axes[1][i] < 0) axes[1][i] += b.rank(); for (int i = 0; i < a.rank(); i++) { if (!Ints.contains(axes[0], i)) listA.add(i); for (int i = 0; i < b.rank(); i++) { if (!Ints.contains(axes[1], i)) listB.add(i); int aLength = Math.min(a.rank(), axes[0].length); for (int i = 0; i < aLength; i++) { n2 *= a.size(axes[0][i]); int bNax = Math.min(b.rank(), axes[1].length); for (int i = 0; i < bNax; i++) { n3 *= b.size(axes[1][i]);
if (input.rank() != 2 || input.columns() != W.rows()) { if (input.rank() != 2) { throw new DL4JInvalidInputException("Input that is not a matrix; expected matrix (rank 2), got rank " + input.rank() + " array with shape " + Arrays.toString(input.shape()) + ". Missing preprocessor or wrong input type? " + layerId()); "Input cardinality (" + input.columns() + " columns; shape = " + Arrays.toString(input.shape()) + ") is invalid: does not match layer input cardinality (layer # inputs = " + W.size(0) + ") " + layerId());