Java Code Examples for org.nd4j.linalg.util.FeatureUtil#toOutcomeVector()

The following examples show how to use org.nd4j.linalg.util.FeatureUtil#toOutcomeVector() . 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: CifarLoader.java    From DataVec with Apache License 2.0 6 votes vote down vote up
public Pair<INDArray, opencv_core.Mat> convertMat(byte[] byteFeature) {
    INDArray label = FeatureUtil.toOutcomeVector(byteFeature[0], NUM_LABELS);; // first value in the 3073 byte array
    opencv_core.Mat image = new opencv_core.Mat(HEIGHT, WIDTH, CV_8UC(CHANNELS)); // feature are 3072
    ByteBuffer imageData = image.createBuffer();

    for (int i = 0; i < HEIGHT * WIDTH; i++) {
        imageData.put(3 * i, byteFeature[i + 1 + 2 * HEIGHT * WIDTH]); // blue
        imageData.put(3 * i + 1, byteFeature[i + 1 + HEIGHT * WIDTH]); // green
        imageData.put(3 * i + 2, byteFeature[i + 1]); // red
    }
    //        if (useSpecialPreProcessCifar) {
    //            image = convertCifar(image);
    //        }

    return new Pair<>(label, image);
}
 
Example 2
Source File: CifarLoader.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
public Pair<INDArray, Mat> convertMat(byte[] byteFeature) {
    INDArray label = FeatureUtil.toOutcomeVector(byteFeature[0], NUM_LABELS);; // first value in the 3073 byte array
    Mat image = new Mat(HEIGHT, WIDTH, CV_8UC(CHANNELS)); // feature are 3072
    ByteBuffer imageData = image.createBuffer();

    for (int i = 0; i < HEIGHT * WIDTH; i++) {
        imageData.put(3 * i, byteFeature[i + 1 + 2 * HEIGHT * WIDTH]); // blue
        imageData.put(3 * i + 1, byteFeature[i + 1 + HEIGHT * WIDTH]); // green
        imageData.put(3 * i + 2, byteFeature[i + 1]); // red
    }
    //        if (useSpecialPreProcessCifar) {
    //            image = convertCifar(image);
    //        }

    return new Pair<>(label, image);
}
 
Example 3
Source File: EvalTest.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testStringListLabels() {
    INDArray trueOutcome = FeatureUtil.toOutcomeVector(0, 2);
    INDArray predictedOutcome = FeatureUtil.toOutcomeVector(0, 2);

    List<String> labelsList = new ArrayList<>();
    labelsList.add("hobbs");
    labelsList.add("cal");

    Evaluation eval = new Evaluation(labelsList);

    eval.eval(trueOutcome, predictedOutcome);
    assertEquals(1, eval.classCount(0));
    assertEquals(labelsList.get(0), eval.getClassLabel(0));

}
 
Example 4
Source File: EvalTest.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testStringHashLabels() {
    INDArray trueOutcome = FeatureUtil.toOutcomeVector(0, 2);
    INDArray predictedOutcome = FeatureUtil.toOutcomeVector(0, 2);

    Map<Integer, String> labelsMap = new HashMap<>();
    labelsMap.put(0, "hobbs");
    labelsMap.put(1, "cal");

    Evaluation eval = new Evaluation(labelsMap);

    eval.eval(trueOutcome, predictedOutcome);
    assertEquals(1, eval.classCount(0));
    assertEquals(labelsMap.get(0), eval.getClassLabel(0));

}
 
Example 5
Source File: DataSet.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the outcome of a particular example
 *
 * @param example the example to transform
 * @param label   the label of the outcome
 */
@Override
public void setOutcome(int example, int label) {
    if (example > numExamples())
        throw new IllegalArgumentException("No example at " + example);
    if (label > numOutcomes() || label < 0)
        throw new IllegalArgumentException("Illegal label");

    INDArray outcome = FeatureUtil.toOutcomeVector(label, numOutcomes());
    getLabels().putRow(example, outcome);
}
 
Example 6
Source File: LFWLoader.java    From Canova with Apache License 2.0 5 votes vote down vote up
public DataSet getDataFor(int i) {
    File image = new File(images.get(i));
    int outcome = outcomes.indexOf(image.getParentFile().getAbsolutePath());
    try {
        return new DataSet(loader.asRowVector(image), FeatureUtil.toOutcomeVector(outcome, outcomes.size()));
    } catch (Exception e) {
        throw new IllegalStateException("Unable to getFromOrigin data for image " + i + " for path " + images.get(i));
    }
}
 
Example 7
Source File: EvalTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testEval() {
    int classNum = 5;
    Evaluation eval = new Evaluation (classNum);

    // Testing the edge case when some classes do not have true positive
    INDArray trueOutcome = FeatureUtil.toOutcomeVector(0, 5); //[1,0,0,0,0]
    INDArray predictedOutcome = FeatureUtil.toOutcomeVector(0, 5); //[1,0,0,0,0]
    eval.eval(trueOutcome, predictedOutcome);
    assertEquals(1, eval.classCount(0));
    assertEquals(1.0, eval.f1(), 1e-1);

    // Testing more than one sample. eval() does not reset the Evaluation instance
    INDArray trueOutcome2 = FeatureUtil.toOutcomeVector(1, 5); //[0,1,0,0,0]
    INDArray predictedOutcome2 = FeatureUtil.toOutcomeVector(0, 5); //[1,0,0,0,0]
    eval.eval(trueOutcome2, predictedOutcome2);
    // Verified with sklearn in Python
    // from sklearn.metrics import classification_report
    // classification_report(['a', 'a'], ['a', 'b'], labels=['a', 'b', 'c', 'd', 'e'])
    assertEquals(eval.f1(), 0.6, 1e-1);
    // The first entry is 0 label
    assertEquals(1, eval.classCount(0));
    // The first entry is 1 label
    assertEquals(1, eval.classCount(1));
    // Class 0: one positive, one negative -> (one true positive, one false positive); no true/false negatives
    assertEquals(1, eval.positive().get(0), 0);
    assertEquals(1, eval.negative().get(0), 0);
    assertEquals(1, eval.truePositives().get(0), 0);
    assertEquals(1, eval.falsePositives().get(0), 0);
    assertEquals(0, eval.trueNegatives().get(0), 0);
    assertEquals(0, eval.falseNegatives().get(0), 0);


    // The rest are negative
    assertEquals(1, eval.negative().get(0), 0);
    // 2 rows and only the first is correct
    assertEquals(0.5, eval.accuracy(), 0);
}
 
Example 8
Source File: DataSet.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the outcome of a particular example
 *
 * @param example the example to transform
 * @param label   the label of the outcome
 */
@Override
public void setOutcome(int example, int label) {
    if (example > numExamples())
        throw new IllegalArgumentException("No example at " + example);
    if (label > numOutcomes() || label < 0)
        throw new IllegalArgumentException("Illegal label");

    INDArray outcome = FeatureUtil.toOutcomeVector(label, numOutcomes());
    getLabels().putRow(example, outcome);
}
 
Example 9
Source File: TfidfVectorizer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Vectorizes the passed in text treating it as one document
 *
 * @param text  the text to vectorize
 * @param label the label of the text
 * @return a dataset with a transform of weights(relative to impl; could be word counts or tfidf scores)
 */
@Override
public DataSet vectorize(String text, String label) {
    INDArray input = transform(text);
    INDArray labelMatrix = FeatureUtil.toOutcomeVector(labelsSource.indexOf(label), labelsSource.size());

    return new DataSet(input, labelMatrix);
}
 
Example 10
Source File: BagOfWordsVectorizer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public DataSet vectorize(String text, String label) {
    INDArray input = transform(text);
    INDArray labelMatrix = FeatureUtil.toOutcomeVector(labelsSource.indexOf(label), labelsSource.size());

    return new DataSet(input, labelMatrix);
}
 
Example 11
Source File: MLLibUtil.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param point
 * @param numPossibleLabels
 * @return {@link DataSet}
 */
private static DataSet fromLabeledPoint(LabeledPoint point, long numPossibleLabels) {
    Vector features = point.features();
    double label = point.label();

    // FIXMEL int cast
    double[] fArr = features.toArray();
    return new DataSet(Nd4j.create(fArr, new long[]{1,fArr.length}),
                    FeatureUtil.toOutcomeVector((int) label, (int) numPossibleLabels));
}
 
Example 12
Source File: LFWLoader.java    From Canova with Apache License 2.0 4 votes vote down vote up
public DataSet fromImageFile(int label,File image) throws Exception {
    INDArray outcome = FeatureUtil.toOutcomeVector(label, numNames);
    INDArray image2 = ArrayUtil.toNDArray(loader.flattenedImageFromFile(image));
    return new DataSet(image2,outcome);
}
 
Example 13
Source File: DataVecByteDataSetFunction.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public Tuple2<Double, DataSet> call(Tuple2<Text, BytesWritable> inputTuple) throws Exception {
    int lenFeatureVector = 0;

    if (numPossibleLabels >= 1) {
        lenFeatureVector = byteFileLen - 1;
        if (labelIndex < 0)
            labelIndex = byteFileLen - 1;
    }

    InputStream inputStream = new DataInputStream(new ByteArrayInputStream(inputTuple._2().getBytes()));

    int batchNumCount = 0;
    byte[] byteFeature = new byte[byteFileLen];
    List<DataSet> dataSets = new ArrayList<>();
    INDArray label;
    int featureCount;

    try {
        INDArray featureVector = Nd4j.create(lenFeatureVector);
        while ((inputStream.read(byteFeature)) != -1 && batchNumCount != batchSize) {
            featureCount = 0;
            label = FeatureUtil.toOutcomeVector(byteFeature[labelIndex], numPossibleLabels);
            for (int j = 1; j <= featureVector.length(); j++)
                featureVector.putScalar(featureCount++, byteFeature[j]);
            dataSets.add(new DataSet(featureVector, label));
            batchNumCount++;
            byteFeature = new byte[byteFileLen];
            featureVector = Nd4j.create(lenFeatureVector);
        }
    } catch (IOException e) {
        log.error("",e);
    }

    List<INDArray> inputs = new ArrayList<>();
    List<INDArray> labels = new ArrayList<>();

    for (DataSet data : dataSets) {
        inputs.add(data.getFeatures());
        labels.add(data.getLabels());
    }

    DataSet ds = new DataSet(Nd4j.vstack(inputs.toArray(new INDArray[0])),
                    Nd4j.vstack(labels.toArray(new INDArray[0])));
    if (preProcessor != null)
        preProcessor.preProcess(ds);
    return new Tuple2<>((double) batchNumCount, ds);

}
 
Example 14
Source File: DataVecSequenceDataSetFunction.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public DataSet call(List<List<Writable>> input) throws Exception {
    Iterator<List<Writable>> iter = input.iterator();

    INDArray features = null;
    INDArray labels = Nd4j.zeros(1, (regression ? 1 : numPossibleLabels), input.size());

    int[] fIdx = new int[3];
    int[] lIdx = new int[3];

    int i = 0;
    while (iter.hasNext()) {
        List<Writable> step = iter.next();
        if (i == 0) {
            features = Nd4j.zeros(1, step.size() - 1, input.size());
        }

        Iterator<Writable> timeStepIter = step.iterator();
        int countIn = 0;
        int countFeatures = 0;
        while (timeStepIter.hasNext()) {
            Writable current = timeStepIter.next();
            if (converter != null)
                current = converter.convert(current);
            if (countIn++ == labelIndex) {
                //label
                if (regression) {
                    lIdx[2] = i;
                    labels.putScalar(lIdx, current.toDouble());
                } else {
                    INDArray line = FeatureUtil.toOutcomeVector(current.toInt(), numPossibleLabels);
                    labels.tensorAlongDimension(i, 1).assign(line); //1d from [1,nOut,timeSeriesLength] -> tensor i along dimension 1 is at time i
                }
            } else {
                //feature
                fIdx[1] = countFeatures++;
                fIdx[2] = i;
                try {
                    features.putScalar(fIdx, current.toDouble());
                } catch (UnsupportedOperationException e) {
                    // This isn't a scalar, so check if we got an array already
                    if (current instanceof NDArrayWritable) {
                        features.get(NDArrayIndex.point(fIdx[0]), NDArrayIndex.all(), NDArrayIndex.point(fIdx[2]))
                                        .putRow(0, ((NDArrayWritable) current).get());
                    } else {
                        throw e;
                    }
                }
            }
        }
        i++;
    }

    DataSet ds = new DataSet(features, labels);
    if (preProcessor != null)
        preProcessor.preProcess(ds);
    return ds;
}
 
Example 15
Source File: BaseDataFetcher.java    From nd4j with Apache License 2.0 2 votes vote down vote up
/**
 * Creates an output label matrix
 *
 * @param outcomeLabel the outcome label to use
 * @return a binary vector where 1 is transform to the
 * index specified by outcomeLabel
 */
protected INDArray createOutputVector(int outcomeLabel) {
    return FeatureUtil.toOutcomeVector(outcomeLabel, numOutcomes);
}
 
Example 16
Source File: BaseDataFetcher.java    From deeplearning4j with Apache License 2.0 2 votes vote down vote up
/**
 * Creates an output label matrix
 *
 * @param outcomeLabel the outcome label to use
 * @return a binary vector where 1 is transform to the
 * index specified by outcomeLabel
 */
protected INDArray createOutputVector(int outcomeLabel) {
    return FeatureUtil.toOutcomeVector(outcomeLabel, numOutcomes);
}