public static void printMatrixFullPrecision(INDArray matrix) { boolean floatType = (matrix.data().dataType() == DataBuffer.Type.FLOAT); printNDArrayHeader(matrix); long[] shape = matrix.shape(); for (int i = 0; i < shape[0]; i++) { for (int j = 0; j < shape[1]; j++) { if (floatType) System.out.print(matrix.getFloat(i, j)); else System.out.print(matrix.getDouble(i, j)); if (j != shape[1] - 1) System.out.print(", "); else System.out.println(); } } }
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; }
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; }
/** * 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; } }
/** * Reverses the passed in matrix such that m[0] becomes m[m.length - 1] etc * * @param reverse the matrix to reverse * @return the reversed matrix */ @Override public INDArray reverse(INDArray reverse) { // FIXME: native method should be used instead INDArray rev = reverse.linearView(); INDArray ret = Nd4j.create(rev.shape()); int count = 0; for (long i = rev.length() - 1; i >= 0; i--) { ret.putScalar(count++, rev.getFloat(i)); } return ret.reshape(reverse.shape()); }
public static void checkForNaN(INDArray z) { if (Nd4j.getExecutioner().getProfilingMode() != OpExecutioner.ProfilingMode.NAN_PANIC && Nd4j.getExecutioner().getProfilingMode() != OpExecutioner.ProfilingMode.ANY_PANIC) return; int match = 0; if (!z.isScalar()) { MatchCondition condition = new MatchCondition(z, Conditions.isNan()); match = Nd4j.getExecutioner().exec(condition, Integer.MAX_VALUE).getInt(0); } else { if (z.data().dataType() == DataBuffer.Type.DOUBLE) { if (Double.isNaN(z.getDouble(0))) match = 1; } else { if (Float.isNaN(z.getFloat(0))) match = 1; } } if (match > 0) throw new ND4JIllegalStateException("P.A.N.I.C.! Op.Z() contains " + match + " NaN value(s): "); }
public static void checkForInf(INDArray z) { if (Nd4j.getExecutioner().getProfilingMode() != OpExecutioner.ProfilingMode.INF_PANIC && Nd4j.getExecutioner().getProfilingMode() != OpExecutioner.ProfilingMode.ANY_PANIC) return; int match = 0; if (!z.isScalar()) { MatchCondition condition = new MatchCondition(z, Conditions.isInfinite()); match = Nd4j.getExecutioner().exec(condition, Integer.MAX_VALUE).getInt(0); } else { if (z.data().dataType() == DataBuffer.Type.DOUBLE) { if (Double.isInfinite(z.getDouble(0))) match = 1; } else { if (Float.isInfinite(z.getFloat(0))) match = 1; } } if (match > 0) throw new ND4JIllegalStateException("P.A.N.I.C.! Op.Z() contains " + match + " Inf value(s)"); }
@Override public INDArray getUFactor(INDArray A) { // FIXME: int cast if (A.rows() > Integer.MAX_VALUE || A.columns() > Integer.MAX_VALUE) throw new ND4JArraySizeException(); int m = (int) A.rows(); int n = (int) A.columns(); INDArray U = Nd4j.create(n, n); for (int r = 0; r < n; r++) { for (int c = 0; c < n; c++) { if (r <= c && r < m && c < n) { U.putScalar(r, c, A.getFloat(r, c)); } else { U.putScalar(r, c, 0.f); } } } return U; }
on.setValueFor(currentField,tensor.getFloat(0));
@Override public INDArray getLFactor(INDArray A) { // FIXME: int cast if (A.rows() > Integer.MAX_VALUE || A.columns() > Integer.MAX_VALUE) throw new ND4JArraySizeException(); int m = (int) A.rows(); int n = (int) A.columns(); INDArray L = Nd4j.create(m, n); for (int r = 0; r < m; r++) { for (int c = 0; c < n; c++) { if (r > c && r < m && c < n) { L.putScalar(r, c, A.getFloat(r, c)); } else if (r < c) { L.putScalar(r, c, 0.f); } else { L.putScalar(r, c, 1.f); } } } return L; }
/** * 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"); }
private List<RecognisedObject> predict(INDArray predictions) { List<RecognisedObject> objects = new ArrayList<>(); int[] topNPredictions = new int[topN]; float[] topNProb = new float[topN]; String outLabels[]=new String[topN]; //brute force collect top N int i = 0; for (int batch = 0; batch < predictions.size(0); batch++) { INDArray currentBatch = predictions.getRow(batch).dup(); while (i < topN) { topNPredictions[i] = Nd4j.argMax(currentBatch, 1).getInt(0, 0); topNProb[i] = currentBatch.getFloat(batch, topNPredictions[i]); currentBatch.putScalar(0, topNPredictions[i], 0); outLabels[i]= imageNetLabels.getLabel(topNPredictions[i]); objects.add(new RecognisedObject(outLabels[i], "eng", outLabels[i], topNProb[i])); i++; } } return objects; } }
@Override public float op(float origin) { if (numProcessed >= Integer.MAX_VALUE) throw new IllegalArgumentException("Number of processed elements can not be >= Integer.MAX_VALUE"); float ret = (y.getFloat((int) numProcessed)); numProcessed++; return ret; }
private int numIncorrectInMiniBatch(DataSet miniBatch, int maxIndex) { int numIncorrect = 0; indices2[1] = 0; for (int i = 0; i < maxIndex; i++) { indices2[0] = i; numIncorrect += miniBatch.getLabels().getFloat(indices2) == 1 ? 1 : 0; } return numIncorrect; }
public static float calculateWrongness(int exampleIndex, INDArray predictedLabels, INDArray labels) { final int positiveLabelMutated = 0; final int negativeLabel = 1; final boolean predictedPositive = predictedLabels.getDouble(exampleIndex, positiveLabelMutated) > predictedLabels.getDouble(exampleIndex, negativeLabel); if (predictedPositive) { return predictedLabels.getFloat(exampleIndex, positiveLabelMutated); } else { return predictedLabels.getFloat(exampleIndex, negativeLabel); } } }
private float[] getFloatArrayFromSlice(INDArray rowSlice) { float[] result = new float[rowSlice.columns()]; for (int i = 0; i < rowSlice.columns(); i++) { result[i] = rowSlice.getFloat(i); } return result; }
public static int[] toInts(INDArray n) { if (n instanceof IComplexNDArray) throw new IllegalArgumentException("Unable to convert complex array"); n = n.linearView(); int[] ret = new int[n.length()]; for (int i = 0; i < n.length(); i++) ret[i] = (int) n.getFloat(i); return ret; }
protected static int numLabels(INDArray labels) { FloatSet set = new FloatArraySet(); for (int i = 0; i < labels.size(0); i++) { set.add(labels.getFloat(i)); } return set.size(); }
protected static int numLabels(INDArray labels) { FloatSet set = new FloatArraySet(); for (int i = 0; i < labels.size(0); i++) { set.add(labels.getFloat(i)); } return set.size(); }
private FloatArrayList getVectorElementsFromArray(INDArray vectorArray) { FloatArrayList vectorElements = new FloatArrayList(vectorArray.length()); NdIndexIterator ndIndexIterator = new NdIndexIterator('c', vectorArray.shape()); while (ndIndexIterator.hasNext()) { vectorElements.add(vectorArray.getFloat(ndIndexIterator.next())); } return vectorElements; }