Java Code Examples for org.nd4j.linalg.factory.Nd4j#scalar()

The following examples show how to use org.nd4j.linalg.factory.Nd4j#scalar() . 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: DefaultOpExecutioner.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Override
public INDArray execAndReturn(Op op) {
    if (op instanceof TransformOp) {
        return execAndReturn((TransformOp) op);
    }
    if (op instanceof ScalarOp) {
        return execAndReturn((ScalarOp) op);
    }
    if (op instanceof Accumulation) {
        return Nd4j.scalar(execAndReturn((Accumulation) op).getFinalResult());
    }
    if (op instanceof IndexAccumulation) {
        return Nd4j.scalar(execAndReturn((IndexAccumulation) op).getFinalResult());
    }

    throw new IllegalArgumentException("Illegal opType of op: " + op.getClass());
}
 
Example 2
Source File: BaseScalarBoolOp.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
public BaseScalarBoolOp(SameDiff sameDiff,
                        SDVariable i_v,
                        Number scalar,
                        boolean inPlace,
                        Object[] extraArgs) {
    super(sameDiff,inPlace,extraArgs);
    this.scalarValue = Nd4j.scalar(i_v.dataType(), scalar);
    if (i_v != null) {
        this.xVertexId = i_v.name();
        sameDiff.addArgsFor(new String[]{xVertexId},this);
        SameDiffUtils.validateDifferentialFunctionSameDiff(sameDiff, i_v, this);
    } else {
        throw new IllegalArgumentException("Input not null variable.");
    }

}
 
Example 3
Source File: NDArrayTestsFortran.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testElementWiseOps() {
    INDArray n1 = Nd4j.scalar(1);
    INDArray n2 = Nd4j.scalar(2);
    INDArray nClone = n1.add(n2);
    assertEquals(Nd4j.scalar(3), nClone);
    INDArray n1PlusN2 = n1.add(n2);
    assertFalse(getFailureMessage(), n1PlusN2.equals(n1));

    INDArray n3 = Nd4j.scalar(3.0);
    INDArray n4 = Nd4j.scalar(4.0);
    INDArray subbed = n4.sub(n3);
    INDArray mulled = n4.mul(n3);
    INDArray div = n4.div(n3);

    assertFalse(subbed.equals(n4));
    assertFalse(mulled.equals(n4));
    assertEquals(Nd4j.scalar(1.0), subbed);
    assertEquals(Nd4j.scalar(12.0), mulled);
    assertEquals(Nd4j.scalar(1.333333333333333333333), div);
}
 
Example 4
Source File: ReductionBpOpValidation.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testNorm2Bp() {
    //dL/dIn = dL/dOut * dOut/dIn
    //       = dL/dOut * x/|x|_2

    for (boolean keepDims : new boolean[]{false, true}) {

        INDArray preReduceInput = Nd4j.linspace(1, 12, 12).reshape(3, 4).castTo(DataType.DOUBLE);

        double norm2 = preReduceInput.norm2Number().doubleValue();

        INDArray dLdOut;
        if (keepDims) {
            dLdOut = Nd4j.valueArrayOf(new long[]{1, 1}, 0.5);
        } else {
            dLdOut = Nd4j.scalar(DataType.DOUBLE, 0.5);
        }
        INDArray dLdInExpected = preReduceInput.div(norm2).muli(0.5);
        INDArray dLdIn = Nd4j.createUninitialized(DataType.DOUBLE, 3, 4);

        String err = OpValidation.validate(new OpTestCase(new Norm2Bp(preReduceInput, dLdOut, dLdIn, keepDims))
                .expectedOutput(0, dLdInExpected));

        assertNull(err);
    }
}
 
Example 5
Source File: ReductionBpOpValidation.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testNorm1Bp() {
    //dL/dIn = dL/dOut * dOut/dIn
    //       = dL/dOut * sgn(in)

    for (boolean keepDims : new boolean[]{false, true}) {

        INDArray preReduceInput = Nd4j.linspace(-5, 6, 12).addi(0.1).reshape(3, 4);

        INDArray sgn = Transforms.sign(preReduceInput, true);

        INDArray dLdOut;
        if (keepDims) {
            dLdOut = Nd4j.valueArrayOf(new long[]{1, 1}, 0.5);
        } else {
            dLdOut = Nd4j.scalar(0.5);
        }
        INDArray dLdInExpected = sgn.muli(0.5);
        INDArray dLdIn = Nd4j.createUninitialized(3, 4);

        String err = OpValidation.validate(new OpTestCase(new Norm1Bp(preReduceInput, dLdOut, dLdIn, keepDims))
                .expectedOutput(0, dLdInExpected));

        assertNull(err);
    }
}
 
Example 6
Source File: ReductionBpOpValidation.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testProdBP() {
    //Full array product reduction

    //dL/dIn_i  = dL/dOut * dOut/dIn_i
    //          = dL/dOut * d(prod(in))/dIn_i
    //          = dL/dOut * (prod(in) / in_i)

    for (boolean keepDims : new boolean[]{false, true}) {

        INDArray preReduceInput = Nd4j.linspace(1, 12, 12).reshape(3, 4);
        INDArray dLdOut;
        if (keepDims) {
            dLdOut = Nd4j.valueArrayOf(new long[]{1, 1}, 0.5);
        } else {
            dLdOut = Nd4j.scalar(0.5);
        }
        double prod = preReduceInput.prodNumber().doubleValue();
        INDArray dLdInExpected = Nd4j.valueArrayOf(preReduceInput.shape(), prod).divi(preReduceInput).muli(0.5);

        INDArray dLdIn = Nd4j.createUninitialized(3, 4);

        String err = OpValidation.validate(new OpTestCase(new ProdBp(preReduceInput, dLdOut, dLdIn, keepDims))
                .expectedOutput(0, dLdInExpected));

        assertNull(err);
    }
}
 
Example 7
Source File: AeronNDArraySerdeTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testToAndFromCompressed() {
    INDArray arr = Nd4j.scalar(1.0);
    INDArray compress = Nd4j.getCompressor().compress(arr, "GZIP");
    assertTrue(compress.isCompressed());
    UnsafeBuffer buffer = AeronNDArraySerde.toBuffer(compress);
    INDArray back = AeronNDArraySerde.toArray(buffer);
    INDArray decompressed = Nd4j.getCompressor().decompress(compress);
    assertEquals(arr, decompressed);
    assertEquals(arr, back);
}
 
Example 8
Source File: StandardScaler.java    From nd4j with Apache License 2.0 5 votes vote down vote up
public void fit(DataSet dataSet) {
    mean = dataSet.getFeatureMatrix().mean(0);
    std = dataSet.getFeatureMatrix().std(0);
    std.addi(Nd4j.scalar(Nd4j.EPS_THRESHOLD));
    if (std.min(1) == Nd4j.scalar(Nd4j.EPS_THRESHOLD))
        logger.info("API_INFO: Std deviation found to be zero. Transform will round upto epsilon to avoid nans.");
}
 
Example 9
Source File: BinarySerdeTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testToAndFromCompressed() {
    OpValidationSuite.ignoreFailing();  //Failing 2019/01/24
    INDArray arr = Nd4j.scalar(1.0);
    INDArray compress = Nd4j.getCompressor().compress(arr, "GZIP");
    assertTrue(compress.isCompressed());
    ByteBuffer buffer = BinarySerde.toByteBuffer(compress);
    INDArray back = BinarySerde.toArray(buffer);
    INDArray decompressed = Nd4j.getCompressor().decompress(compress);
    assertEquals(arr, decompressed);
    assertEquals(arr, back);
}
 
Example 10
Source File: BinarySerdeTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testToAndFrom() {
    INDArray arr = Nd4j.scalar(1.0);
    ByteBuffer buffer = BinarySerde.toByteBuffer(arr);
    INDArray back = BinarySerde.toArray(buffer);
    assertEquals(arr, back);
}
 
Example 11
Source File: Rank.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public void initFromTensorFlow(NodeDef nodeDef, SameDiff initWith, Map<String, AttrValue> attributesForNode, GraphDef graph) {
    val name = TFGraphMapper.getInstance().getNodeName(nodeDef.getName());
    val input = initWith.getVariable(name);
    val outputVertex = input.getVarName();
    if (!initWith.isPlaceHolder(input.getVarName()) && initWith.shapeAlreadyExistsForVarName(outputVertex)) {
        val inputShape = initWith.getShapeForVarName(input.getVarName());
        val resultLength = Nd4j.scalar(inputShape.length);
        val thisResultId = outputVertex;
        initWith.putArrayForVarName(thisResultId, resultLength);
        initWith.putShapeForVarName(thisResultId, new long[]{1, 1});
    }
}
 
Example 12
Source File: MixedDataTypesTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testBasicCreation_5_3() {
    val scalar = Nd4j.scalar(Short.valueOf((short) 1));
    assertNotNull(scalar);
    assertEquals(0, scalar.rank());
    assertEquals(1, scalar.length());
    assertEquals(DataType.SHORT, scalar.dataType());
    assertEquals(1.0, scalar.getDouble(0), 1e-5);
}
 
Example 13
Source File: BaseSparseNDArray.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray mmul(INDArray other) {
    long[] shape = {rows(), other.columns()};
    INDArray result = createUninitialized(shape, 'f');
    if (result.isScalar())
        return Nd4j.scalar(Nd4j.getBlasWrapper().dot(this, other));
    return mmuli(other, result);
}
 
Example 14
Source File: LoaderIteratorTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testMDSLoaderIter(){

    for(boolean r : new boolean[]{false, true}) {
        List<String> l = Arrays.asList("3", "0", "1");
        Random rng = r ? new Random(12345) : null;
        MultiDataSetIterator iter = new MultiDataSetLoaderIterator(l, null, new Loader<MultiDataSet>() {
            @Override
            public MultiDataSet load(Source source) throws IOException {
                INDArray i = Nd4j.scalar(Integer.valueOf(source.getPath()));
                return new org.nd4j.linalg.dataset.MultiDataSet(i, i);
            }
        }, new LocalFileSourceFactory());

        int count = 0;
        int[] exp = {3, 0, 1};
        while (iter.hasNext()) {
            MultiDataSet ds = iter.next();
            if(!r) {
                assertEquals(exp[count], ds.getFeatures()[0].getInt(0));
            }
            count++;
        }
        assertEquals(3, count);

        iter.reset();
        assertTrue(iter.hasNext());
    }
}
 
Example 15
Source File: BaseNDArray.java    From nd4j with Apache License 2.0 4 votes vote down vote up
protected INDArray createScalar(double d) {
    return Nd4j.scalar(d);
}
 
Example 16
Source File: BaseNDArray.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
protected INDArray createScalar(double d) {
    return Nd4j.scalar(d);
}
 
Example 17
Source File: SameDiffTests.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testMulGradient() {
    INDArray arr1 = Nd4j.linspace(1, 4, 4).reshape(2, 2);
    INDArray arr2 = Nd4j.linspace(1, 4, 4).reshape(2, 2);

    INDArray gradAssertion = Nd4j.ones(arr1.shape());
    INDArray scalar = Nd4j.scalar(1.0);
    INDArray aGradAssertion = Nd4j.create(new double[][]{
            {1, 4},
            {9, 16}
    });

    INDArray cGradAssertion = Nd4j.create(new double[][]{
            {1, 2},
            {3, 4}
    });

    INDArray wGradAssertion = Nd4j.create(new double[][]{
            {2, 8},
            {18, 32}
    });

    INDArray dGradAssertion = Nd4j.ones(2, 2);

    SameDiff sameDiff = SameDiff.create();

    SDVariable sdVariable = sameDiff.var("a", arr1);
    SDVariable sdVariable1 = sameDiff.var("w", arr2);
    SDVariable varMulPre = sdVariable.mul("c", sdVariable1);
    SDVariable varMul = varMulPre.mul("d", sdVariable1);
    SDVariable sum = sameDiff.sum("ret", varMul, Integer.MAX_VALUE);

    Pair<Map<SDVariable, DifferentialFunction>, List<DifferentialFunction>> mapListPair = sameDiff.execBackwards();

    SDVariable finalResult = sameDiff.grad(sum.getVarName());

    SDVariable cGrad = sameDiff.grad(varMulPre.getVarName());

    SDVariable mulGradResult = sameDiff.grad(varMul.getVarName());
    SDVariable aGrad = sameDiff.grad(sdVariable.getVarName());
    SDVariable wGrad = sameDiff.grad(sdVariable1.getVarName());
    SDVariable dGrad = sameDiff.grad(varMul.getVarName());

    INDArray scalarGradTest = finalResult.getArr();
    assertEquals(scalar, scalarGradTest);


    INDArray gradTest = mulGradResult.getArr();
    assertEquals(gradAssertion, gradTest);

    INDArray aGradTest = aGrad.getArr();
    assertEquals(aGradAssertion, aGradTest);

    INDArray cGradTest = cGrad.getArr();
    assertEquals(cGradAssertion, cGradTest);

    INDArray wGradTest = wGrad.getArr();
    assertEquals(wGradAssertion, wGradTest);

    INDArray dGradTest = dGrad.getArr();
    assertEquals(dGradAssertion, dGradTest);


}
 
Example 18
Source File: ByteOrderTests.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testScalarEncoding() {
    val scalar = Nd4j.scalar(2.0f);

    FlatBufferBuilder bufferBuilder = new FlatBufferBuilder(0);
    val fb = scalar.toFlatArray(bufferBuilder);
    bufferBuilder.finish(fb);
    val db = bufferBuilder.dataBuffer();

    val flat = FlatArray.getRootAsFlatArray(db);


    val restored = Nd4j.createFromFlatArray(flat);

    assertEquals(scalar, restored);
}
 
Example 19
Source File: BooleanIndexing.java    From deeplearning4j with Apache License 2.0 3 votes vote down vote up
/**
 * This method returns last index matching given condition
 *
 * PLEASE NOTE: This method will return -1 value if condition wasn't met
 *
 * @param array
 * @param condition
 * @return
 */
public static INDArray lastIndex(INDArray array, Condition condition) {
    if (!(condition instanceof BaseCondition))
        throw new UnsupportedOperationException("Only static Conditions are supported");

    LastIndex idx = new LastIndex(array, condition);
    Nd4j.getExecutioner().exec(idx);
    return Nd4j.scalar(DataType.LONG, idx.getFinalResult().longValue());
}
 
Example 20
Source File: BaseNDArray.java    From nd4j with Apache License 2.0 2 votes vote down vote up
/**
 * Fetch a particular number on a multi dimensional scale.
 *
 * @param indexes the indexes to get a number from
 * @return the number at the specified indices
 */
@Override
public INDArray getScalar(int... indexes) {
    return Nd4j.scalar(getDouble(indexes));
}