Search in sources :

Example 1 with StringSplit

use of org.datavec.api.split.StringSplit in project deeplearning4j by deeplearning4j.

the class RecordReaderFunction method call.

@Override
public DataSet call(String v1) throws Exception {
    recordReader.initialize(new StringSplit(v1));
    List<DataSet> dataSets = new ArrayList<>();
    List<Writable> record = recordReader.next();
    List<Writable> currList;
    if (record instanceof List)
        currList = (List<Writable>) record;
    else
        currList = new ArrayList<>(record);
    INDArray label = null;
    INDArray featureVector = Nd4j.create(labelIndex >= 0 ? currList.size() - 1 : currList.size());
    int count = 0;
    for (int j = 0; j < currList.size(); j++) {
        if (labelIndex >= 0 && j == labelIndex) {
            if (numPossibleLabels < 1)
                throw new IllegalStateException("Number of possible labels invalid, must be >= 1");
            Writable current = currList.get(j);
            if (converter != null)
                current = converter.convert(current);
            label = FeatureUtil.toOutcomeVector(Double.valueOf(current.toString()).intValue(), numPossibleLabels);
        } else {
            Writable current = currList.get(j);
            featureVector.putScalar(count++, Double.valueOf(current.toString()));
        }
    }
    dataSets.add(new DataSet(featureVector, labelIndex >= 0 ? label : featureVector));
    List<INDArray> inputs = new ArrayList<>();
    List<INDArray> labels = new ArrayList<>();
    for (DataSet data : dataSets) {
        inputs.add(data.getFeatureMatrix());
        labels.add(data.getLabels());
    }
    DataSet ret = new DataSet(Nd4j.vstack(inputs.toArray(new INDArray[0])), Nd4j.vstack(labels.toArray(new INDArray[0])));
    return ret;
}
Also used : StringSplit(org.datavec.api.split.StringSplit) INDArray(org.nd4j.linalg.api.ndarray.INDArray) DataSet(org.nd4j.linalg.dataset.DataSet) ArrayList(java.util.ArrayList) Writable(org.datavec.api.writable.Writable) List(java.util.List) ArrayList(java.util.ArrayList)

Example 2 with StringSplit

use of org.datavec.api.split.StringSplit in project deeplearning4j by deeplearning4j.

the class StringToDataSetExportFunction method call.

@Override
public void call(Iterator<String> stringIterator) throws Exception {
    String jvmuid = UIDProvider.getJVMUID();
    uid = Thread.currentThread().getId() + jvmuid.substring(0, Math.min(8, jvmuid.length()));
    List<List<Writable>> list = new ArrayList<>(batchSize);
    while (stringIterator.hasNext()) {
        String next = stringIterator.next();
        recordReader.initialize(new StringSplit(next));
        list.add(recordReader.next());
        processBatchIfRequired(list, !stringIterator.hasNext());
    }
}
Also used : StringSplit(org.datavec.api.split.StringSplit) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

ArrayList (java.util.ArrayList)2 List (java.util.List)2 StringSplit (org.datavec.api.split.StringSplit)2 Writable (org.datavec.api.writable.Writable)1 INDArray (org.nd4j.linalg.api.ndarray.INDArray)1 DataSet (org.nd4j.linalg.dataset.DataSet)1