/** * 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); }
/** * * @param thisnD * @param filePath */ public static void write(INDArray thisnD, String filePath) { //TO DO: Add precision support in toString //TO DO: Write to file one line at time String lineOne = "{\n"; String lineTwo = "\"filefrom\": \"dl4j\",\n"; String lineThree = "\"ordering\": \"" + thisnD.ordering() + "\",\n"; String lineFour = "\"shape\":\t" + java.util.Arrays.toString(thisnD.shape()) + ",\n"; String lineFive = "\"data\":\n"; String fileData = thisnD.toString(); String fileEnd = "\n}\n"; String fileBegin = lineOne + lineTwo + lineThree + lineFour + lineFive; try { FileUtils.writeStringToFile(new File(filePath), fileBegin + fileData + fileEnd); } catch (IOException e) { throw new RuntimeException("Error writing output", e); } } }
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"); throw new IllegalStateException( "Cannot merge 4d arrays with different shape (other than # examples): " + " data[0].shape = " + Arrays.toString(shape) + ", data[" + i + "].shape = " + Arrays.toString(thisShape)); 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);
/** * Merge the specified features and mask arrays (i.e., concatenate the examples) * * @param featuresToMerge Features to merge * @param featureMasksToMerge Mask arrays to merge. May be null * @return Merged features and mask. Mask may be null */ public static Pair<INDArray, INDArray> mergeFeatures(@NonNull INDArray[] featuresToMerge, INDArray[] featureMasksToMerge) { int rankFeatures = featuresToMerge[0].rank(); switch (rankFeatures) { case 2: return DataSetUtil.merge2d(featuresToMerge, featureMasksToMerge); case 3: return DataSetUtil.mergeTimeSeries(featuresToMerge, featureMasksToMerge); case 4: return DataSetUtil.merge4d(featuresToMerge, featureMasksToMerge); default: throw new IllegalStateException("Cannot merge examples: features rank must be in range 2 to 4" + " inclusive. First example features shape: " + Arrays.toString(featureMasksToMerge[0].shape())); } }
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) + ") "); + " Got mask array with shape " + Arrays.toString(mask.shape()) + "; per-output masking is not " + "supported for LossCosineProximity");
@Override public GradientUpdater instantiate(INDArray viewArray, boolean initializeViewArray) { AdamUpdater u = new AdamUpdater(this); long[] gradientShape = viewArray.shape(); gradientShape = Arrays.copyOf(gradientShape, gradientShape.length); gradientShape[1] /= 2; u.setStateViewArray(viewArray, gradientShape, viewArray.ordering(), initializeViewArray); return u; }
@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) + ") "); } final INDArray grad = Nd4j.ones(labels.shape()); calculate(labels, preOutput, activationFn, mask, null, grad); return grad; }
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);
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)
/** * 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); }
@Override public INDArray backprop(INDArray output, int miniBatchSize) { //Input: 4d epsilons (CNN) //Output: 3d epsilons (RNN) if (output.ordering() == 'f') output = output.dup('c'); int[] shape = output.shape(); //First: reshape 4d to 2d INDArray twod = output.reshape('c', output.size(0), ArrayUtil.prod(output.shape()) / output.size(0)); //Second: reshape 2d to 4d INDArray reshaped = twod.dup('f').reshape('f', miniBatchSize, shape[0] / miniBatchSize, product); return reshaped.permute(0, 2, 1); }
/** * 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); }
/** * 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())); }
private static String writeStringForArray(INDArray write, String format) { if (format.isEmpty()) format = "0.000000000000000000E0"; String lineOne = "{\n"; String lineTwo = "\"filefrom\": \"dl4j\",\n"; String lineThree = "\"ordering\": \"" + write.ordering() + "\",\n"; String lineFour = "\"shape\":\t" + java.util.Arrays.toString(write.shape()) + ",\n"; String lineFive = "\"data\":\n"; String fileData = new NDArrayStrings(",", format).format(write, false); String fileEnd = "\n}\n"; String fileBegin = lineOne + lineTwo + lineThree + lineFour + lineFive; String fileContents = fileBegin + fileData + fileEnd; return fileContents; }
@Override public GradientUpdater instantiate(INDArray viewArray, boolean initializeViewArray) { AdaDeltaUpdater u = new AdaDeltaUpdater(this); long[] gradientShape = viewArray.shape(); gradientShape = Arrays.copyOf(gradientShape, gradientShape.length); gradientShape[1] /= 2; u.setStateViewArray(viewArray, gradientShape, viewArray.ordering(), initializeViewArray); return u; }
@Override public Pair<Double, INDArray> computeGradientAndScore(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask, boolean average) { final INDArray scoreArr = Nd4j.create(labels.size(0), 1); final INDArray grad = Nd4j.ones(labels.shape()); calculate(labels, preOutput, activationFn, mask, scoreArr, grad); double score = scoreArr.sumNumber().doubleValue(); if (average) score /= scoreArr.size(0); return new Pair<>(score, grad); }
/** * 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); }