/** * 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 */ public BaseComplexNDArray(int[] shape, int[] stride, long offset) { this(new float[ArrayUtil.prod(shape) * 2], shape, stride, offset); }
public FlatIterator(int[] shape) { this.shape = shape; this.currentCoord = new int[shape.length]; length = ArrayUtil.prod(shape); }
public BaseComplexNDArray(long[] shape, long[] stride, long offset) { this(new float[ArrayUtil.prod(shape) * 2], shape, stride, offset); }
/** * Create a complex ndarray with the given complex doubles. * Note that this maybe an easier setup than the new float * * @param newData the new data for this array * @param shape the shape of the ndarray */ public BaseComplexNDArray(IComplexNumber[] newData, int[] shape) { super(new float[ArrayUtil.prod(shape) * 2]); /* init(shape); for (int i = 0; i < length; i++) putScalar(i, newData[i].asDouble()); */ throw new UnsupportedOperationException(); }
/** * Computes the tensors per slice * given a tensor shape and array * @param arr the array to get the tensors per slice for * @param tensorShape the desired tensor shape * @return the tensors per slice of an ndarray */ public static long tensorsPerSlice(INDArray arr, int[] tensorShape) { return lengthPerSlice(arr) / ArrayUtil.prod(tensorShape); }
/** * 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 */ public BaseNDArray(int[] shape, int[] stride, long offset) { this(new float[ArrayUtil.prod(shape)], shape, stride, offset, Nd4j.order()); }
/** * 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); }
public BaseNDArray(long[] shape, long[] stride, long offset) { this(new float[ArrayUtil.prod(shape)], shape, stride, offset, Nd4j.order()); }
/** * @return */ public long numElements() { long ret = 0; for (SDVariable variable : variables()) { ret += ArrayUtil.prod(variable.getShape()); } return ret; }
/** * * Create a buffer equal of length prod(shape) * * @param shape the shape of the buffer to create * @param type the opType to create * @return the created buffer */ public static DataBuffer createBuffer(int[] shape, DataBuffer.Type type, long offset) { int length = ArrayUtil.prod(shape); return type == DataBuffer.Type.DOUBLE ? createBuffer(new double[length], offset) : createBuffer(new float[length], offset); }
/** * This maps an index of a vector * on to a vector in the matrix that can be used * for indexing in to a tensor * @param index the index to map * @param arr the array to use * for indexing * @param rank the dimensions to compute a slice for * @return the mapped index */ public static int mapIndexOntoTensor(int index, INDArray arr, int... rank) { int ret = index * ArrayUtil.prod(ArrayUtil.removeIndex(arr.shape(), rank)); return ret; }
@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()); }
public INDArray mapTensorProto(OnnxProto3.TensorProto tensor) { if(tensor == null) return null; DataBuffer.Type type = nd4jTypeFromOnnxType(tensor.getDataType()); ByteString bytes = tensor.getRawData(); ByteBuffer byteBuffer = bytes.asReadOnlyByteBuffer().order(ByteOrder.nativeOrder()); ByteBuffer directAlloc = ByteBuffer.allocateDirect(byteBuffer.capacity()).order(ByteOrder.nativeOrder()); directAlloc.put(byteBuffer); directAlloc.rewind(); long[] shape = getShapeFromTensor(tensor); DataBuffer buffer = Nd4j.createBuffer(directAlloc,type, ArrayUtil.prod(shape)); INDArray arr = Nd4j.create(buffer).reshape(shape); return arr; }
@Override public long n() { if(n == 0) { if(arg() != null) this.n = ArrayUtil.prod(arg().getShape()); } return n; }
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)")); }
public static List<Pair<INDArray, String>> get3dReshapedWithShape(long seed, long... shape) { Nd4j.getRandom().setSeed(seed); long[] shape2d = {shape[0] * shape[2], shape[1]}; int lenshape2d = ArrayUtil.prod(shape2d); INDArray array2d = Nd4j.linspace(1, lenshape2d, lenshape2d).reshape(shape2d); INDArray array3d = array2d.reshape(shape); return Collections.singletonList(new Pair<>(array3d, "get3dReshapedWithShape(" + seed + "," + Arrays.toString(shape) + ").get(0)")); }
@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; }
@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; }
/** * Create a random ndarray with the given shape using * the current time as the seed * * @param shape the shape of the ndarray * @return the random ndarray with the specified shape */ public static IComplexNDArray complexRand(int... shape) { INDArray based = Nd4j.rand(new int[] {1, ArrayUtil.prod(shape) * 2}); IComplexNDArray ret = Nd4j.createComplex(based.data(), shape); logCreationIfNecessary(ret); return ret; }
public long getReductionLength(DifferentialFunction func) { val inputShape = func.arg().getShape(); if (Shape.isWholeArray(inputShape, func.getDimensions())) { return ArrayUtil.prod(inputShape); } int prod = 1; for (int i : func.getDimensions()) { prod *= inputShape[i]; } return prod; }