public static List<Pair<INDArray, String>> get4dPermutedWithShape(int seed, int... shape) { Nd4j.getRandom().setSeed(seed); int[] createdShape = {shape[1], shape[3], shape[2], shape[0]}; INDArray arr = Nd4j.rand(createdShape); INDArray permuted = arr.permute(3, 0, 2, 1); return Collections.singletonList(new Pair<>(permuted, "get4dPermutedWithShape(" + seed + "," + Arrays.toString(shape) + ").get(0)")); }
as2d = data.tensorAlongDimension(0, 1, 0); //Edge case: timeSeriesLength=1 } else { INDArray permuted = data.permute(0, 2, 1); //Permute, so we get correct order after reshaping as2d = permuted.reshape('f', shape[0] * shape[2], shape[1]);
public static List<Pair<INDArray, String>> get6dPermutedWithShape(int seed, int... shape) { Nd4j.getRandom().setSeed(seed); int[] createdShape = {shape[1], shape[4], shape[5], shape[3], shape[2], shape[0]}; INDArray arr = Nd4j.rand(createdShape); INDArray permuted = arr.permute(5, 0, 4, 3, 1, 2); return Collections.singletonList(new Pair<>(permuted, "get6dPermutedWithShape(" + seed + "," + Arrays.toString(shape) + ").get(0)")); }
public static INDArray reshapeTimeSeriesTo2d(INDArray labels) { int[] labelsShape = labels.shape(); INDArray labels2d; if (labelsShape[0] == 1) { labels2d = labels.tensorAlongDimension(0, 1, 2).permutei(1, 0); //Edge case: miniBatchSize==1 } else if (labelsShape[2] == 1) { labels2d = labels.tensorAlongDimension(0, 1, 0); //Edge case: timeSeriesLength=1 } else { labels2d = labels.permute(0, 2, 1); labels2d = labels2d.reshape('f', labelsShape[0] * labelsShape[2], labelsShape[1]); } return labels2d; }
public static List<Pair<INDArray, String>> get5dPermutedWithShape(int seed, int... shape) { Nd4j.getRandom().setSeed(seed); int[] createdShape = {shape[1], shape[4], shape[3], shape[2], shape[0]}; INDArray arr = Nd4j.rand(createdShape); INDArray permuted = arr.permute(4, 0, 3, 2, 1); return Collections.singletonList(new Pair<>(permuted, "get5dPermutedWithShape(" + seed + "," + Arrays.toString(shape) + ").get(0)")); }
public static INDArray reshape3dTo2d(INDArray in) { if (in.rank() != 3) throw new IllegalArgumentException("Invalid input: expect NDArray with rank 3"); int[] shape = in.shape(); if (shape[0] == 1) return in.tensorAlongDimension(0, 1, 2).permutei(1, 0); //Edge case: miniBatchSize==1 if (shape[2] == 1) return in.tensorAlongDimension(0, 1, 0); //Edge case: timeSeriesLength=1 INDArray permuted = in.permute(0, 2, 1); //Permute, so we get correct order after reshaping return permuted.reshape('f', shape[0] * shape[2], shape[1]); }
public static List<Pair<INDArray, String>> get3dPermutedWithShape(long seed, long... shape) { Nd4j.getRandom().setSeed(seed); long[] createdShape = {shape[1], shape[2], shape[0]}; int lencreatedShape = ArrayUtil.prod(createdShape); INDArray arr = Nd4j.linspace(1, lencreatedShape, lencreatedShape).reshape(createdShape); INDArray permuted = arr.permute(2, 0, 1); return Collections.singletonList(new Pair<>(permuted, "get3dPermutedWithShape(" + seed + "," + Arrays.toString(shape) + ").get(0)")); }
/** * Roll the specified axis backwards, * until it lies in a given position. * See numpy's rollaxis * @param a the array to roll * @param axis the axis to roll backwards * @param start the starting point * @return the rolled ndarray */ public static INDArray rollAxis(INDArray a, int axis, int start) { if (axis < 0) axis += a.rank(); if (start < 0) start += a.rank(); if (axis == start) return a; if (axis < start) start--; if (!(axis >= 0 && axis < a.rank())) throw new IllegalArgumentException("Axis must be >= 0 && < start"); if (!(start >= 0 && axis < a.rank() + 1)) throw new IllegalArgumentException("Axis must be >= 0 && < start"); List<Integer> range = new ArrayList<>(Ints.asList(ArrayUtil.range(0, a.rank()))); range.remove(axis); range.add(start, axis); int[] newRange = Ints.toArray(range); return a.permute(newRange); }
arr = (arr.permute(3, 2, 0, 1).dup('c')); initWith.associateArrayWithVariable(arr, vertexId);
public static Pair<INDArray, String> getPermutedWithShape(char ordering, long rows, long cols, long seed) { Nd4j.getRandom().setSeed(seed); long len = rows * cols; INDArray arr = Nd4j.linspace(1, len, len).reshape(cols, rows); return new Pair<>(arr.permute(1, 0), "getPermutedWithShape(" + rows + "," + cols + "," + seed + ")"); }
@Override public INDArray backprop(INDArray epsilons, int miniBatchSize) { INDArray epsilonsReshaped = super.backprop(epsilons, miniBatchSize); return epsilonsReshaped.permute(0, 3, 1, 2); }
protected INDArray toINDArrayBGR(BufferedImage image) { int height = image.getHeight(); int width = image.getWidth(); int bands = image.getSampleModel().getNumBands(); byte[] pixels = ((DataBufferByte)image.getRaster().getDataBuffer()).getData(); int[] shape = new int[]{height, width, bands}; INDArray ret2 = Nd4j.create(1, pixels.length); for(int i = 0; i < ret2.length(); i++) { ret2.putScalar(i, ((int)pixels[i])& 0xFF); } return ret2.reshape(shape).permute(2, 0, 1); }
public static List<Pair<INDArray, String>> get4dPermutedWithShape(int seed, int... shape) { Nd4j.getRandom().setSeed(seed); int[] createdShape = {shape[1], shape[3], shape[2], shape[0]}; INDArray arr = Nd4j.rand(createdShape); INDArray permuted = arr.permute(3, 0, 2, 1); return Collections.singletonList(new Pair<>(permuted, "get4dPermutedWithShape(" + seed + "," + Arrays.toString(shape) + ").get(0)")); }
public static List<Pair<INDArray, String>> get5dPermutedWithShape(int seed, int... shape) { Nd4j.getRandom().setSeed(seed); int[] createdShape = {shape[1], shape[4], shape[3], shape[2], shape[0]}; INDArray arr = Nd4j.rand(createdShape); INDArray permuted = arr.permute(4, 0, 3, 2, 1); return Collections.singletonList(new Pair<>(permuted, "get5dPermutedWithShape(" + seed + "," + Arrays.toString(shape) + ").get(0)")); }
protected INDArray toINDArrayBGR(BufferedImage image) { int height = image.getHeight(); int width = image.getWidth(); int bands = image.getSampleModel().getNumBands(); byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); int[] shape = new int[] {height, width, bands}; INDArray ret2 = Nd4j.create(1, pixels.length); for (int i = 0; i < ret2.length(); i++) { ret2.putScalar(i, ((int) pixels[i]) & 0xFF); } return ret2.reshape(shape).permute(2, 0, 1); }
public static List<Pair<INDArray, String>> get6dPermutedWithShape(int seed, int... shape) { Nd4j.getRandom().setSeed(seed); int[] createdShape = {shape[1], shape[4], shape[5], shape[3], shape[2], shape[0]}; INDArray arr = Nd4j.rand(createdShape); INDArray permuted = arr.permute(5, 0, 4, 3, 1, 2); return Collections.singletonList(new Pair<>(permuted, "get6dPermutedWithShape(" + seed + "," + Arrays.toString(shape) + ").get(0)")); }
public static List<Pair<INDArray, String>> get3dPermutedWithShape(int seed, int... shape) { Nd4j.getRandom().setSeed(seed); int[] createdShape = {shape[1], shape[2], shape[0]}; int lencreatedShape = ArrayUtil.prod(createdShape); INDArray arr = Nd4j.linspace(1, lencreatedShape, lencreatedShape).reshape(createdShape); INDArray permuted = arr.permute(2, 0, 1); return Collections.singletonList(new Pair<>(permuted, "get3dPermutedWithShape(" + seed + "," + Arrays.toString(shape) + ").get(0)")); }
public static Pair<INDArray, String> getPermutedWithShape(char ordering, int rows, int cols, int seed) { Nd4j.getRandom().setSeed(seed); int len = rows * cols; INDArray arr = Nd4j.linspace(1, len, len).reshape(cols, rows); return new Pair<>(arr.permute(1, 0), "getPermutedWithShape(" + rows + "," + cols + "," + seed + ")"); }