org.nd4j.linalg.api.buffer.DataType Java Examples

The following examples show how to use org.nd4j.linalg.api.buffer.DataType. 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: CompressionTests.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testThresholdSerialization1() throws Exception {
    INDArray initial = Nd4j.create(new double[] {-1.0, -2.0, 0.0, 0.0, 1.0, 1.0});
    INDArray exp_0 = Nd4j.create(new double[] {-1.0 + 1e-3, -2.0 + 1e-3, 0.0, 0.0, 1.0 - 1e-3, 1.0 - 1e-3});
    INDArray exp_1 = Nd4j.create(new double[] {-1e-3, -1e-3, 0.0, 0.0, 1e-3, 1e-3});

    //Nd4j.getCompressor().getCompressor("THRESHOLD").configure(1e-3);
    INDArray compressed = Nd4j.getExecutioner().thresholdEncode(initial, 1e-3f);

    assertEquals(exp_0, initial);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Nd4j.write(baos, compressed);

    INDArray serialized = Nd4j.read(new ByteArrayInputStream(baos.toByteArray()));

    INDArray decompressed_copy = Nd4j.create(DataType.DOUBLE, initial.length());
    Nd4j.getExecutioner().thresholdDecode(serialized, decompressed_copy);

    assertEquals(exp_1, decompressed_copy);
}
 
Example #2
Source File: NDArrayTestsFortran.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testBroadcastingGenerated() {
    int[][] broadcastShape = NDArrayCreationUtil.getRandomBroadCastShape(7, 6, 10);
    List<List<Pair<INDArray, String>>> broadCastList = new ArrayList<>(broadcastShape.length);
    for (int[] shape : broadcastShape) {
        List<Pair<INDArray, String>> arrShape = NDArrayCreationUtil.get6dPermutedWithShape(7, shape, DataType.DOUBLE);
        broadCastList.add(arrShape);
        broadCastList.add(NDArrayCreationUtil.get6dReshapedWithShape(7, shape, DataType.DOUBLE));
        broadCastList.add(NDArrayCreationUtil.getAll6dTestArraysWithShape(7, shape, DataType.DOUBLE));
    }

    for (List<Pair<INDArray, String>> b : broadCastList) {
        for (Pair<INDArray, String> val : b) {
            INDArray inputArrBroadcast = val.getFirst();
            val destShape = NDArrayCreationUtil.broadcastToShape(inputArrBroadcast.shape(), 7);
            INDArray output = inputArrBroadcast
                            .broadcast(NDArrayCreationUtil.broadcastToShape(inputArrBroadcast.shape(), 7));
            assertArrayEquals(destShape, output.shape());
        }
    }



}
 
Example #3
Source File: ShapeOpValidation.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testGatherScalar(){
    INDArray in = Nd4j.linspace(100, 200, 100, DataType.FLOAT).reshape(100);
    INDArray indices = Nd4j.scalar(0);
    INDArray axis = Nd4j.scalar(0);

    DynamicCustomOp op = DynamicCustomOp.builder("gather")
            .addInputs(in, indices, axis)
            .build();

    List<LongShapeDescriptor> l = op.calculateOutputShape();
    long[] shape = l.get(0).getShape();
    assertArrayEquals(new long[0], shape);

    INDArray arr = Nd4j.create(l.get(0));

    op.addOutputArgument(arr);

    Nd4j.exec(op);

    INDArray exp = Nd4j.scalar(DataType.FLOAT, 100);
    assertEquals(exp, arr);
}
 
Example #4
Source File: ActivationGradChecks.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testActivationGradientCheck2(){
    Nd4j.getRandom().setSeed(12345);
    SameDiff sd = SameDiff.create();
    SDVariable x = sd.placeHolder("x", DataType.DOUBLE, 3, 4);
    SDVariable y = sd.var("y", Nd4j.rand(DataType.DOUBLE, 4, 5));
    SDVariable mmul = x.mmul("mmul", y);
    SDVariable sigmoid = sd.math().tanh("sigmoid", mmul);
    SDVariable loss = sigmoid.std(true);

    Map<String, INDArray> m = new HashMap<>();
    m.put("x", Nd4j.rand(DataType.DOUBLE, 3, 4));

    GradCheckUtil.ActGradConfig c = GradCheckUtil.ActGradConfig.builder()
            .sd(sd)
            .placeholderValues(m)
            .activationGradsToCheck(Arrays.asList("sigmoid", "mmul"))
            .subset(GradCheckUtil.Subset.RANDOM)
            .maxPerParam(10)
            .build();

    boolean ok = GradCheckUtil.checkActivationGradients(c);

    assertTrue(ok);
}
 
Example #5
Source File: PythonNumpyTest.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Parameterized.Parameters(name = "{index}: Testing with DataType={0}")
public static DataType[] data() {
    return new DataType[] {
            DataType.BOOL,
            DataType.FLOAT16,
            DataType.BFLOAT16,
            DataType.FLOAT,
            DataType.DOUBLE,
            DataType.INT8,
            DataType.INT16,
            DataType.INT32,
            DataType.INT64,
            DataType.UINT8,
            DataType.UINT16,
            DataType.UINT32,
            DataType.UINT64
    };
}
 
Example #6
Source File: RNNTestCases.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public Object getConfiguration() throws Exception {
    return new NeuralNetConfiguration.Builder()
            .dataType(DataType.FLOAT)
            .seed(12345)
            .updater(new Adam(5e-2))
            .l1(1e-3).l2(1e-3)
            .list()
            .layer(0, new Bidirectional(new LSTM.Builder().activation(Activation.TANH).nOut(10).build()))
            .layer(new GlobalPoolingLayer.Builder().poolingType(PoolingType.AVG).build())
            .layer(new OutputLayer.Builder().nOut(6)
                    .lossFunction(LossFunctions.LossFunction.MCXENT)
                    .activation(Activation.SOFTMAX)
                    .build())
            .setInputType(InputType.recurrent(1))
            .build();
}
 
Example #7
Source File: CustomOpsTests.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Ignore("AS 11/13/2019 https://github.com/eclipse/deeplearning4j/issues/8374")
@Test
public void testDrawBoundingBoxesShape() {
    INDArray images = Nd4j.createFromArray(new float[]{0.7788f, 0.8012f, 0.7244f,  0.2309f, 0.7271f,
                    0.1804f,0.5056f,0.8925f,0.5461f,0.9234f,0.0856f,0.7938f,0.6591f,0.5555f,0.1596f,
                    0.3087f,0.1548f,0.4695f,0.9939f,0.6113f,0.6765f,0.1800f,0.6750f,0.2246f,0.0509f,
                    0.4601f,0.8284f,0.2354f,0.9752f,0.8361f,0.2585f,0.4189f,0.7028f,0.7679f,0.5373f,
                    0.7234f,0.2690f,0.0062f,0.0327f,0.0644f,0.8428f,0.7494f,0.0755f,0.6245f,0.3491f,
                    0.5793f,0.5730f,0.1822f,0.6420f,0.9143f}).reshape(2,5,5,1);
    INDArray boxes = Nd4j.createFromArray(new float[]{0.7717f,    0.9281f,    0.9846f,    0.4838f,
                                                      0.6433f,    0.6041f,    0.6501f,    0.7612f,
                                                      0.7605f,    0.3948f,    0.9493f,    0.8600f,
                                                      0.7876f,    0.8945f,    0.4638f,    0.7157f}).reshape(2,2,4);
    INDArray colors = Nd4j.createFromArray(new float[]{0.9441f, 0.5957f}).reshape(1,2);
    INDArray output = Nd4j.create(DataType.FLOAT, images.shape());
    val op = new DrawBoundingBoxes(images, boxes, colors, output);
    Nd4j.exec(op);
    INDArray expected = Nd4j.createFromArray(new float[]{0.7788f, 0.8012f, 0.7244f, 0.2309f, 0.7271f,
                       0.1804f, 0.5056f, 0.8925f, 0.5461f, 0.9234f, 0.0856f, 0.7938f, 0.9441f,
                       0.9441f, 0.1596f, 0.3087f, 0.1548f, 0.4695f, 0.9939f, 0.6113f, 0.6765f,
                       0.1800f, 0.6750f, 0.2246f, 0.0509f, 0.4601f, 0.8284f, 0.2354f, 0.9752f, 0.8361f,
            0.2585f, 0.4189f,0.7028f,0.7679f,0.5373f,0.7234f,0.2690f,0.0062f,0.0327f,0.0644f,
           0.8428f, 0.9441f,0.9441f,0.9441f,0.3491f,0.5793f,0.5730f,0.1822f,0.6420f,0.9143f});
    assertEquals(expected, output);
}
 
Example #8
Source File: OpExecutionerTests.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
    public void testVPull2() {
        int indexes[] = new int[] {0, 2, 4};
        INDArray array = Nd4j.linspace(1, 25, 25, DataType.DOUBLE).reshape(5, 5);
        INDArray assertion = Nd4j.createUninitialized(DataType.DOUBLE, new long[] {3, 5}, 'c');
        for (int i = 0; i < 3; i++) {
            assertion.putRow(i, array.getRow(indexes[i]));
        }

        INDArray result = Nd4j.pullRows(array, 1, indexes, 'c');

        assertEquals(3, result.rows());
        assertEquals(5, result.columns());
        assertEquals(assertion, result);

//        System.out.println(assertion.toString());
//        System.out.println(result.toString());
    }
 
Example #9
Source File: ShapeTestsC.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testThreeTwoTwo() {
    INDArray threeTwoTwo = Nd4j.linspace(1, 12, 12, DataType.DOUBLE).reshape(3, 2, 2);
    INDArray[] assertions = new INDArray[] {Nd4j.create(new double[] {1, 3}), Nd4j.create(new double[] {2, 4}),
                    Nd4j.create(new double[] {5, 7}), Nd4j.create(new double[] {6, 8}),
                    Nd4j.create(new double[] {9, 11}), Nd4j.create(new double[] {10, 12}),

    };

    assertEquals(assertions.length, threeTwoTwo.tensorsAlongDimension(1));
    for (int i = 0; i < assertions.length; i++) {
        INDArray arr = threeTwoTwo.tensorAlongDimension(i, 1);
        assertEquals(assertions[i], arr);
    }

}
 
Example #10
Source File: OpExecutionerTestsC.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testDimensionMax() {
    INDArray linspace = Nd4j.linspace(1, 6, 6, DataType.DOUBLE).reshape(2, 3);
    int axis = 0;
    INDArray row = linspace.slice(axis);
    Max max = new Max(row);
    double max2 = Nd4j.getExecutioner().execAndReturn(max).getFinalResult().doubleValue();
    assertEquals(3.0, max2, 1e-1);

    Min min = new Min(row);
    double min2 = Nd4j.getExecutioner().execAndReturn(min).getFinalResult().doubleValue();
    assertEquals(1.0, min2, 1e-1);
    Max matrixMax = new Max(linspace, 1);
    INDArray exec2 = Nd4j.getExecutioner().execAndReturn(matrixMax).z();
    assertEquals(Nd4j.create(new double[] {3, 6}), exec2);
}
 
Example #11
Source File: SameDiffOutputTest.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void outputTest(){
    DataSet data = new DataSet(Nd4j.zeros(10, 10), Nd4j.zeros(10, 10));
    SameDiff sd = SameDiff.create();

    SDVariable in = sd.placeHolder("input", DataType.FLOAT, 10, 10);
    SDVariable out = in.add("out", 2);

    TrainingConfig conf = new TrainingConfig.Builder()
            .l2(1e-4)
            .updater(new Sgd(3e-1))
            .dataSetFeatureMapping("input")
            .dataSetLabelMapping()
            .build();

    sd.setTrainingConfig(conf);

    INDArray output = sd.output(data, "out").get("out");

    assertTrue("output != input + 2", output.equalsWithEps(
            Nd4j.zeros(10, 10).add(2).castTo(DataType.FLOAT),
            0.0001));
}
 
Example #12
Source File: SpecialTests.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void reproduceWorkspaceCrash_5(){
    val conf = WorkspaceConfiguration.builder().build();

    val ws = Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread(conf, "WS");

    INDArray arr = Nd4j.create(new double[]{1, 0, 0, 0, 1, 0, 0, 0, 0, 0}, new long[]{1, 10});

    Nd4j.setDefaultDataTypes(DataType.DOUBLE, DataType.DOUBLE);
    assertEquals(DataType.DOUBLE, arr.dataType());

    for( int i=0; i<100; i++ ) {
        try(val ws2 = ws.notifyScopeEntered()) {
            INDArray crash = arr.castTo(DataType.BOOL).castTo(DataType.DOUBLE);
            crash.dup();
        }
    }
}
 
Example #13
Source File: ShapeOpValidation.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testBroadcastDynamicShape1(){

    //Test case: [2,1] and [4]: expect [2,4]
    INDArray out = Nd4j.create(DataType.INT, 2);
    DynamicCustomOp op = DynamicCustomOp.builder("broadcast_dynamic_shape")
            .addInputs(Nd4j.createFromArray(new int[]{2,1}), Nd4j.createFromArray(new int[]{4}))
            .addOutputs(out)
            .build();
    Nd4j.getExecutioner().exec(op);
    assertEquals(Nd4j.createFromArray(new int[]{2,4}), out);

    //Same thing, reversed input order (expect same output)
    op = DynamicCustomOp.builder("broadcast_dynamic_shape")
            .addInputs(Nd4j.createFromArray(new int[]{4}), Nd4j.createFromArray(new int[]{2,1}))
            .addOutputs(out)
            .build();
    Nd4j.getExecutioner().exec(op);
    assertEquals(Nd4j.createFromArray(new int[]{2,4}), out);
}
 
Example #14
Source File: KDTreeTest.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testInsert() {
    int elements = 10;
    List<Double> digits = Arrays.asList(1.0, 0.0, 2.0, 3.0);

    KDTree kdTree = new KDTree(digits.size());
    List<List<Double>> lists = new ArrayList<>();
    for (int i = 0; i < elements; i++) {
        List<Double> thisList = new ArrayList<>(digits.size());
        for (int k = 0; k < digits.size(); k++) {
            thisList.add(digits.get(k) + i);
        }
        lists.add(thisList);
    }

    for (int i = 0; i < elements; i++) {
        double[] features = Doubles.toArray(lists.get(i));
        INDArray ind = Nd4j.create(features, new long[]{1, features.length}, DataType.FLOAT);
        kdTree.insert(ind);
        assertEquals(i + 1, kdTree.size());
    }
}
 
Example #15
Source File: LastTimeStep.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public org.deeplearning4j.nn.api.Layer instantiate(NeuralNetConfiguration conf,
                                                   Collection<TrainingListener> trainingListeners, int layerIndex, INDArray layerParamsView,
                                                   boolean initializeParams, DataType networkDataType) {
    NeuralNetConfiguration conf2 = conf.clone();
    conf2.setLayer(((LastTimeStep) conf2.getLayer()).getUnderlying());
    return new LastTimeStepLayer(underlying.instantiate(conf2, trainingListeners, layerIndex, layerParamsView,
                    initializeParams, networkDataType));
}
 
Example #16
Source File: JCublasNDArrayFactory.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray empty(DataType type) {
    long extras  = ArrayOptionsHelper.setOptionBit(0L, ArrayType.EMPTY);
    extras = ArrayOptionsHelper.setOptionBit(extras, type);
    val shape = Nd4j.getShapeInfoProvider().createShapeInformation(new long[0], new long[0], 1, 'c', extras);
    return new JCublasNDArray(null, (CudaLongDataBuffer) shape.getFirst(), shape.getSecond());
}
 
Example #17
Source File: NDBaseTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testScatterMul() {
    NDBase base = new NDBase();

    //from testScatterOpGradients.
    INDArray x = Nd4j.ones(DataType.DOUBLE, 20, 10).add(1.0);
    INDArray indices = Nd4j.createFromArray(3, 4, 5, 10, 18);
    INDArray updates = Nd4j.ones(DataType.DOUBLE, 5, 10).add(1.0);
    INDArray y = base.scatterMul(x,indices, updates);

    y = y.getColumn(0);
    INDArray  y_exp = Nd4j.createFromArray(2.0, 2.0, 2.0, 4.0, 4.0, 4.0, 2.0, 2.0, 2.0, 2.0, 4.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 4.0, 2.0);
    assertEquals(y_exp, y);
}
 
Example #18
Source File: NDArrayTestsFortran.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadWriteDouble() throws Exception {
    INDArray write = Nd4j.linspace(1, 4, 4, DataType.FLOAT);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(bos);
    Nd4j.write(write, dos);

    ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
    DataInputStream dis = new DataInputStream(bis);
    INDArray read = Nd4j.read(dis);
    assertEquals(write, read);

}
 
Example #19
Source File: SeluBp.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public List<DataType> calculateOutputDataTypes(List<DataType> dataTypes) {
    Preconditions
            .checkArgument(dataTypes != null && dataTypes.size() == 2, "Expected exactly 2 input datatypes, got %s", dataTypes);
    Preconditions.checkArgument(dataTypes.get(0).isFPType() && dataTypes.get(1).isFPType(), "Input datatypes must be floating point, got %s", dataTypes);

    return Collections.singletonList(dataTypes.get(0));
}
 
Example #20
Source File: Cropping3D.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public org.deeplearning4j.nn.api.Layer instantiate(NeuralNetConfiguration conf,
                                                   Collection<TrainingListener> iterationListeners, int layerIndex, INDArray layerParamsView,
                                                   boolean initializeParams, DataType networkDataType) {
    Cropping3DLayer ret = new Cropping3DLayer(conf, networkDataType);
    ret.setListeners(iterationListeners);
    ret.setIndex(layerIndex);
    Map<String, INDArray> paramTable = initializer().init(conf, layerParamsView, initializeParams);
    ret.setParamTable(paramTable);
    ret.setConf(conf);
    return ret;
}
 
Example #21
Source File: GlobalPoolingLayer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public org.deeplearning4j.nn.api.Layer instantiate(NeuralNetConfiguration conf,
                                                   Collection<TrainingListener> trainingListeners, int layerIndex, INDArray layerParamsView,
                                                   boolean initializeParams, DataType networkDataType) {
    org.deeplearning4j.nn.layers.pooling.GlobalPoolingLayer ret =
                    new org.deeplearning4j.nn.layers.pooling.GlobalPoolingLayer(conf, networkDataType);
    ret.setListeners(trainingListeners);
    ret.setIndex(layerIndex);
    ret.setParamsViewArray(layerParamsView);
    Map<String, INDArray> paramTable = initializer().init(conf, layerParamsView, initializeParams);
    ret.setParamTable(paramTable);
    ret.setConf(conf);
    return ret;
}
 
Example #22
Source File: GradientCheckTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
    public void testEmbeddingLayerPreluSimple() {
        Random r = new Random(12345);
        int nExamples = 5;
        INDArray input = Nd4j.zeros(nExamples, 1);
        INDArray labels = Nd4j.zeros(nExamples, 3);
        for (int i = 0; i < nExamples; i++) {
            input.putScalar(i, r.nextInt(4));
            labels.putScalar(new int[] {i, r.nextInt(3)}, 1.0);
        }

        MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().l2(0.2).l1(0.1)
                .dataType(DataType.DOUBLE)
                .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).seed(12345L)
                .list().layer(new EmbeddingLayer.Builder().nIn(4).nOut(3).weightInit(WeightInit.XAVIER)
                                .updater(new NoOp()).build())
                .layer(new PReLULayer.Builder().inputShape(3).sharedAxes(1).updater(new NoOp()).build())
                .layer(new OutputLayer.Builder(LossFunction.MCXENT).nIn(3).nOut(3)
                        .weightInit(WeightInit.XAVIER).dist(new NormalDistribution(0, 1))
                        .updater(new NoOp()).activation(Activation.SOFTMAX).build())
                .build();

        MultiLayerNetwork mln = new MultiLayerNetwork(conf);
        mln.init();

        if (PRINT_RESULTS) {
            System.out.println("testEmbeddingLayerSimple");
//            for (int j = 0; j < mln.getnLayers(); j++)
//                System.out.println("Layer " + j + " # params: " + mln.getLayer(j).numParams());
        }

        boolean gradOK = GradientCheckUtil.checkGradients(mln, DEFAULT_EPS, DEFAULT_MAX_REL_ERROR,
                DEFAULT_MIN_ABS_ERROR, PRINT_RESULTS, RETURN_ON_FIRST_FAILURE, input, labels);

        String msg = "testEmbeddingLayerSimple";
        assertTrue(msg, gradOK);
    }
 
Example #23
Source File: TestSerializationDoubleToFloat.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testSerializationFullArrayNd4jWriteRead() throws Exception {
    int length = 4;

    //WRITE OUT A DOUBLE ARRAY
    //Hack before setting datatype - fix already in r119_various branch
    Nd4j.create(1);
    val initialType = Nd4j.dataType();

    Nd4j.setDataType(DataType.DOUBLE);
    INDArray arr = Nd4j.linspace(1, length, length).reshape('c', 2, 2);
    arr.subi(50.0123456); //assures positive and negative numbers with decimal points

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (DataOutputStream dos = new DataOutputStream(baos)) {
        Nd4j.write(arr, dos);
    }
    byte[] bytes = baos.toByteArray();

    //SET DATA TYPE TO FLOAT and initialize another array with the same contents
    //Nd4j.create(1);
    DataTypeUtil.setDTypeForContext(DataType.FLOAT);
    System.out.println("The data opType is " + Nd4j.dataType());
    INDArray arr1 = Nd4j.linspace(1, length, length).reshape('c', 2, 2);
    arr1.subi(50.0123456);

    log.info("A  ---------------");

    INDArray arr2;
    try (DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bytes))) {
        arr2 = Nd4j.read(dis);
    }

    //System.out.println(new NDArrayStrings(6).format(arr2.sub(arr1).mul(100).div(arr1)));
    arr1 = arr1.castTo(DataType.DOUBLE);
    assertTrue(Transforms.abs(arr1.sub(arr2).div(arr1)).maxNumber().doubleValue() < 0.01);
}
 
Example #24
Source File: ResizeBilinear.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public List<DataType> calculateOutputDataTypes(List<DataType> inputDataTypes){
    Preconditions.checkState(inputDataTypes != null && (inputDataTypes.size() == 1 || inputDataTypes.size() == 2),
            "Expected 1 or 2 input datatypes for %s, got %s", getClass(), inputDataTypes);
    if(inputDataTypes.get(0).isFPType())
        return Collections.singletonList(inputDataTypes.get(0));
    return Collections.singletonList(Nd4j.defaultFloatingPointType());
}
 
Example #25
Source File: NDArrayTestsFortran.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
@Ignore
public void testTensorDot() {
    INDArray oneThroughSixty = Nd4j.arange(60).reshape('f', 3, 4, 5).castTo(DataType.DOUBLE);
    INDArray oneThroughTwentyFour = Nd4j.arange(24).reshape('f', 4, 3, 2).castTo(DataType.DOUBLE);
    INDArray result = Nd4j.tensorMmul(oneThroughSixty, oneThroughTwentyFour, new int[][] {{1, 0}, {0, 1}});
    assertArrayEquals(new long[] {5, 2}, result.shape());
    INDArray assertion = Nd4j.create(new double[][] {{440., 1232.}, {1232., 3752.}, {2024., 6272.}, {2816., 8792.},
                    {3608., 11312.}});
    assertEquals(assertion, result);

}
 
Example #26
Source File: PythonNumpyJobTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testNumpyJobBasic(){
    PythonContextManager.deleteNonMainContexts();
    List<PythonVariable> inputs = new ArrayList<>();
    INDArray x = Nd4j.ones(dataType, 2, 3);
    INDArray y = Nd4j.zeros(dataType, 2, 3);
    INDArray z = (dataType == DataType.BOOL)?x:x.mul(y.add(2));
    z = (dataType == DataType.BFLOAT16)? z.castTo(DataType.FLOAT): z;
    PythonType<INDArray> arrType = PythonTypes.get("numpy.ndarray");
    inputs.add(new PythonVariable<>("x", arrType, x));
    inputs.add(new PythonVariable<>("y", arrType, y));
    List<PythonVariable> outputs = new ArrayList<>();
    PythonVariable<INDArray> output = new PythonVariable<>("z", arrType);
    outputs.add(output);
    String code = (dataType == DataType.BOOL)?"z = x":"z = x * (y + 2)";

    PythonJob job = new PythonJob("job1", code, false);

    job.exec(inputs, outputs);

    INDArray z2 = output.getValue();

    if (dataType == DataType.BFLOAT16){
        z2  = z2.castTo(DataType.FLOAT);
    }

    Assert.assertEquals(z, z2);

}
 
Example #27
Source File: MergeAddOp.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public List<DataType> calculateOutputDataTypes(List<DataType> dataTypes){
    DataType first = dataTypes.get(0);
    for( int i=1; i<dataTypes.size(); i++ ){
        Preconditions.checkState(first == dataTypes.get(i), "Expected all input datatypes to be the same: first input is %s, input %s is %s", first, i, dataTypes.get(i));
    }
    return Collections.singletonList(first);
}
 
Example #28
Source File: SpecialTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
    public void reproduceWorkspaceCrash_2(){
        val dtypes = new DataType[]{DataType.DOUBLE, DataType.FLOAT, DataType.HALF, DataType.LONG, DataType.INT, DataType.SHORT, DataType.BYTE, DataType.UBYTE, DataType.BOOL};
        for (val dX : dtypes) {
            for (val dZ: dtypes) {
                val array = Nd4j.create(dX, 2, 5).assign(1);

//                log.info("Trying to cast {} to {}", dX, dZ);
                val casted = array.castTo(dZ);

                val exp = Nd4j.create(dZ, 2, 5).assign(1);
                assertEquals(exp, casted);
            }
        }
    }
 
Example #29
Source File: MiscOpValidation.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testOneHot1(){
    List<String> failed = new ArrayList<>();

    //Because it's on the diagonal, should be the same for all axis args...
    for( int i=-1; i<=0; i++ ) {
        INDArray indicesArr = Nd4j.createFromArray(0, 1, 2);
        int depth = 3;

        SameDiff sd = SameDiff.create();
        SDVariable indices = sd.constant(indicesArr);
        SDVariable oneHot = sd.oneHot(indices, depth, i, 1.0, 0.0, DataType.DOUBLE);

        INDArray exp = Nd4j.eye(3).castTo(DataType.DOUBLE);

        String msg = "Axis: " + i;
        log.info("Test case: " + msg);

        String err = OpValidation.validate(new TestCase(sd)
                .testName(msg)
                .gradientCheck(false)
                .expected(oneHot, exp));

        if(err != null){
            failed.add(err);
        }
    }
    assertEquals(failed.toString(), 0, failed.size());
}
 
Example #30
Source File: LoneTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
    public void checkSliceofSlice() {
        /*
            Issue 1: Slice of slice with c order and f order views are not equal

            Comment out assert and run then -> Issue 2: Index out of bound exception with certain shapes when accessing elements with getDouble() in f order
            (looks like problem is when rank-1==1) eg. 1,2,1 and 2,2,1
         */
        int[] ranksToCheck = new int[]{2, 3, 4, 5};
        for (int rank = 0; rank < ranksToCheck.length; rank++) {
//            log.info("\nRunning through rank " + ranksToCheck[rank]);
            List<Pair<INDArray, String>> allF = NDArrayCreationUtil.getTestMatricesWithVaryingShapes(ranksToCheck[rank], 'f', DataType.FLOAT);
            Iterator<Pair<INDArray, String>> iter = allF.iterator();
            while (iter.hasNext()) {
                Pair<INDArray, String> currentPair = iter.next();
                INDArray origArrayF = currentPair.getFirst();
                INDArray sameArrayC = origArrayF.dup('c');
//                log.info("\nLooping through slices for shape " + currentPair.getSecond());
//                log.info("\nOriginal array:\n" + origArrayF);
                origArrayF.toString();
                INDArray viewF = origArrayF.slice(0);
                INDArray viewC = sameArrayC.slice(0);
//                log.info("\nSlice 0, C order:\n" + viewC.toString());
//                log.info("\nSlice 0, F order:\n" + viewF.toString());
                viewC.toString();
                viewF.toString();
                for (int i = 0; i < viewF.slices(); i++) {
                    //assertEquals(viewF.slice(i),viewC.slice(i));
                    for (int j = 0; j < viewF.slice(i).length(); j++) {
                        //if (j>0) break;
//                        log.info("\nC order slice " + i + ", element 0 :" + viewC.slice(i).getDouble(j)); //C order is fine
//                        log.info("\nF order slice " + i + ", element 0 :" + viewF.slice(i).getDouble(j)); //throws index out of bound err on F order
                        viewC.slice(i).getDouble(j);
                        viewF.slice(i).getDouble(j);
                    }
                }
            }
        }
    }