Java Code Examples for org.nd4j.linalg.util.ArrayUtil#prod()
The following examples show how to use
org.nd4j.linalg.util.ArrayUtil#prod() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OpExecutionerUtil.java From nd4j with Apache License 2.0 | 6 votes |
/**Choose tensor dimension for operations with 3 arguments: z=Op(x,y) or similar<br> * @see #chooseElementWiseTensorDimension(INDArray) */ public static int chooseElementWiseTensorDimension(INDArray x, INDArray y, INDArray z) { if (x.isVector()) return ArrayUtil.argMax(x.shape()); // FIXME: int cast int opAlongDimensionMinStride = (int) ArrayUtil.argMinOfMax(x.stride(), y.stride(), z.stride()); int opAlongDimensionMaxLength = ArrayUtil.argMax(x.shape()); //Edge case: shapes with 1s in them can have stride of 1 on the dimensions of length 1 if (opAlongDimensionMinStride == opAlongDimensionMaxLength || x.size((int) opAlongDimensionMinStride) == 1) return opAlongDimensionMaxLength; int nOpsAlongMinStride = ArrayUtil.prod(ArrayUtil.removeIndex(x.shape(), (int) opAlongDimensionMinStride)); int nOpsAlongMaxLength = ArrayUtil.prod(ArrayUtil.removeIndex(x.shape(), opAlongDimensionMaxLength)); if (nOpsAlongMinStride <= 10 * nOpsAlongMaxLength) return opAlongDimensionMinStride; else return opAlongDimensionMaxLength; }
Example 2
Source File: NDArrayCreationUtil.java From nd4j with Apache License 2.0 | 6 votes |
public static List<Pair<INDArray, String>> getSubMatricesWithShape(char ordering, long rows, long cols, long seed) { //Create 3 identical matrices. Could do get() on single original array, but in-place modifications on one //might mess up tests for another Nd4j.getRandom().setSeed(seed); long[] shape = new long[] {2 * rows + 4, 2 * cols + 4}; int len = ArrayUtil.prod(shape); INDArray orig = Nd4j.linspace(1, len, len).reshape(ordering, shape); INDArray first = orig.get(NDArrayIndex.interval(0, rows), NDArrayIndex.interval(0, cols)); Nd4j.getRandom().setSeed(seed); orig = Nd4j.linspace(1, len, len).reshape(shape); INDArray second = orig.get(NDArrayIndex.interval(3, rows + 3), NDArrayIndex.interval(3, cols + 3)); Nd4j.getRandom().setSeed(seed); orig = Nd4j.linspace(1, len, len).reshape(ordering, shape); INDArray third = orig.get(NDArrayIndex.interval(rows, 2 * rows), NDArrayIndex.interval(cols, 2 * cols)); String baseMsg = "getSubMatricesWithShape(" + rows + "," + cols + "," + seed + ")"; List<Pair<INDArray, String>> list = new ArrayList<>(3); list.add(new Pair<>(first, baseMsg + ".get(0)")); list.add(new Pair<>(second, baseMsg + ".get(1)")); list.add(new Pair<>(third, baseMsg + ".get(2)")); return list; }
Example 3
Source File: NDArrayIndex.java From nd4j with Apache License 2.0 | 6 votes |
/** * Compute the offset given an array of offsets. * The offset is computed(for both fortran an d c ordering) as: * sum from i to n - 1 o[i] * s[i] * where i is the index o is the offset and s is the stride * Notice the -1 at the end. * @param strides the strides to compute the offset for * @param offsets the offsets for each dimension * @return the offset that should be used for indexing */ public static long offset(long[] strides, long[] offsets) { int ret = 0; if (ArrayUtil.prod(offsets) == 1) { for (int i = 0; i < offsets.length; i++) { ret += offsets[i] * strides[i]; } } else { for (int i = 0; i < offsets.length; i++) { ret += offsets[i] * strides[i]; } } return ret; }
Example 4
Source File: NdIndexIterator.java From nd4j with Apache License 2.0 | 6 votes |
/** * Pass in the shape to iterate over. * Defaults to c ordering * @param shape the shape to iterate over */ public NdIndexIterator(char order, boolean cache, long... shape) { this.shape = ArrayUtil.copy(shape); this.length = ArrayUtil.prod(shape); this.order = order; this.cache = cache; if (this.cache) { LinearIndexLookup lookup = lookupMap.get(new Pair<>(shape, order)); if (lookup == null) { lookup = new LinearIndexLookup(shape, order); //warm up the cache for (int i = 0; i < length; i++) { lookup.lookup(i); } lookupMap.put(new Pair<>(shape, order), lookup); this.lookup = lookup; } else { this.lookup = lookupMap.get(new Pair<>(shape, order)); } } }
Example 5
Source File: SDVariable.java From nd4j with Apache License 2.0 | 6 votes |
/** * A getter for the allocated ndarray * with this {@link SDVariable}. * * This getter will lazy initialize an array if one is not found * based on the associated shape and {@link WeightInitScheme} * if neither are found, an {@link ND4JIllegalStateException} * is thrown. * * If a {@link DifferentialFunction} is defined, note that * its getArr() method is called instead. * @return the {@link INDArray} associated with this variable. */ public INDArray getArr() { if(sameDiff.arrayAlreadyExistsForVarName(getVarName())) return sameDiff.getArrForVarName(getVarName()); //initialize value if it's actually a scalar constant (zero or 1 typically...) if(getScalarValue() != null && ArrayUtil.prod(getShape()) == 1) { INDArray arr = Nd4j.valueArrayOf(getShape(), getScalarValue().doubleValue()); sameDiff.associateArrayWithVariable(arr,this); } else if(sameDiff.getShapeForVarName(getVarName()) == null) return null; else { INDArray newAlloc = getWeightInitScheme().create(sameDiff.getShapeForVarName(getVarName())); sameDiff.associateArrayWithVariable(newAlloc,this); } return sameDiff.getArrForVarName(getVarName()); }
Example 6
Source File: Shape.java From nd4j with Apache License 2.0 | 6 votes |
/** * Return the shape of the largest length array * based on the input * @param inputs the inputs to get the max shape for * @return the largest shape based on the inputs */ public static long[] getMaxShape(INDArray...inputs) { if(inputs == null) return null; else if(inputs.length < 2) return inputs[0].shape(); else { long[] currMax = inputs[0].shape(); for(int i = 1; i < inputs.length; i++) { if(inputs[i] == null) { continue; } if(ArrayUtil.prod(currMax) < inputs[i].length()) { currMax = inputs[i].shape(); } } return currMax; } }
Example 7
Source File: DefaultRandom.java From nd4j with Apache License 2.0 | 5 votes |
@Override public INDArray nextInt(long[] shape) { int length = ArrayUtil.prod(shape); INDArray ret = Nd4j.create(shape); DataBuffer data = ret.data(); for (int i = 0; i < length; i++) { data.put(i, nextInt()); } return ret; }
Example 8
Source File: DefaultRandom.java From nd4j with Apache License 2.0 | 5 votes |
@Override public INDArray nextInt(int n, long[] shape) { int length = ArrayUtil.prod(shape); INDArray ret = Nd4j.create(shape); DataBuffer data = ret.data(); for (int i = 0; i < length; i++) { data.put(i, nextInt(n)); } return ret; }
Example 9
Source File: NDArrayCreationUtil.java From nd4j with Apache License 2.0 | 5 votes |
public static List<Pair<INDArray, String>> get4dTensorAlongDimensionWithShape(int seed, int... shape) { List<Pair<INDArray, String>> list = new ArrayList<>(); String baseMsg = "get4dTensorAlongDimensionWithShape(" + seed + "," + Arrays.toString(shape) + ")"; //Create some 5d arrays and get subsets using 4d TAD on them //This is not an exhausive list of possible 4d arrays from 5d via TAD Nd4j.getRandom().setSeed(seed); int[] shape4d1 = {3, shape[0], shape[1], shape[2], shape[3]}; int len = ArrayUtil.prod(shape4d1); INDArray orig1a = Nd4j.linspace(1, len, len).reshape(ArrayUtil.toLongArray(shape4d1)); INDArray tad1a = orig1a.javaTensorAlongDimension(0, 1, 2, 3, 4); INDArray orig1b = Nd4j.linspace(1, len, len).reshape(ArrayUtil.toLongArray(shape4d1)); INDArray tad1b = orig1b.javaTensorAlongDimension(2, 1, 2, 3, 4); list.add(new Pair<>(tad1a, baseMsg + ".get(0)")); list.add(new Pair<>(tad1b, baseMsg + ".get(1)")); int[] shape4d2 = {3, shape[0], shape[1], shape[2], shape[3]}; int len2 = ArrayUtil.prod(shape4d2); INDArray orig2 = Nd4j.linspace(1, len2, len2).reshape(ArrayUtil.toLongArray(shape4d2)); INDArray tad2 = orig2.javaTensorAlongDimension(1, 3, 4, 2, 1); list.add(new Pair<>(tad2, baseMsg + ".get(2)")); int[] shape4d3 = {shape[0], shape[1], 3, shape[2], shape[3]}; int len3 = ArrayUtil.prod(shape4d3); INDArray orig3 = Nd4j.linspace(1, len3, len3).reshape(ArrayUtil.toLongArray(shape4d3)); INDArray tad3 = orig3.javaTensorAlongDimension(1, 4, 1, 3, 0); list.add(new Pair<>(tad3, baseMsg + ".get(3)")); int[] shape4d4 = {shape[0], shape[1], shape[2], shape[3], 3}; int len4 = ArrayUtil.prod(shape4d4); INDArray orig4 = Nd4j.linspace(1, len4, len4).reshape(ArrayUtil.toLongArray(shape4d4)); INDArray tad4 = orig4.javaTensorAlongDimension(1, 2, 0, 3, 1); list.add(new Pair<>(tad4, baseMsg + ".get(4)")); return list; }
Example 10
Source File: BaseTransformOp.java From nd4j with Apache License 2.0 | 5 votes |
public BaseTransformOp(SameDiff sameDiff, SDVariable i_v1, SDVariable i_v2, boolean inPlace) { super(sameDiff,inPlace,new Object[] {i_v2}); if (i_v1 != null && i_v2 != null) { f().validateDifferentialFunctionsameDiff(i_v1); f().validateDifferentialFunctionsameDiff(i_v2); this.sameDiff = sameDiff; this.inPlace = inPlace; this.xVertexId = i_v1.getVarName(); this.yVertexId = i_v2.getVarName(); sameDiff.addArgsFor(new SDVariable[]{i_v1,i_v2},this); if(Shape.isPlaceholderShape(i_v1.getShape())) { sameDiff.addPropertyToResolve(this,i_v1.getVarName()); } if(Shape.isPlaceholderShape(i_v2.getShape())) { sameDiff.addPropertyToResolve(this,i_v2.getVarName()); } if(i_v1.getShape() != null) this.n = ArrayUtil.prod(i_v1.getShape()); } else { throw new IllegalArgumentException("Input not null variables."); } }
Example 11
Source File: BaseTransformOp.java From nd4j with Apache License 2.0 | 5 votes |
public BaseTransformOp(SameDiff sameDiff, SDVariable i_v1, SDVariable i_v2, Object[] extraArgs) { super(sameDiff,extraArgs); if (i_v1 != null && i_v2 != null) { f().validateDifferentialFunctionsameDiff(i_v1); f().validateDifferentialFunctionsameDiff(i_v2); this.sameDiff = sameDiff; this.xVertexId = i_v1.getVarName(); this.yVertexId = i_v2.getVarName(); sameDiff.addArgsFor(new SDVariable[]{i_v1,i_v2},this); if(i_v1.getShape() != null) this.n = ArrayUtil.prod(i_v1.getShape()); if(Shape.isPlaceholderShape(i_v1.getShape())) { sameDiff.addPropertyToResolve(this,i_v1.getVarName()); } if(Shape.isPlaceholderShape(i_v2.getShape())) { sameDiff.addPropertyToResolve(this,i_v2.getVarName()); } } else { throw new IllegalArgumentException("Input not null variables."); } }
Example 12
Source File: NDArrayCreationUtil.java From nd4j with Apache License 2.0 | 5 votes |
public static List<Pair<INDArray, String>> get3dTensorAlongDimensionWithShape(long seed, long... shape) { List<Pair<INDArray, String>> list = new ArrayList<>(); String baseMsg = "get3dTensorAlongDimensionWithShape(" + seed + "," + Arrays.toString(shape) + ")"; //Create some 4d arrays and get subsets using 3d TAD on them //This is not an exhaustive list of possible 3d arrays from 4d via TAD Nd4j.getRandom().setSeed(seed); // int[] shape4d1 = {shape[2],shape[1],shape[0],3}; val shape4d1 = new long[]{shape[0], shape[1], shape[2], 3}; int lenshape4d1 = ArrayUtil.prod(shape4d1); INDArray orig1a = Nd4j.linspace(1, lenshape4d1, lenshape4d1).reshape(shape4d1); INDArray tad1a = orig1a.javaTensorAlongDimension(0, 0, 1, 2); INDArray orig1b = Nd4j.linspace(1, lenshape4d1, lenshape4d1).reshape(shape4d1); INDArray tad1b = orig1b.javaTensorAlongDimension(1, 0, 1, 2); list.add(new Pair<>(tad1a, baseMsg + ".get(0)")); list.add(new Pair<>(tad1b, baseMsg + ".get(1)")); long[] shape4d2 = {3, shape[0], shape[1], shape[2]}; int lenshape4d2 = ArrayUtil.prod(shape4d2); INDArray orig2 = Nd4j.linspace(1, lenshape4d2, lenshape4d2).reshape(shape4d2); INDArray tad2 = orig2.javaTensorAlongDimension(1, 1, 2, 3); list.add(new Pair<>(tad2, baseMsg + ".get(2)")); long[] shape4d3 = {shape[0], shape[1], 3, shape[2]}; int lenshape4d3 = ArrayUtil.prod(shape4d3); INDArray orig3 = Nd4j.linspace(1, lenshape4d3, lenshape4d3).reshape(shape4d3); INDArray tad3 = orig3.javaTensorAlongDimension(1, 1, 3, 0); list.add(new Pair<>(tad3, baseMsg + ".get(3)")); long[] shape4d4 = {shape[0], 3, shape[1], shape[2]}; int lenshape4d4 = ArrayUtil.prod(shape4d4); INDArray orig4 = Nd4j.linspace(1, lenshape4d4, lenshape4d4).reshape(shape4d4); INDArray tad4 = orig4.javaTensorAlongDimension(1, 2, 0, 3); list.add(new Pair<>(tad4, baseMsg + ".get(4)")); return list; }
Example 13
Source File: BaseOp.java From nd4j with Apache License 2.0 | 5 votes |
@Override public long n() { if(n == 0) { if(arg() != null) this.n = ArrayUtil.prod(arg().getShape()); } return n; }
Example 14
Source File: BiasAdd.java From nd4j with Apache License 2.0 | 5 votes |
@Override public List<long[]> calculateOutputShape() { val args = args(); for(int i = 0; i < args.length; i++) if(args[i].getShape() == null) return Collections.emptyList(); val firstShape = ArrayUtil.prod(args[0].getShape()); val secondShape = ArrayUtil.prod(args[1].getShape()); if(firstShape > secondShape) return Arrays.asList(args[0].getShape()); else return Arrays.asList(args[1].getShape()); }
Example 15
Source File: NDArrayCreationUtil.java From nd4j with Apache License 2.0 | 5 votes |
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)")); }
Example 16
Source File: BaseNDArray.java From nd4j with Apache License 2.0 | 5 votes |
@Override public INDArray repeat(int dimension, long... repeats) { Nd4j.getCompressor().autoDecompress(this); if (dimension < 0) dimension += rank(); if (repeats.length < rank()) { if (dimension > 0) repeats = Longs.concat(ArrayUtil.nTimes((long) rank() - repeats.length, 1), repeats); //append rather than prepend for dimension == 0 else repeats = Longs.concat(repeats, ArrayUtil.nTimes((long) rank() - repeats.length, 1)); } long[] newShape = new long[rank()]; for (int i = 0; i < newShape.length; i++) newShape[i] = size(i) * repeats[i]; INDArray ret = Nd4j.create(newShape); //number of times to repeat each value long repeatDelta = ArrayUtil.prod(newShape) / length(); for (int i = 0; i < tensorssAlongDimension(dimension); i++) { INDArray thisTensor = tensorAlongDimension(i, dimension); INDArray retTensor = ret.tensorAlongDimension(i, dimension); int retIdx = 0; for (int k = 0; k < thisTensor.length(); k++) { for (int j = 0; j < repeatDelta; j++) { retTensor.putScalar(retIdx++, thisTensor.getDouble(k)); } } } return ret; }
Example 17
Source File: CudaFloatDataBufferTest.java From nd4j with Apache License 2.0 | 5 votes |
@Test public void testToFlattenedWithOrder(){ int[] firstShape = {10,3}; int firstLen = ArrayUtil.prod(firstShape); int[] secondShape = {2,7}; int secondLen = ArrayUtil.prod(secondShape); int[] thirdShape = {3,3}; int thirdLen = ArrayUtil.prod(thirdShape); INDArray firstC = Nd4j.linspace(1,firstLen,firstLen).reshape('c',firstShape); INDArray firstF = Nd4j.create(firstShape,'f').assign(firstC); INDArray secondC = Nd4j.linspace(1,secondLen,secondLen).reshape('c',secondShape); INDArray secondF = Nd4j.create(secondShape,'f').assign(secondC); INDArray thirdC = Nd4j.linspace(1,thirdLen,thirdLen).reshape('c',thirdShape); INDArray thirdF = Nd4j.create(thirdShape,'f').assign(thirdC); assertEquals(firstC,firstF); assertEquals(secondC,secondF); assertEquals(thirdC,thirdF); INDArray cc = Nd4j.toFlattened('c',firstC,secondC,thirdC); INDArray cf = Nd4j.toFlattened('c',firstF,secondF,thirdF); assertEquals(cc,cf); INDArray cmixed = Nd4j.toFlattened('c',firstC,secondF,thirdF); assertEquals(cc,cmixed); INDArray fc = Nd4j.toFlattened('f',firstC,secondC,thirdC); assertNotEquals(cc,fc); INDArray ff = Nd4j.toFlattened('f',firstF,secondF,thirdF); assertEquals(fc,ff); INDArray fmixed = Nd4j.toFlattened('f',firstC,secondF,thirdF); assertEquals(fc,fmixed); }
Example 18
Source File: NDArrayCreationUtil.java From nd4j with Apache License 2.0 | 4 votes |
public static List<Pair<INDArray, String>> get4dSubArraysWithShape(int seed, int... shape) { List<Pair<INDArray, String>> list = new ArrayList<>(); String baseMsg = "get4dSubArraysWithShape(" + seed + "," + Arrays.toString(shape) + ")"; //Create and return various sub arrays: Nd4j.getRandom().setSeed(seed); int[] newShape1 = Arrays.copyOf(shape, shape.length); newShape1[0] += 5; int len = ArrayUtil.prod(newShape1); INDArray temp1 = Nd4j.linspace(1, len, len).reshape(ArrayUtil.toLongArray(newShape1)); INDArray subset1 = temp1.get(NDArrayIndex.interval(2, shape[0] + 2), NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.all()); list.add(new Pair<>(subset1, baseMsg + ".get(0)")); int[] newShape2 = Arrays.copyOf(shape, shape.length); newShape2[1] += 5; int len2 = ArrayUtil.prod(newShape2); INDArray temp2 = Nd4j.linspace(1, len2, len2).reshape(ArrayUtil.toLongArray(newShape2)); INDArray subset2 = temp2.get(NDArrayIndex.all(), NDArrayIndex.interval(3, shape[1] + 3), NDArrayIndex.all(), NDArrayIndex.all()); list.add(new Pair<>(subset2, baseMsg + ".get(1)")); int[] newShape3 = Arrays.copyOf(shape, shape.length); newShape3[2] += 5; int len3 = ArrayUtil.prod(newShape3); INDArray temp3 = Nd4j.linspace(1, len3, len3).reshape(ArrayUtil.toLongArray(newShape3)); INDArray subset3 = temp3.get(NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.interval(4, shape[2] + 4), NDArrayIndex.all()); list.add(new Pair<>(subset3, baseMsg + ".get(2)")); int[] newShape4 = Arrays.copyOf(shape, shape.length); newShape4[3] += 5; int len4 = ArrayUtil.prod(newShape4); INDArray temp4 = Nd4j.linspace(1, len4, len4).reshape(ArrayUtil.toLongArray(newShape4)); INDArray subset4 = temp4.get(NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.interval(3, shape[3] + 3)); list.add(new Pair<>(subset4, baseMsg + ".get(3)")); int[] newShape5 = Arrays.copyOf(shape, shape.length); newShape5[0] += 5; newShape5[1] += 5; newShape5[2] += 5; newShape5[3] += 5; int len5 = ArrayUtil.prod(newShape5); INDArray temp5 = Nd4j.linspace(1, len5, len5).reshape(ArrayUtil.toLongArray(newShape5)); INDArray subset5 = temp5.get(NDArrayIndex.interval(4, shape[0] + 4), NDArrayIndex.interval(3, shape[1] + 3), NDArrayIndex.interval(2, shape[2] + 2), NDArrayIndex.interval(1, shape[3] + 1)); list.add(new Pair<>(subset5, baseMsg + ".get(4)")); return list; }
Example 19
Source File: BaseNDArray.java From nd4j with Apache License 2.0 | 4 votes |
public BaseNDArray(long[] shape, long[] stride, long offset) { this(new float[ArrayUtil.prod(shape)], shape, stride, offset, Nd4j.order()); }
Example 20
Source File: BaseComplexNDArray.java From nd4j with Apache License 2.0 | 2 votes |
/** * Construct an ndarray of the specified shape * with an empty data array * * @param shape the shape of the ndarray * @param stride the stride of the ndarray * @param offset the desired offset * @param ordering the ordering for the ndarray */ public BaseComplexNDArray(int[] shape, int[] stride, long offset, char ordering) { this(new float[ArrayUtil.prod(shape) * 2], shape, stride, offset); this.shapeInformation = Shape.createShapeInformation(shape, stride, offset, stride[stride.length - 1], ordering); }