Java Code Examples for org.nd4j.linalg.api.ndarray.INDArray#toFloatVector()

The following examples show how to use org.nd4j.linalg.api.ndarray.INDArray#toFloatVector() . 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 testBitmapEncoding6() {
    Nd4j.getRandom().setSeed(119);
    INDArray initial = Nd4j.rand(new int[]{10000}, -1, 1, Nd4j.getRandom());
    INDArray exp_1 = initial.dup();

    INDArray enc = Nd4j.getExecutioner().bitmapEncode(initial, 1e-3);
    //assertEquals(exp_0, initial);

    Nd4j.getExecutioner().bitmapDecode(enc, initial);

    val f0 = exp_1.toFloatVector();
    val f1 = initial.toFloatVector();

    assertArrayEquals(f0, f1, 1e-5f);

    assertEquals(exp_1, initial);
}
 
Example 2
Source File: HyperRect.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public void enlargeTo(INDArray point) {
    float[] pointAsArray = point.toFloatVector();
    for (int i = 0; i < lowerEnds.length; i++) {
        float p = pointAsArray[i];
        if (lowerEnds[i] > p)
            lowerEnds[i] = p;
        else if (higherEnds[i] < p)
            higherEnds[i] = p;
    }
}
 
Example 3
Source File: ND4JConverters.java    From konduit-serving with Apache License 2.0 4 votes vote down vote up
@Override
public float[] convert(INDArray from) {
    return from.toFloatVector();
}
 
Example 4
Source File: JsonModelServerTest.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testSameDiffMnist() throws Exception {

    SameDiff sd = SameDiff.create();
    SDVariable in = sd.placeHolder("in", DataType.FLOAT, -1, 28*28);
    SDVariable w = sd.var("w", Nd4j.rand(DataType.FLOAT, 28*28, 10));
    SDVariable b = sd.var("b", Nd4j.rand(DataType.FLOAT, 1, 10));
    SDVariable sm = sd.nn.softmax("softmax", in.mmul(w).add(b), -1);

    val server = new JsonModelServer.Builder<float[], Integer>(sd)
            .outputSerializer( new IntSerde())
            .inputDeserializer(new FloatSerde())
            .inferenceAdapter(new InferenceAdapter<float[], Integer>() {
                @Override
                public MultiDataSet apply(float[] input) {
                    return new MultiDataSet(Nd4j.create(input, 1, input.length), null);
                }

                @Override
                public Integer apply(INDArray... nnOutput) {
                    return nnOutput[0].argMax().getInt(0);
                }
            })
            .orderedInputNodes("in")
            .orderedOutputNodes("softmax")
            .port(PORT+1)
            .build();

    val client = JsonRemoteInference.<float[], Integer>builder()
            .endpointAddress("http://localhost:" + (PORT+1) + "/v1/serving")
            .outputDeserializer(new IntSerde())
            .inputSerializer( new FloatSerde())
            .build();

    try{
        server.start();
        for( int i=0; i<10; i++ ){
            INDArray f = Nd4j.rand(DataType.FLOAT, 1, 28*28);
            INDArray exp = sd.output(Collections.singletonMap("in", f), "softmax").get("softmax");
            float[] fArr = f.toFloatVector();
            int out = client.predict(fArr);
            assertEquals(exp.argMax().getInt(0), out);
        }
    } finally {
        server.stop();
    }
}
 
Example 5
Source File: JsonModelServerTest.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testMlnMnist() throws Exception {

    MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
            .list()
            .layer(new DenseLayer.Builder().nIn(784).nOut(10).build())
            .layer(new LossLayer.Builder().activation(Activation.SOFTMAX).build())
            .build();

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

    val server = new JsonModelServer.Builder<float[], Integer>(net)
            .outputSerializer( new IntSerde())
            .inputDeserializer(new FloatSerde())
            .inferenceAdapter(new InferenceAdapter<float[], Integer>() {
                @Override
                public MultiDataSet apply(float[] input) {
                    return new MultiDataSet(Nd4j.create(input, 1, input.length), null);
                }

                @Override
                public Integer apply(INDArray... nnOutput) {
                    return nnOutput[0].argMax().getInt(0);
                }
            })
            .orderedInputNodes("in")
            .orderedOutputNodes("softmax")
            .port(PORT + 1)
            .inferenceMode(SEQUENTIAL)
            .numWorkers(2)
            .build();

    val client = JsonRemoteInference.<float[], Integer>builder()
            .endpointAddress("http://localhost:" + (PORT + 1) + "/v1/serving")
            .outputDeserializer(new IntSerde())
            .inputSerializer( new FloatSerde())
            .build();

    try {
        server.start();
        for (int i = 0; i < 10; i++) {
            INDArray f = Nd4j.rand(DataType.FLOAT, 1, 28 * 28);
            INDArray exp = net.output(f);
            float[] fArr = f.toFloatVector();
            int out = client.predict(fArr);
            assertEquals(exp.argMax().getInt(0), out);
        }
    } catch (Exception e){
        log.error("",e);
        throw e;
    } finally {
        server.stop();
    }
}