/** * Concatenates two matrices horizontally. * Matrices must have identical * numbers of rows. * * @param arrs */ public INDArray hstack(INDArray... arrs) { return Nd4j.concat(1, arrs); }
/** * Concatenates two matrices vertically. Matrices must have identical * numbers of columns. * * @param arrs */ @Override public INDArray vstack(final INDArray... arrs) { return Nd4j.concat(0, arrs); }
static INDArray append(INDArray arr1, INDArray values, int dimension) { if(dimension == -1) { return Nd4j.toFlattened(arr1, values); } else { return Nd4j.concat(dimension, arr1, values); } }
/** * Get a view of the underlying array * relative to the size of the actual array. * (Sometimes there are overflows in the internals * but you want to use the internal INDArray for computing something * directly, this gives you the relevant subset that reflects the content of the list) * @return the view of the underlying ndarray relative to the collection's real size */ public INDArray array() { List<INDArray> retList = new ArrayList<>(list.size()); for(X x : list) { retList.add(x.array()); } return Nd4j.concat(0,retList.toArray(new INDArray[retList.size()])); }
return Nd4j.concat(0,resultList.toArray(new INDArray[resultList.size()]));
/** * Append the given * array with the specified value size * along a particular axis * @param arr the array to append to * @param padAmount the pad amount of the array to be returned * @param val the value to append * @param axis the axis to append to * @return the newly created array */ public static INDArray append(INDArray arr, int padAmount, double val, int axis) { if (padAmount == 0) return arr; long[] paShape = ArrayUtil.copy(arr.shape()); if (axis < 0) axis = axis + arr.shape().length; paShape[axis] = padAmount; INDArray concatArray = Nd4j.valueArrayOf(paShape, val); return Nd4j.concat(axis, arr, concatArray); }
/** * Append the given * array with the specified value size * along a particular axis * @param arr the array to append to * @param padAmount the pad amount of the array to be returned * @param val the value to append * @param axis the axis to append to * @return the newly created array */ public static INDArray prepend(INDArray arr, int padAmount, double val, int axis) { if (padAmount == 0) return arr; long[] paShape = ArrayUtil.copy(arr.shape()); if (axis < 0) axis = axis + arr.shape().length; paShape[axis] = padAmount; INDArray concatArr = Nd4j.valueArrayOf(paShape, val); return Nd4j.concat(axis, concatArr, arr); }
return Nd4j.concat(0,arrList.toArray(new INDArray[arrList.size()]));
return Nd4j.concat(0,arrList.toArray(new INDArray[arrList.size()]));
static INDArray insert(INDArray arr1, int index, INDArray values, int dimension) { if(dimension == -1) { INDArray flat1 = Nd4j.toFlattened(arr1); INDArray flatValues = Nd4j.toFlattened(values); INDArray firstSlice = flat1.get(NDArrayIndex.interval(0, index)); INDArray secondSlice = flat1.get(NDArrayIndex.interval(index, flat1.length())); return Nd4j.toFlattened(firstSlice, flatValues, secondSlice); } else { INDArray firstSlice = arr1.get(createIntervalOnDimension(dimension, false, 0, index)); INDArray secondSlice = arr1.get(createIntervalOnDimension(dimension, false, index, arr1.shape()[dimension])); return Nd4j.concat(dimension, firstSlice, values, secondSlice); } }
static INDArray delete(int dimension, INDArray arr1, int... interval) { int length = interval.length; int lastIntervalValue = interval[length - 1]; if(dimension == -1) { INDArray array1 = arr1.get(NDArrayIndex.interval(0, interval[0])); if(lastIntervalValue == arr1.length() - 1) { return Nd4j.toFlattened(array1); } else { INDArray array2 = arr1.get(NDArrayIndex.interval(lastIntervalValue + 1, arr1.length())); return Nd4j.toFlattened(array1, array2); } } else { INDArray array1 = arr1.get(createIntervalOnDimension(dimension, false, 0, interval[0])); if(lastIntervalValue == arr1.shape()[dimension] - 1) { return array1; } else { INDArray array2 = arr1.get(createIntervalOnDimension(dimension, false, lastIntervalValue + 1, arr1.shape()[dimension])); return Nd4j.concat(dimension, array1, array2); } } }
INDArray concatenatedAxisZero = Nd4j.concat(0, Nd4j.create(3, 2), Nd4j.create(5, 2)); print("Concatenated arrays on dimension zero", concatenatedAxisZero); INDArray concatenatedAxisOne = Nd4j.concat(1, Nd4j.create(3, 2), Nd4j.create(3, 5)); print("Concatenated arrays on dimension 1", concatenatedAxisOne);
/** * Concatenates two matrices horizontally. * Matrices must have identical * numbers of rows. * * @param arrs */ public INDArray hstack(INDArray... arrs) { return Nd4j.concat(1, arrs); }
/** * Concatenates two matrices vertically. Matrices must have identical * numbers of columns. * * @param arrs */ @Override public INDArray vstack(final INDArray... arrs) { return Nd4j.concat(0, arrs); }
private void diffuse(List<Pruning.Feature> featuresList) { INDArray ndDiffused = Nd4j.create(embedding.shape()); Nd4j.copy(embedding, ndDiffused); featuresList.addAll(featuresList); features = featuresList.toArray(new Pruning.Feature[0]); for (int i = features.length / 2; i < features.length; i++) { features[i] = new Pruning.Feature("diffuse", features[i]); } for (int diffIteration = 0; diffIteration < diffusionIterations; diffIteration++) { INDArray ndDiffusedTemp = Nd4j.create(embedding.shape()); nodeQueue.set(0); final ArrayList<Future<?>> futures = new ArrayList<>(); for (int i = 0; i < concurrency; i++) { futures.add(executorService.submit(new DiffusionTask(ndDiffused, ndDiffusedTemp))); } ParallelUtil.awaitTermination(futures); ndDiffused = ndDiffusedTemp; } embedding = Nd4j.concat(1, embedding, ndDiffused); }
/** * Append the given * array with the specified value size * along a particular axis * @param arr the array to append to * @param padAmount the pad amount of the array to be returned * @param val the value to append * @param axis the axis to append to * @return the newly created array */ public static INDArray append(INDArray arr, int padAmount, double val, int axis) { if (padAmount == 0) return arr; int[] paShape = ArrayUtil.copy(arr.shape()); if (axis < 0) axis = axis + arr.shape().length; paShape[axis] = padAmount; INDArray concatArray = Nd4j.valueArrayOf(paShape, val); return Nd4j.concat(axis, arr, concatArray); }
/** * Append the given * array with the specified value size * along a particular axis * @param arr the array to append to * @param padAmount the pad amount of the array to be returned * @param val the value to append * @param axis the axis to append to * @return the newly created array */ public static INDArray prepend(INDArray arr, int padAmount, double val, int axis) { if (padAmount == 0) return arr; int[] paShape = ArrayUtil.copy(arr.shape()); if (axis < 0) axis = axis + arr.shape().length; paShape[axis] = padAmount; INDArray concatArr = Nd4j.valueArrayOf(paShape, val); return Nd4j.concat(axis, concatArr, arr); }
/** * @param dimension the dimension along which the tensors will be joined * @param toConcat an array of IntegerTensor * @return an IntegerTensor with toConcat joined along existing dimension * <p> * e.g. A, B, C = IntegerTensor.ones(4, 2) * <p> * IntegerTensor.concat(0, A, B, C) gives IntegerTensor.ones(12, 2) */ static IntegerTensor concat(int dimension, IntegerTensor... toConcat) { INDArray[] concatAsINDArray = new INDArray[toConcat.length]; for (int i = 0; i < toConcat.length; i++) { concatAsINDArray[i] = Nd4jIntegerTensor.unsafeGetNd4J(toConcat[i]).dup(); if (concatAsINDArray[i].shape().length == 0) { concatAsINDArray[i] = concatAsINDArray[i].reshape(1); } } INDArray concat = Nd4j.concat(dimension, concatAsINDArray); return new Nd4jIntegerTensor(concat); }
/** * @param dimension the dimension along which the tensors will be joined * @param toConcat an array of DoubleTensor * @return a DoubleTensor with toConcat joined along an existing dimension * <p> * e.g. A, B, C = DoubleTensor.ones(4, 2) * <p> * DoubleTensor.concat(0, A, B, C) gives DoubleTensor.ones(12, 2) */ static DoubleTensor concat(int dimension, DoubleTensor... toConcat) { INDArray[] concatAsINDArray = new INDArray[toConcat.length]; for (int i = 0; i < toConcat.length; i++) { concatAsINDArray[i] = Nd4jDoubleTensor.unsafeGetNd4J(toConcat[i]).dup(); if (concatAsINDArray[i].shape().length == 0) { concatAsINDArray[i] = concatAsINDArray[i].reshape(1); } } INDArray concat = Nd4j.concat(dimension, concatAsINDArray); return new Nd4jDoubleTensor(concat); }