org.nd4j.linalg.api.shape.Shape Java Examples

The following examples show how to use org.nd4j.linalg.api.shape.Shape. 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: NDArrayTestsFortran.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testWrap() throws Exception {
    int[] shape = {2, 4};
    INDArray d = Nd4j.linspace(1, 8, 8).reshape(shape[0], shape[1]);
    INDArray n = d;
    assertEquals(d.rows(), n.rows());
    assertEquals(d.columns(), n.columns());

    INDArray vector = Nd4j.linspace(1, 3, 3);
    INDArray testVector = vector;
    for (int i = 0; i < vector.length(); i++)
        assertEquals(vector.getDouble(i), testVector.getDouble(i), 1e-1);
    assertEquals(3, testVector.length());
    assertEquals(true, testVector.isVector());
    assertEquals(true, Shape.shapeEquals(new long[] {3}, testVector.shape()));

    INDArray row12 = Nd4j.linspace(1, 2, 2).reshape(2, 1);
    INDArray row22 = Nd4j.linspace(3, 4, 2).reshape(1, 2);

    assertEquals(row12.rows(), 2);
    assertEquals(row12.columns(), 1);
    assertEquals(row22.rows(), 1);
    assertEquals(row22.columns(), 2);
}
 
Example #2
Source File: CosineSimilarity.java    From nd4j with Apache License 2.0 6 votes vote down vote up
public static List<SDVariable> doDiff(SameDiff sameDiff, DifferentialFunctionFactory f, SDVariable x, SDVariable y,
                                      SDVariable gradOut, int... dimensions){
    SDVariable a = sameDiff.sum(x.mul(y),dimensions);
    SDVariable l2x = f.norm2(x, dimensions);
    SDVariable l2y = f.norm2(y, dimensions);
    SDVariable b = l2x.mul(l2y);

    int origRank = Shape.rankFromShape(x.getShape());
    SDVariable broadcastableA = f.reductionBroadcastableWithOrigShape(origRank, dimensions, a);
    SDVariable broadcastableB = f.reductionBroadcastableWithOrigShape(origRank, dimensions, b);
    SDVariable broadcastableL2xSq = f.reductionBroadcastableWithOrigShape(origRank, dimensions, sameDiff.square(l2x));
    SDVariable broadcastableL2ySq = f.reductionBroadcastableWithOrigShape(origRank, dimensions, sameDiff.square(l2y));
    SDVariable broadcastableGrad = f.reductionBroadcastableWithOrigShape(origRank, dimensions, gradOut);

    SDVariable dcdx = y.sub(x.mul(broadcastableA).div(broadcastableL2xSq)).div(broadcastableB);
    SDVariable dcdy = x.sub(y.mul(broadcastableA).div(broadcastableL2ySq)).div(broadcastableB);

    return Arrays.asList(dcdx.mul(broadcastableGrad), dcdy.mul(broadcastableGrad));
}
 
Example #3
Source File: ManhattanDistance.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Override
public List<SDVariable> doDiff(List<SDVariable> i_v1) {
    //ddist(x,y)/dxi = sign(xi-yi)
    SDVariable difference = larg().sub(rarg());
    SDVariable gradBroadcastable;
    int origRank = Shape.rankFromShape(arg().getShape());   //TODO shape may not always be defined?
    if(!(dimensions.length == 1 && dimensions[0] == Integer.MAX_VALUE) ){
        //1x1 output case
        gradBroadcastable = i_v1.get(0);
    } else {
        gradBroadcastable = f().reductionBroadcastableWithOrigShape(origRank, dimensions, i_v1.get(0));
    }

    SDVariable gradX = sameDiff.sign(difference).mul(gradBroadcastable);
    SDVariable gradY = f().neg(gradX);
    return Arrays.asList(gradX, gradY);
}
 
Example #4
Source File: Deconvolution2DLayer.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public INDArray activate(boolean training, LayerWorkspaceMgr workspaceMgr) {
    assertInputSet(false);

    if (cacheMode == null)
        cacheMode = CacheMode.NONE;

    applyDropOutIfNecessary(training, workspaceMgr);

    INDArray z = preOutput(training, false, workspaceMgr).getFirst();

    IActivation afn = layerConf().getActivationFn();

    if (helper != null && Shape.strideDescendingCAscendingF(z)) {
        INDArray ret = helper.activate(z, layerConf().getActivationFn(), training);
        if (ret != null) {
            return ret;
        }
    }

    INDArray activation = afn.getActivation(z, training);
    return activation;
}
 
Example #5
Source File: EuclideanDistance.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Override
public List<SDVariable> doDiff(List<SDVariable> i_v1) {
    //ddist(x,y)/dxi = (xi-yi)/dist(x,y)
    SDVariable euc = outputVariables()[0];
    SDVariable difference = larg().sub(rarg());
    SDVariable divBroadcastable;
    int origRank = Shape.rankFromShape(arg().getShape());   //TODO shape may not always be defined?
    if(!(dimensions.length == 1 && dimensions[0] == Integer.MAX_VALUE) ){
        //1x1 output case
        divBroadcastable = i_v1.get(0).div(euc);
    } else {
        divBroadcastable = f().reductionBroadcastableWithOrigShape(origRank, dimensions, i_v1.get(0).div(euc));
    }

    SDVariable gradX = difference.mul(divBroadcastable);
    SDVariable gradY = f().neg(gradX);
    return Arrays.asList(gradX, gradY);
}
 
Example #6
Source File: CnnToFeedForwardPreProcessor.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public INDArray backprop(INDArray epsilons, int miniBatchSize, LayerWorkspaceMgr workspaceMgr) {
    //Epsilons from layer above should be 2d, with shape [miniBatchSize, depthOut*outH*outW]
    if (epsilons.ordering() != 'c' || !Shape.strideDescendingCAscendingF(epsilons))
        epsilons = workspaceMgr.dup(ArrayType.ACTIVATION_GRAD, epsilons, 'c');

    if (epsilons.rank() == 4)
        return workspaceMgr.leverageTo(ArrayType.ACTIVATION_GRAD, epsilons); //Should never happen

    if (epsilons.columns() != inputWidth * inputHeight * numChannels)
        throw new IllegalArgumentException("Invalid input: expect output columns must be equal to rows "
                        + inputHeight + " x columns " + inputWidth + " x channels " + numChannels + " but was instead "
                        + Arrays.toString(epsilons.shape()));

    INDArray ret;
    if(format == CNN2DFormat.NCHW){
        ret = epsilons.reshape('c', epsilons.size(0), numChannels, inputHeight, inputWidth);
    } else {
        ret = epsilons.reshape('c', epsilons.size(0), inputHeight, inputWidth, numChannels);
    }

    return workspaceMgr.leverageTo(ArrayType.ACTIVATION_GRAD, ret); //Move if required to specified workspace
}
 
Example #7
Source File: BaseNDArray.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public float[][] toFloatMatrix() {
    if(!isMatrix()) {
        throw new ND4JIllegalStateException("Unable to create a 2d array from a non matrix! Shape: " + Shape.shapeToStringShort(this));
    }

    if (this.rows() > Integer.MAX_VALUE || this.columns() > Integer.MAX_VALUE)
        throw new ND4JArraySizeException();

    float[][] ret = new float[(int) rows()][ (int) columns()];
    for(int i = 0; i < ret.length; i++) {
        ret[i] = getRow(i).dup().data().asFloat();
    }

    return ret;
}
 
Example #8
Source File: BaseNDArray.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public double getDouble(int... indices) {
    autoProcessScalarCall();
    Nd4j.getCompressor().autoDecompress(this);
    Preconditions.checkState(!isEmpty(), "Unable to get value from empty array");

    for (int i = 0; i < indices.length; i++) {
        if (indices[i] < 0)
            indices[i] += rank();
    }
    if (indices.length == 1) {
        if (rank() == 1)
            return Shape.getDouble(this, indices[0]);
        else if (isRowVector())
            return Shape.getDouble(this, 0, indices[0]);
        else if (isColumnVector())
            return Shape.getDouble(this, indices[0], 0);
        else if ((isScalar() || length() == 1) && indices[0] == 0)
            return data().getDouble(0);
    }
    return Shape.getDouble(this, indices);
}
 
Example #9
Source File: BaseNDArray.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public double getDouble(long... indices) {
    autoProcessScalarCall();
    Nd4j.getCompressor().autoDecompress(this);
    Preconditions.checkState(!isEmpty(), "Unable to get value from empty array");

    for (int i = 0; i < indices.length; i++) {
        if (indices[i] < 0)
            indices[i] += rank();
    }
    if (indices.length == 1) {
        if (rank() == 1)
            return Shape.getDouble(this, indices[0]);
        else if (isRowVector())
            return Shape.getDouble(this, 0, indices[0]);
        else if (isColumnVector())
            return Shape.getDouble(this, indices[0], 0);
        else if (isScalar() && indices[0] == 0)
            return data().getDouble(0);
        else
            throw new IllegalStateException("Indexes length must be > 1 for non vectors and scalars");
    }
    return Shape.getDouble(this, indices);
}
 
Example #10
Source File: BaseScalarOp.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public List<LongShapeDescriptor> calculateOutputShape(OpContext oc) {
    INDArray x = oc != null ? oc.getInputArray(0) : x();

    val ret = new ArrayList<LongShapeDescriptor>(1);

    long[] s;
    if(x != null){
        s = x.shape();
    } else {
        s = arg().getShape();
    }

    val aT = arg().dataType();
    val sT = scalarValue.dataType();

    ret.add(LongShapeDescriptor.fromShape(s, Shape.pickPairwiseDataType(aT, sT)));
    return ret;
}
 
Example #11
Source File: ShapeBufferTests.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testShape() {
    long[] shape = {2, 4};
    long[] stride = {1, 2};
    val shapeInfoBuffer = Shape.createShapeInformation(shape, stride, 1, 'c', DataType.DOUBLE, false);
    val buff = shapeInfoBuffer.asNioLong();
    val shapeView = Shape.shapeOf(buff);
    assertTrue(Shape.contentEquals(shape, shapeView));
    val strideView = Shape.stride(buff);
    assertTrue(Shape.contentEquals(stride, strideView));
    assertEquals('c', Shape.order(buff));
    assertEquals(1, Shape.elementWiseStride(buff));
    assertFalse(Shape.isVector(buff));
    assertTrue(Shape.contentEquals(shape, Shape.shapeOf(buff)));
    assertTrue(Shape.contentEquals(stride, Shape.stride(buff)));
}
 
Example #12
Source File: Indices.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/**
 * Prunes indices of greater length than the shape
 * and fills in missing indices if there are any
 *
 * @param originalShape the original shape to adjust to
 * @param indexes       the indexes to adjust
 * @return the  adjusted indices
 */
public static INDArrayIndex[] adjustIndices(int[] originalShape, INDArrayIndex... indexes) {
    if (Shape.isVector(originalShape) && indexes.length == 1)
        return indexes;

    if (indexes.length < originalShape.length)
        indexes = fillIn(originalShape, indexes);
    if (indexes.length > originalShape.length) {
        INDArrayIndex[] ret = new INDArrayIndex[originalShape.length];
        System.arraycopy(indexes, 0, ret, 0, originalShape.length);
        return ret;
    }

    if (indexes.length == originalShape.length)
        return indexes;
    for (int i = 0; i < indexes.length; i++) {
        if (indexes[i].end() >= originalShape[i] || indexes[i] instanceof NDArrayIndexAll)
            indexes[i] = NDArrayIndex.interval(0, originalShape[i] - 1);
    }

    return indexes;
}
 
Example #13
Source File: BaseNDArray.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 * Create an ndarray from the specified slices.
 * This will go through and merge all of the
 * data from each slice in to one ndarray
 * which will then take the specified shape
 *
 * @param slices the slices to merge
 * @param shape  the shape of the ndarray
 */
public BaseNDArray(List<INDArray> slices, int[] shape, int[] stride, char ordering) {
    Shape.assertValidOrder(ordering);
    DataBuffer ret = slices.get(0).data().dataType() == (DataType.FLOAT)
            ? Nd4j.createBuffer(new float[ArrayUtil.prod(shape)])
            : Nd4j.createBuffer(new double[ArrayUtil.prod(shape)]);
    this.data = ret;
    setShapeInformation(Nd4j.getShapeInfoProvider().createShapeInformation(ArrayUtil.toLongArray(shape), ArrayUtil.toLongArray(stride),
            Shape.elementWiseStride(shape, stride, ordering == 'f'), ordering, slices.get(0).dataType(), false));
    init(shape, stride);
    //    Shape.setElementWiseStride(this.shapeInfo(),Shape.elementWiseStride(shape, stride, ordering == 'f'));

    if (slices.get(0).isScalar()) {
        for (int i = 0; i < length(); i++) {
            putScalar(i, slices.get(i).getDouble(0));
        }
    } else {
        for (int i = 0; i < slices(); i++) {
            putSlice(i, slices.get(i));
        }
    }
}
 
Example #14
Source File: AdamUpdater.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public void setStateViewArray(INDArray viewArray, long[] gradientShape, char gradientOrder, boolean initialize) {
    if (!viewArray.isRowVector())
        throw new IllegalArgumentException("Invalid input: expect row vector input");
    if (initialize)
        viewArray.assign(0);
    long length = viewArray.length();
    this.m = viewArray.get(NDArrayIndex.point(0), NDArrayIndex.interval(0, length / 2));
    this.v = viewArray.get(NDArrayIndex.point(0), NDArrayIndex.interval(length / 2, length));

    //Reshape to match the expected shape of the input gradient arrays
    this.m = Shape.newShapeNoCopy(this.m, gradientShape, gradientOrder == 'f');
    this.v = Shape.newShapeNoCopy(this.v, gradientShape, gradientOrder == 'f');
    if (m == null || v == null)
        throw new IllegalStateException("Could not correctly reshape gradient view arrays");

    this.gradientReshapeOrder = gradientOrder;
}
 
Example #15
Source File: BooleanIndexing.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/**
 * Or over the whole ndarray given some condition
 *
 * @param n
 * @param cond
 * @return
 */
public static boolean or(final INDArray n, final Condition cond) {
    if (cond instanceof BaseCondition) {
        long val = (long) Nd4j.getExecutioner().exec(new MatchCondition(n, cond), Integer.MAX_VALUE).getDouble(0);

        if (val > 0)
            return true;
        else
            return false;

    } else {
        boolean ret = false;
        final AtomicBoolean a = new AtomicBoolean(ret);
        Shape.iterate(n, new CoordinateFunction() {
            @Override
            public void process(long[]... coord) {
                if (!a.get())
                    a.compareAndSet(false, a.get() || cond.apply(n.getDouble(coord[0])));
            }
        });

        return a.get();
    }
}
 
Example #16
Source File: AMSGradUpdater.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Override
public void setStateViewArray(INDArray viewArray, long[] gradientShape, char gradientOrder, boolean initialize) {
    if (!viewArray.isRowVector())
        throw new IllegalArgumentException("Invalid input: expect row vector input");
    if (initialize)
        viewArray.assign(0);
    val n = viewArray.length() / 3;
    this.m = viewArray.get(NDArrayIndex.point(0), NDArrayIndex.interval(0, n));
    this.v = viewArray.get(NDArrayIndex.point(0), NDArrayIndex.interval(n, 2*n));
    this.vHat = viewArray.get(NDArrayIndex.point(0), NDArrayIndex.interval(2*n, 3*n));

    //Reshape to match the expected shape of the input gradient arrays
    this.m = Shape.newShapeNoCopy(this.m, gradientShape, gradientOrder == 'f');
    this.v = Shape.newShapeNoCopy(this.v, gradientShape, gradientOrder == 'f');
    this.vHat = Shape.newShapeNoCopy(this.vHat, gradientShape, gradientOrder == 'f');
    if (m == null || v == null || vHat == null)
        throw new IllegalStateException("Could not correctly reshape gradient view arrays");

    this.gradientReshapeOrder = gradientOrder;
}
 
Example #17
Source File: BasicWorkspaceTests.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testNoShape1() {
    int outDepth = 50;
    int miniBatch = 64;
    int outH = 8;
    int outW = 8;

    try (Nd4jWorkspace wsI =
                    (Nd4jWorkspace) Nd4j.getWorkspaceManager().getAndActivateWorkspace(basicConfig, "ITER")) {
        INDArray delta = Nd4j.create(new int[] {50, 64, 8, 8}, new int[] {64, 3200, 8, 1}, 'c');
        delta = delta.permute(1, 0, 2, 3);

        assertArrayEquals(new long[] {64, 50, 8, 8}, delta.shape());
        assertArrayEquals(new long[] {3200, 64, 8, 1}, delta.stride());

        INDArray delta2d = Shape.newShapeNoCopy(delta, new int[] {outDepth, miniBatch * outH * outW}, false);

        assertNotNull(delta2d);
    }
}
 
Example #18
Source File: BaseNDArray.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public INDArray putScalar(long i, double value) {
    Preconditions.checkArgument(dataType() != DataType.BOOL || value == 0.0 || value == 1.0, "Cannot put value %s into boolean array" +
            " - only putScalar with values 0 or 1 is allowed on boolean arrays", value);
    if (i < 0)
        i += rank();

    // TODO: i'm not sure that rank == 1 has fair shortcut here
    if (isScalar()) {
        autoProcessScalarCall();
        data.put(i, value);
        return this;
    } else if (rank() == 1) {
        data.put(i * stride(0), value);
        return this;
    }

    // we cant raise rank here, if original rank is 1
    if (isRowVector() && rank() == 2) {
        return putScalar(0, i, value);
    } else if (isColumnVector() && rank() == 2) {
        return putScalar(i, 0, value);
    }
    long[] indexes = ordering() == 'c' ? Shape.ind2subC(this, i) : Shape.ind2sub(this, i);
    return putScalar(indexes, value);
}
 
Example #19
Source File: StaticShapeTests.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testShapeInd2Sub() {
    long normalTotal = 0;
    long n = 1000;
    for (int i = 0; i < n; i++) {
        long start = System.nanoTime();
        Shape.ind2subC(new int[] {2, 2}, 1);
        long end = System.nanoTime();
        normalTotal += Math.abs(end - start);
    }

    normalTotal /= n;
    System.out.println(normalTotal);

    System.out.println("C " + Arrays.toString(Shape.ind2subC(new int[] {2, 2}, 1)));
    System.out.println("F " + Arrays.toString(Shape.ind2sub(new int[] {2, 2}, 1)));
}
 
Example #20
Source File: CheckUtil.java    From nd4j with Apache License 2.0 6 votes vote down vote up
public static boolean checkDivManually(INDArray first, INDArray second, double maxRelativeDifference,
                double minAbsDifference) {
    //No apache commons element-wise division, but can do this manually

    INDArray result = first.div(second);
    long[] shape = first.shape();

    INDArray expected = Nd4j.zeros(first.shape());

    for (int i = 0; i < shape[0]; i++) {
        for (int j = 0; j < shape[1]; j++) {
            double v = first.getDouble(i, j) / second.getDouble(i, j);
            expected.putScalar(new int[] {i, j}, v);
        }
    }
    if (!checkShape(expected, result))
        return false;
    boolean ok = checkEntries(expected, result, maxRelativeDifference, minAbsDifference);
    if (!ok) {
        INDArray onCopies = Shape.toOffsetZeroCopy(first).mul(Shape.toOffsetZeroCopy(second));
        printFailureDetails(first, second, expected, result, onCopies, "div");
    }
    return ok;
}
 
Example #21
Source File: BaseNDArray.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Override
public INDArray putScalar(long[] indexes, double value) {
    Nd4j.getCompressor().autoDecompress(this);


    for (int i = 0; i < indexes.length; i++) {
        if (indexes[i] < 0)
            indexes[i] += rank();
    }

    if (indexes.length == 1) {
        return putScalar(indexes[0], value);
    } else if (indexes.length == 2) {
        return putScalar(indexes[0], indexes[1], value);
    } else if (indexes.length == 3) {
        return putScalar(indexes[0], indexes[1], indexes[2], value);
    } else if (indexes.length == 4) {
        return putScalar(indexes[0], indexes[1], indexes[2], indexes[3], value);
    } else {
        autoProcessScalarCall();
        long offset = Shape.getOffset(javaShapeInformation, indexes);
        data.put(offset, value);
    }
    return this;
}
 
Example #22
Source File: ShapeTest.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testEqualsWithSqueeze(){

    assertTrue(Shape.shapeEqualWithSqueeze(null, null));
    assertTrue(Shape.shapeEqualWithSqueeze(new long[0], new long[0]));
    assertTrue(Shape.shapeEqualWithSqueeze(new long[0], new long[]{1}));
    assertFalse(Shape.shapeEqualWithSqueeze(new long[0], new long[]{1,2}));
    assertFalse(Shape.shapeEqualWithSqueeze(new long[0], new long[]{2,1}));
    assertTrue(Shape.shapeEqualWithSqueeze(new long[]{1}, new long[0]));
    assertTrue(Shape.shapeEqualWithSqueeze(new long[0], new long[]{1,1,1,1,1}));
    assertTrue(Shape.shapeEqualWithSqueeze(new long[]{1,1,1,1,1}, new long[0]));
    assertTrue(Shape.shapeEqualWithSqueeze(new long[]{1}, new long[]{1,1,1}));

    assertTrue(Shape.shapeEqualWithSqueeze(new long[]{2,3}, new long[]{2,3}));
    assertFalse(Shape.shapeEqualWithSqueeze(new long[]{2,3}, new long[]{3,2}));
    assertTrue(Shape.shapeEqualWithSqueeze(new long[]{1,2,2}, new long[]{2,2}));
    assertTrue(Shape.shapeEqualWithSqueeze(new long[]{1,2,3}, new long[]{2,1,1,3}));
    assertFalse(Shape.shapeEqualWithSqueeze(new long[]{1,2,3}, new long[]{2,1,1,4}));

}
 
Example #23
Source File: ClipByNorm.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Override
    public List<SDVariable> doDiff(List<SDVariable> grad) {
        //dOut/dIn is ??? if clipped, 1 otherwise
        int origRank = Shape.rankFromShape(arg().getShape());
        SDVariable l2norm = f().norm2(arg(), dimensions);
        SDVariable broadcastableNorm = f().reductionBroadcastableWithOrigShape(origRank, dimensions, l2norm);
        SDVariable isClippedBC = f().gte(broadcastableNorm, clipValue);
        SDVariable notClippedBC = isClippedBC.rsub(1.0);

//        SDVariable dnormdx = arg().div(broadcastableNorm);
//        SDVariable sqNorm = f().square(broadcastableNorm);
//        SDVariable dOutdInClipped = sqNorm.rdiv(-1).mul(dnormdx).mul(arg()) //-1/(norm2(x))^2 * x/norm2(x)
//                .add(broadcastableNorm.rdiv(1.0))
//                .mul(clipValue);

        SDVariable dOutdInClipped = f().neg(f().square(arg()).div(f().cube(broadcastableNorm))) //-x^2/(norm2(x))^3
                .add(broadcastableNorm.rdiv(1.0))   //+ 1/norm(x)
                .mul(clipValue).mul(isClippedBC);


        SDVariable ret = notClippedBC.add(dOutdInClipped).mul(grad.get(0));
        return Arrays.asList(ret);
    }
 
Example #24
Source File: StandardDeviation.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public List<LongShapeDescriptor> calculateOutputShape() {
    if(args().length < 1) {
        throw new ND4JIllegalStateException("Unable to compute input shape. No arguments found.");
    }

    long[] argShape = arg().getShape();
    if (argShape == null && x() == null) {
        return Collections.emptyList();
    }
    long[] inputShape = (argShape == null || Shape.isPlaceholderShape(argShape) ? x().shape() : argShape);

    val ret = new ArrayList<LongShapeDescriptor>(1);
    val reducedShape = Shape.getReducedShape(inputShape,dimensions, isKeepDims());
    ret.add(LongShapeDescriptor.fromShape(reducedShape, resultType()));
    return ret;
}
 
Example #25
Source File: BaseNDArray.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
public BaseNDArray(List<INDArray> slices, long[] shape, long[] stride, char ordering) {
    DataBuffer ret = Nd4j.createBuffer(slices.get(0).dataType(), Shape.lengthOf(shape), false); /*slices.get(0).data().dataType() == (DataType.FLOAT)
            ? Nd4j.createBuffer(new float[ArrayUtil.prod(shape)])
            : Nd4j.createBuffer(new double[ArrayUtil.prod(shape)]);
            */
    this.data = ret;
    setShapeInformation(Nd4j.getShapeInfoProvider().createShapeInformation(shape, stride,
            Shape.elementWiseStride(shape, stride, ordering == 'f'), ordering, slices.get(0).dataType(), false));
    init(shape, stride);
    //    Shape.setElementWiseStride(this.shapeInfo(),Shape.elementWiseStride(shape, stride, ordering == 'f'));

    if (slices.get(0).isScalar()) {
        for (int i = 0; i < length(); i++) {
            putScalar(i, slices.get(i).getDouble(0));
        }
    } else {
        for (int i = 0; i < slices(); i++) {
            putSlice(i, slices.get(i));
        }
    }
}
 
Example #26
Source File: CheckUtil.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/** Same as checkMmul, but for matrix subtraction */
public static boolean checkSubtract(INDArray first, INDArray second, double maxRelativeDifference,
                double minAbsDifference) {
    RealMatrix rmFirst = convertToApacheMatrix(first);
    RealMatrix rmSecond = convertToApacheMatrix(second);

    INDArray result = first.sub(second);
    RealMatrix rmResult = rmFirst.subtract(rmSecond);

    if (!checkShape(rmResult, result))
        return false;
    boolean ok = checkEntries(rmResult, result, maxRelativeDifference, minAbsDifference);
    if (!ok) {
        INDArray onCopies = Shape.toOffsetZeroCopy(first).sub(Shape.toOffsetZeroCopy(second));
        printFailureDetails(first, second, rmResult, result, onCopies, "sub");
    }
    return ok;
}
 
Example #27
Source File: BaseOp.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
protected void defineDimensions(int... dimensions){
    if (dimensions != null && dimensions.length > 0) {
        if(x != null) {
            dimensions = Shape.normalizeAxis(x.rank(), dimensions);
        }
    }

    if (dimensions == null || dimensions.length == 0)
        dimensions = new int[]{Integer.MAX_VALUE};

    try(MemoryWorkspace ws = Nd4j.getWorkspaceManager().scopeOutOfWorkspaces()) {
        this.dimensionz = Shape.ndArrayDimFromInt(dimensions);
    }
}
 
Example #28
Source File: BaseReduceSameOp.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public List<LongShapeDescriptor> calculateOutputShape(OpContext oc) {
    INDArray x = oc != null ? oc.getInputArray(0) : x();

    if(x == null)
        return Collections.emptyList();

    //Calculate reduction shape. Note that reduction on scalar - returns a scalar
    long[] reducedShape = x.rank() == 0 ? x.shape() : Shape.getReducedShape(x.shape(),dimensions, isKeepDims());
    DataType rt = oc != null ? resultType(oc) : resultType();
    return Collections.singletonList(LongShapeDescriptor.fromShape(reducedShape, rt));
}
 
Example #29
Source File: BaseNDArray.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public BaseNDArray(double[] data, long[] shape, long[] stride, long offset, char ordering) {
    Shape.assertValidOrder(ordering);
    setShapeInformation(Nd4j.getShapeInfoProvider().createShapeInformation(shape, stride,
            Shape.elementWiseStride(shape, stride, ordering == 'f'), ordering, DataType.DOUBLE, data != null && data.length > 0 ? false : true));
    if (data != null && data.length > 0) {
        this.data = Nd4j.createBuffer(data, offset);
        if (offset >= data.length)
            throw new IllegalArgumentException("invalid offset: must be < data.length");
    }

    init(shape, stride);
}
 
Example #30
Source File: BaseTransformOp.java    From nd4j with Apache License 2.0 5 votes vote down vote up
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.");
    }


}