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

The following examples show how to use org.nd4j.linalg.api.ndarray.INDArray#getFloat() . 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: BlasBufferUtil.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the float data
 * for this ndarray.
 * If possible (the offset is 0 representing the whole buffer)
 * it will return a direct reference to the underlying array
 * @param buf the ndarray to get the data for
 * @return the float data for this ndarray
 */
public static float[] getFloatData(INDArray buf) {
    if (buf.data().dataType() != DataBuffer.Type.FLOAT)
        throw new IllegalArgumentException("Float data must be obtained from a float buffer");

    if (buf.data().allocationMode() == DataBuffer.AllocationMode.HEAP) {
        return buf.data().asFloat();
    } else {
        float[] ret = new float[(int) buf.length()];
        INDArray linear = buf.linearView();

        for (int i = 0; i < buf.length(); i++)
            ret[i] = linear.getFloat(i);
        return ret;
    }
}
 
Example 2
Source File: BlasBufferUtil.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the float data
 * for this ndarray.
 * If possible (the offset is 0 representing the whole buffer)
 * it will return a direct reference to the underlying array
 * @param buf the ndarray to get the data for
 * @return the float data for this ndarray
 */
public static float[] getFloatData(INDArray buf) {
    if (buf.data().dataType() != DataType.FLOAT)
        throw new IllegalArgumentException("Float data must be obtained from a float buffer");

    if (buf.data().allocationMode() == DataBuffer.AllocationMode.HEAP) {
        return buf.data().asFloat();
    } else {
        float[] ret = new float[(int) buf.length()];
        INDArray linear = buf.reshape(-1);

        for (int i = 0; i < buf.length(); i++)
            ret[i] = linear.getFloat(i);
        return ret;
    }
}
 
Example 3
Source File: NDArrayUtil.java    From nd4j with Apache License 2.0 6 votes vote down vote up
public static long[] toLongs(INDArray n) {
    if (n instanceof IComplexNDArray)
        throw new IllegalArgumentException("Unable to convert complex array");

    if (n.length() > Integer.MAX_VALUE)
        throw new ND4JIllegalStateException("Can't convert INDArray with length > Integer.MAX_VALUE");

    n = n.linearView();

    // FIXME: int cast
    long[] ret = new long[(int) n.length()];
    for (int i = 0; i < n.length(); i++)
        ret[i] = (long) n.getFloat(i);

    return ret;
}
 
Example 4
Source File: ImageClassifier.java    From java-ml-projects with Apache License 2.0 6 votes vote down vote up
public Map<String, Double> classifyImage(InputStream imageIS) {
	INDArray imageArray = imageToArray(imageIS, width, height, channels);
	INDArray output = getOutput(imageArray);
	Map<String, Double> predictions = new HashMap<>();
	// this may not be compatible with all kind of results
	float eps = 0.1f;
	for (int i = 0; i < output.columns(); i++) {
		Float v = output.getFloat(i);
		if (Math.abs(v - eps) > eps) {
			String label = "pos " + i;
			if (i < labels.size()) {
				label = labels.get(i);
			}
			predictions.put(label, v.doubleValue());
		}
	}
	return predictions;
}
 
Example 5
Source File: ImageClassifierServiceImpl.java    From java-ml-projects with Apache License 2.0 6 votes vote down vote up
public ClassificationResults classify(InputStream imageStream) {
	INDArray imageArray = imageToArray(imageStream, inputWidth, inputHeight, inputChannels);
	INDArray output = getOutput(model, imageArray);
	ClassificationResults results = new ClassificationResults();
	Map<String, Double> predictions = new HashMap<>();
	for (int i = 0; i < output.columns(); i++) {
		Float v = output.getFloat(i);
		if (Math.abs(v - EPS) > EPS) {
			String label = "label " + i;
			if (i < classifierInfo.getLabels().size()) {
				label = classifierInfo.getLabels().get(i);
			}
			predictions.put(label, v.doubleValue());
		}
	}
	results.setPredictions(predictions);
	results.setPrediction(highestScorePrediction(predictions));
	return results;
}
 
Example 6
Source File: MultiLayerTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public static float[] asFloat(INDArray arr) {
    long len = arr.length();

    float[] f = new float[(int) len];
    for (int i = 0; i < len; i++)
        f[i] = arr.getFloat(i);
    return f;
}
 
Example 7
Source File: CompressionMagicTests.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testMagicDecompression4() throws Exception {
    INDArray array = Nd4j.linspace(1, 100, 2500);

    INDArray compressed = Nd4j.getCompressor().compress(array, "GZIP");

    for (int cnt = 0; cnt < array.length(); cnt++) {
        float a = array.getFloat(cnt);
        float c = compressed.getFloat(cnt);
        assertEquals(a, c, 0.01f);
    }

}
 
Example 8
Source File: NDArrayTestsFortran.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetVsGetScalar() {
    INDArray a = Nd4j.linspace(1, 4, 4).reshape(2, 2);
    float element = a.getFloat(0, 1);
    double element2 = a.getDouble(0, 1);
    assertEquals(element, element2, 1e-1);
    INDArray a2 = Nd4j.linspace(1, 4, 4).reshape(2, 2);
    float element23 = a2.getFloat(0, 1);
    double element22 = a2.getDouble(0, 1);
    assertEquals(element23, element22, 1e-1);

}
 
Example 9
Source File: ImageNetLabels.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Given predictions from the trained model this method will return a string
 * listing the top five matches and the respective probabilities
 * @param predictions
 * @return
 */
public String decodePredictions(INDArray predictions) {
    Preconditions.checkState(predictions.size(1) == predictionLabels.size(), "Invalid input array:" +
            " expected array with size(1) equal to numLabels (%s), got array with shape %s", predictionLabels.size(), predictions.shape());

    String predictionDescription = "";
    int[] top5 = new int[5];
    float[] top5Prob = new float[5];

    //brute force collect top 5
    int i = 0;
    for (int batch = 0; batch < predictions.size(0); batch++) {
        predictionDescription += "Predictions for batch ";
        if (predictions.size(0) > 1) {
            predictionDescription += String.valueOf(batch);
        }
        predictionDescription += " :";
        INDArray currentBatch = predictions.getRow(batch).dup();
        while (i < 5) {
            top5[i] = Nd4j.argMax(currentBatch, 1).getInt(0);
            top5Prob[i] = currentBatch.getFloat(batch, top5[i]);
            currentBatch.putScalar(0, top5[i], 0);
            predictionDescription += "\n\t" + String.format("%3f", top5Prob[i] * 100) + "%, "
                            + predictionLabels.get(top5[i]);
            i++;
        }
    }
    return predictionDescription;
}
 
Example 10
Source File: NDArrayUtil.java    From nd4j with Apache License 2.0 5 votes vote down vote up
public static int[] toInts(INDArray n) {
    if (n instanceof IComplexNDArray)
        throw new IllegalArgumentException("Unable to convert complex array");

    if (n.length() > Integer.MAX_VALUE)
        throw new ND4JIllegalStateException("Can't convert INDArray with length > Integer.MAX_VALUE");

    n = n.linearView();
    int[] ret = new int[(int) n.length()];
    for (int i = 0; i < n.length(); i++)
        ret[i] = (int) n.getFloat(i);
    return ret;
}
 
Example 11
Source File: PolicyNetUtil.java    From FancyBing with GNU General Public License v3.0 5 votes vote down vote up
public static int[][] getMoves(INDArray guess, int rotationMode, boolean[] legalPoints) {
// Limit the top moves < 10 can improve the performance, but also increase the blind point risk
float accuProbability = Config.POLICY_NETWORK_TOP_PROBABILITY;
int[][] moves = new int[Config.POLICY_NETWORK_MAX_TOP_N][2];
INDArray guessIndex;
float accumProb = 0.0f;
double prob = 0.0f;
int count = 0;
int dataIndex = 0;
for (int i = 0; i < Config.POLICY_NETWORK_MAX_TOP_N; i++) {
	guessIndex = Nd4j.argMax(guess, 1);
	dataIndex = (int) guessIndex.getFloat(0);
	prob = guess.getDouble(dataIndex);
	if (prob < 0.01 && count > 0) { // ignore the low probability move
		break;
	}
	
	moves[i][0] = Board.getOriginIndex(dataIndex, rotationMode);
	moves[i][1] = (int) (prob * 1000 + 0.5);
	count++;
	if (!legalPoints[moves[i][0]]) {
		moves[i][0] = Board.PASS;
	}
	accumProb += prob;
	if (accumProb >= accuProbability) {
		break;
	}
	guess.putScalar(dataIndex, -1.0f);
}

int[][] rs = new int[count][2];
for (int i = 0; i < rs.length; i++) {
	rs[i][0] = moves[i][0];
	rs[i][1] = moves[i][1];
}
return rs;
  }
 
Example 12
Source File: HyperRect.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public HyperRect getUpper(INDArray hPoint, int desc) {
    //Interval interval = points.get(desc);
    float higher = higherEnds[desc];
    float d = hPoint.getFloat(desc);
    if (higher < d)
        return null;
    HyperRect ret = new HyperRect(lowerEnds,higherEnds);
    if (ret.lowerEnds[desc] < d)
        ret.lowerEnds[desc] = d;
    return ret;
}
 
Example 13
Source File: CompressionMagicTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testMagicDecompression4() {
    INDArray array = Nd4j.linspace(1, 100, 2500, DataType.FLOAT);

    INDArray compressed = Nd4j.getCompressor().compress(array, "GZIP");

    for (int cnt = 0; cnt < array.length(); cnt++) {
        float a = array.getFloat(cnt);
        float c = compressed.getFloat(cnt);
        assertEquals(a, c, 0.01f);
    }

}
 
Example 14
Source File: NDArrayTestsFortran.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetVsGetScalar() {
    INDArray a = Nd4j.linspace(1, 4, 4, DataType.DOUBLE).reshape(2, 2);
    float element = a.getFloat(0, 1);
    double element2 = a.getDouble(0, 1);
    assertEquals(element, element2, 1e-1);
    INDArray a2 = Nd4j.linspace(1, 4, 4, DataType.DOUBLE).reshape(2, 2);
    float element23 = a2.getFloat(0, 1);
    double element22 = a2.getDouble(0, 1);
    assertEquals(element23, element22, 1e-1);

}
 
Example 15
Source File: NDArrayTestsFortran.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testPutRow() {
    INDArray d = Nd4j.linspace(1, 4, 4).reshape(2, 2);
    INDArray n = d.dup();

    //works fine according to matlab, let's go with it..
    //reproduce with:  A = newShapeNoCopy(linspace(1,4,4),[2 2 ]);
    //A(1,2) % 1 index based
    float nFirst = 3;
    float dFirst = d.getFloat(0, 1);
    assertEquals(nFirst, dFirst, 1e-1);
    assertEquals(d, n);
    assertEquals(true, Arrays.equals(new long[] {2, 2}, n.shape()));

    INDArray newRow = Nd4j.linspace(5, 6, 2);
    n.putRow(0, newRow);
    d.putRow(0, newRow);


    INDArray testRow = n.getRow(0);
    assertEquals(newRow.length(), testRow.length());
    assertEquals(true, Shape.shapeEquals(new long[] {1, 2}, testRow.shape()));


    INDArray nLast = Nd4j.create(Nd4j.linspace(1, 4, 4).data(), new long[] {2, 2});
    INDArray row = nLast.getRow(1);
    INDArray row1 = Nd4j.create(new double[] {2, 4}, new long[] {1, 2});
    assertEquals(row, row1);


    INDArray arr = Nd4j.create(new long[] {3, 2});
    INDArray evenRow = Nd4j.create(new double[] {1, 2}, new long[] {1, 2});
    arr.putRow(0, evenRow);
    INDArray firstRow = arr.getRow(0);
    assertEquals(true, Shape.shapeEquals(new long[] {1, 2}, firstRow.shape()));
    INDArray testRowEven = arr.getRow(0);
    assertEquals(evenRow, testRowEven);


    INDArray row12 = Nd4j.create(new double[] {5, 6}, new long[] {1, 2});
    arr.putRow(1, row12);
    assertEquals(true, Shape.shapeEquals(new long[] {1, 2}, arr.getRow(0).shape()));
    INDArray testRow1 = arr.getRow(1);
    assertEquals(row12, testRow1);


    INDArray multiSliceTest = Nd4j.create(Nd4j.linspace(1, 16, 16).data(), new long[] {4, 2, 2});
    INDArray test = Nd4j.create(new double[] {2, 10}, new long[] {1, 2});
    INDArray test2 = Nd4j.create(new double[] {6, 14}, new long[] {1, 2});

    INDArray multiSliceRow1 = multiSliceTest.slice(1).getRow(0);
    INDArray multiSliceRow2 = multiSliceTest.slice(1).getRow(1);

    assertEquals(test, multiSliceRow1);
    assertEquals(test2, multiSliceRow2);

}
 
Example 16
Source File: CustomOpsTests.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
    public void testUpsampling2dBackprop(){

        Nd4j.getRandom().setSeed(12345);
        int c = 2;
        int[] sz = {2,2};
        long[] inSize = {1, c, 3, 3};
        INDArray eps = Nd4j.rand(DataType.FLOAT, 1, c, sz[0] * inSize[2], sz[1] * inSize[3]);

        INDArray input = Nd4j.create(inSize);    //Unused, not sure why this is even an arg...
        INDArray exp = Nd4j.create(DataType.FLOAT, inSize);

        for( int ch=0; ch<c; ch++ ) {
            for( int h=0; h<eps.size(2); h++ ){
                for( int w=0; w<eps.size(3); w++ ){
                    int[] from = new int[]{0, ch, h, w};
                    int[] to = new int[]{0, ch, h/sz[0], w/sz[1]};
                    float add = eps.getFloat(from);
                    float current = exp.getFloat(to);
                    exp.putScalar(to, current + add);
                }
            }
        }

//        System.out.println("Eps:");
//        System.out.println(eps.shapeInfoToString());
//        System.out.println(Arrays.toString(eps.data().asFloat()));

//        System.out.println("Expected:");
//        System.out.println(exp.shapeInfoToString());
//        System.out.println(Arrays.toString(exp.data().asFloat()));

        DynamicCustomOp op = DynamicCustomOp.builder("upsampling2d_bp")
                .addInputs(input, eps)
                .addOutputs(exp.ulike())
                .addIntegerArguments(1) //1 = NCHW
                .build();

        Nd4j.exec(op);

        INDArray act = op.getOutputArgument(0);
        assertEquals(exp, act);
    }
 
Example 17
Source File: NDArrayTestsFortran.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testPutRow() {
    INDArray d = Nd4j.linspace(1, 4, 4, DataType.DOUBLE).reshape(2, 2);
    INDArray n = d.dup();

    //works fine according to matlab, let's go with it..
    //reproduce with:  A = newShapeNoCopy(linspace(1,4,4),[2 2 ]);
    //A(1,2) % 1 index based
    float nFirst = 3;
    float dFirst = d.getFloat(0, 1);
    assertEquals(nFirst, dFirst, 1e-1);
    assertEquals(d, n);
    assertEquals(true, Arrays.equals(new long[] {2, 2}, n.shape()));

    INDArray newRow = Nd4j.linspace(5, 6, 2, DataType.DOUBLE);
    n.putRow(0, newRow);
    d.putRow(0, newRow);


    INDArray testRow = n.getRow(0);
    assertEquals(newRow.length(), testRow.length());
    assertEquals(true, Shape.shapeEquals(new long[] {1, 2}, testRow.shape()));


    INDArray nLast = Nd4j.create(Nd4j.linspace(1, 4, 4, DataType.DOUBLE).data(), new long[] {2, 2}).castTo(DataType.DOUBLE);
    INDArray row = nLast.getRow(1);
    INDArray row1 = Nd4j.create(new double[] {2, 4});
    assertEquals(row, row1);


    INDArray arr = Nd4j.create(new long[] {3, 2}).castTo(DataType.DOUBLE);
    INDArray evenRow = Nd4j.create(new double[] {1, 2});
    arr.putRow(0, evenRow);
    INDArray firstRow = arr.getRow(0);
    assertEquals(true, Shape.shapeEquals(new long[] {1, 2}, firstRow.shape()));
    INDArray testRowEven = arr.getRow(0);
    assertEquals(evenRow, testRowEven);


    INDArray row12 = Nd4j.create(new double[] {5, 6});
    arr.putRow(1, row12);
    assertEquals(true, Shape.shapeEquals(new long[] {1, 2}, arr.getRow(0).shape()));
    INDArray testRow1 = arr.getRow(1);
    assertEquals(row12, testRow1);


    INDArray multiSliceTest = Nd4j.create(Nd4j.linspace(1, 16, 16, DataType.DOUBLE).data(), new long[] {4, 2, 2}).castTo(DataType.DOUBLE);
    INDArray test = Nd4j.create(new double[] {2, 10});
    INDArray test2 = Nd4j.create(new double[] {6, 14});

    INDArray multiSliceRow1 = multiSliceTest.slice(1).getRow(0);
    INDArray multiSliceRow2 = multiSliceTest.slice(1).getRow(1);

    assertEquals(test, multiSliceRow1);
    assertEquals(test2, multiSliceRow2);
}
 
Example 18
Source File: DeepFMInputLayer.java    From jstarcraft-rns with Apache License 2.0 4 votes vote down vote up
@Override
public Pair<Gradient, INDArray> backpropGradient(INDArray epsilon, LayerWorkspaceMgr workspaceMgr) {
    assertInputSet(true);
    // If this layer is layer L, then epsilon is (w^(L+1)*(d^(L+1))^T) (or
    // equivalent)
    INDArray z = preOutput(true, workspaceMgr); // Note: using preOutput(INDArray) can't be used as this does a setInput(input)
                                                // and resets the 'appliedDropout' flag
    // INDArray activationDerivative =
    // Nd4j.getExecutioner().execAndReturn(Nd4j.getOpFactory().createTransform(conf().getLayer().getActivationFunction(),
    // z).derivative());
    // INDArray activationDerivative =
    // conf().getLayer().getActivationFn().getGradient(z);
    // INDArray delta = epsilon.muli(activationDerivative);
    INDArray delta = layerConf().getActivationFn().backprop(z, epsilon).getFirst(); // TODO handle activation function params

    if (maskArray != null) {
        applyMask(delta);
    }

    Gradient ret = new DefaultGradient();

    INDArray weightGrad = gradientViews.get(DefaultParamInitializer.WEIGHT_KEY); // f order
    weightGrad.assign(0F);
    for (int index = 0; index < input.rows(); index++) {
        for (int column = 0; column < delta.columns(); column++) {
            int cursor = 0;
            for (int dimension = 0; dimension < dimensionSizes.length; dimension++) {
                int point = cursor + input.getInt(index, dimension);
                float value = weightGrad.getFloat(point, column);
                value += delta.getFloat(index, column);
                weightGrad.put(point, column, value);
                cursor += dimensionSizes[dimension];
            }
        }
    }
    ret.gradientForVariable().put(DefaultParamInitializer.WEIGHT_KEY, weightGrad);

    if (hasBias()) {
        INDArray biasGrad = gradientViews.get(DefaultParamInitializer.BIAS_KEY);
        delta.sum(biasGrad, 0); // biasGrad is initialized/zeroed first
        ret.gradientForVariable().put(DefaultParamInitializer.BIAS_KEY, biasGrad);
    }

    INDArray W = getParamWithNoise(DefaultParamInitializer.WEIGHT_KEY, true, workspaceMgr);
    INDArray epsilonNext = workspaceMgr.createUninitialized(ArrayType.ACTIVATION_GRAD, new long[] { W.size(0), delta.size(0) }, 'f');
    epsilonNext = W.mmuli(delta.transpose(), epsilonNext).transpose(); // W.mmul(delta.transpose()).transpose();

    weightNoiseParams.clear();

    epsilonNext = backpropDropOutIfPresent(epsilonNext);
    return new Pair<>(ret, epsilonNext);
}
 
Example 19
Source File: GridExecutionerTest.java    From nd4j with Apache License 2.0 3 votes vote down vote up
@Test
    public void testGridFlow7() throws Exception {
        CudaGridExecutioner executioner = new CudaGridExecutioner();

        INDArray arrayX = Nd4j.create(new float[] {0f, 0f, 0f});
        INDArray arrayY1 = Nd4j.create(new float[] {-1f, -1f, 1f});
        INDArray arrayY2 = Nd4j.create(new float[] {1f, 1f, 1f});
        INDArray exp = Nd4j.create(new float[] {1f, 1f, 1f});

        Set opA = new Set(arrayX, arrayY1, arrayX, arrayY1.length());

        executioner.exec(opA);

        assertEquals(1, executioner.getQueueLength());

        Set opB = new Set(arrayX, arrayY2, arrayX, arrayY1.length());
        executioner.exec(opB);

        assertEquals(1, executioner.getQueueLength());

        assertEquals(1, executioner.getExecutionCounter());
        //System.out.println("---------------------------");

        executioner.flushQueueBlocking();
        arrayX.getFloat(0);


        // it should be 0, because getFloat() should trigger flushQueue
        assertEquals(2, executioner.getExecutionCounter());
        assertEquals(0, executioner.getQueueLength());

        assertEquals(1f, arrayX.getFloat(0), 0.1f);
//        assertEquals(exp, arrayX);
    }
 
Example 20
Source File: NDArrayIndex.java    From nd4j with Apache License 2.0 3 votes vote down vote up
/**
 * Create from a matrix. The rows are the indices
 * The columns are the individual element in each ndarrayindex
 *
 * @param index the matrix to getFloat indices from
 * @return the indices to getFloat
 */
public static INDArrayIndex[] create(INDArray index) {

    if (index.isMatrix()) {

        if (index.rows() > Integer.MAX_VALUE)
            throw new ND4JArraySizeException();

        NDArrayIndex[] ret = new NDArrayIndex[(int) index.rows()];
        for (int i = 0; i < index.rows(); i++) {
            INDArray row = index.getRow(i);
            val nums = new long[(int) index.getRow(i).columns()];
            for (int j = 0; j < row.columns(); j++) {
                nums[j] = (int) row.getFloat(j);
            }

            NDArrayIndex idx = new NDArrayIndex(nums);
            ret[i] = idx;

        }


        return ret;

    } else if (index.isVector()) {
        long[] indices = NDArrayUtil.toLongs(index);
        return new NDArrayIndex[] {new NDArrayIndex(indices)};
    }


    throw new IllegalArgumentException("Passed in ndarray must be a matrix or a vector");

}