Search in sources :

Example 1 with RecordMetaData

use of org.datavec.api.records.metadata.RecordMetaData in project deeplearning4j by deeplearning4j.

the class RecordReaderDataSetIterator method next.

@Override
public DataSet next(int num) {
    if (useCurrent) {
        useCurrent = false;
        if (preProcessor != null)
            preProcessor.preProcess(last);
        return last;
    }
    List<DataSet> dataSets = new ArrayList<>();
    List<RecordMetaData> meta = (collectMetaData ? new ArrayList<RecordMetaData>() : null);
    for (int i = 0; i < num; i++) {
        if (!hasNext())
            break;
        if (recordReader instanceof SequenceRecordReader) {
            if (sequenceIter == null || !sequenceIter.hasNext()) {
                List<List<Writable>> sequenceRecord = ((SequenceRecordReader) recordReader).sequenceRecord();
                sequenceIter = sequenceRecord.iterator();
            }
            List<Writable> record = sequenceIter.next();
            dataSets.add(getDataSet(record));
        } else {
            if (collectMetaData) {
                Record record = recordReader.nextRecord();
                dataSets.add(getDataSet(record.getRecord()));
                meta.add(record.getMetaData());
            } else {
                List<Writable> record = recordReader.next();
                dataSets.add(getDataSet(record));
            }
        }
    }
    batchNum++;
    if (dataSets.isEmpty())
        return new DataSet();
    DataSet ret = DataSet.merge(dataSets);
    if (collectMetaData) {
        ret.setExampleMetaData(meta);
    }
    last = ret;
    if (preProcessor != null)
        preProcessor.preProcess(ret);
    //Add label name values to dataset
    if (recordReader.getLabels() != null)
        ret.setLabelNames(recordReader.getLabels());
    return ret;
}
Also used : RecordMetaData(org.datavec.api.records.metadata.RecordMetaData) SequenceRecordReader(org.datavec.api.records.reader.SequenceRecordReader) DataSet(org.nd4j.linalg.dataset.DataSet) ArrayList(java.util.ArrayList) NDArrayWritable(org.datavec.common.data.NDArrayWritable) Writable(org.datavec.api.writable.Writable) ArrayList(java.util.ArrayList) List(java.util.List) Record(org.datavec.api.records.Record)

Example 2 with RecordMetaData

use of org.datavec.api.records.metadata.RecordMetaData in project deeplearning4j by deeplearning4j.

the class RecordReaderMultiDataSetIterator method loadFromMetaData.

/**
     * Load a multiple sequence examples to a DataSet, using the provided RecordMetaData instances.
     *
     * @param list List of RecordMetaData instances to load from. Should have been produced by the record reader provided
     *             to the SequenceRecordReaderDataSetIterator constructor
     * @return DataSet with the specified examples
     * @throws IOException If an error occurs during loading of the data
     */
public MultiDataSet loadFromMetaData(List<RecordMetaData> list) throws IOException {
    //First: load the next values from the RR / SeqRRs
    Map<String, List<List<Writable>>> nextRRVals = new HashMap<>();
    Map<String, List<List<List<Writable>>>> nextSeqRRVals = new HashMap<>();
    List<RecordMetaDataComposableMap> nextMetas = (collectMetaData ? new ArrayList<RecordMetaDataComposableMap>() : null);
    for (Map.Entry<String, RecordReader> entry : recordReaders.entrySet()) {
        RecordReader rr = entry.getValue();
        List<RecordMetaData> thisRRMeta = new ArrayList<>();
        for (RecordMetaData m : list) {
            RecordMetaDataComposableMap m2 = (RecordMetaDataComposableMap) m;
            thisRRMeta.add(m2.getMeta().get(entry.getKey()));
        }
        List<Record> fromMeta = rr.loadFromMetaData(thisRRMeta);
        List<List<Writable>> writables = new ArrayList<>(list.size());
        for (Record r : fromMeta) {
            writables.add(r.getRecord());
        }
        nextRRVals.put(entry.getKey(), writables);
    }
    for (Map.Entry<String, SequenceRecordReader> entry : sequenceRecordReaders.entrySet()) {
        SequenceRecordReader rr = entry.getValue();
        List<RecordMetaData> thisRRMeta = new ArrayList<>();
        for (RecordMetaData m : list) {
            RecordMetaDataComposableMap m2 = (RecordMetaDataComposableMap) m;
            thisRRMeta.add(m2.getMeta().get(entry.getKey()));
        }
        List<SequenceRecord> fromMeta = rr.loadSequenceFromMetaData(thisRRMeta);
        List<List<List<Writable>>> writables = new ArrayList<>(list.size());
        for (SequenceRecord r : fromMeta) {
            writables.add(r.getSequenceRecord());
        }
        nextSeqRRVals.put(entry.getKey(), writables);
    }
    return nextMultiDataSet(nextRRVals, nextSeqRRVals, nextMetas);
}
Also used : RecordMetaData(org.datavec.api.records.metadata.RecordMetaData) SequenceRecordReader(org.datavec.api.records.reader.SequenceRecordReader) RecordReader(org.datavec.api.records.reader.RecordReader) SequenceRecordReader(org.datavec.api.records.reader.SequenceRecordReader) NDArrayWritable(org.datavec.common.data.NDArrayWritable) Writable(org.datavec.api.writable.Writable) SequenceRecord(org.datavec.api.records.SequenceRecord) SequenceRecord(org.datavec.api.records.SequenceRecord) Record(org.datavec.api.records.Record) RecordMetaDataComposableMap(org.datavec.api.records.metadata.RecordMetaDataComposableMap) RecordMetaDataComposableMap(org.datavec.api.records.metadata.RecordMetaDataComposableMap)

Example 3 with RecordMetaData

use of org.datavec.api.records.metadata.RecordMetaData in project deeplearning4j by deeplearning4j.

the class SequenceRecordReaderDataSetIterator method nextMultipleSequenceReaders.

private DataSet nextMultipleSequenceReaders(int num) {
    List<INDArray> featureList = new ArrayList<>(num);
    List<INDArray> labelList = new ArrayList<>(num);
    List<RecordMetaData> meta = (collectMetaData ? new ArrayList<RecordMetaData>() : null);
    for (int i = 0; i < num && hasNext(); i++) {
        List<List<Writable>> featureSequence;
        List<List<Writable>> labelSequence;
        if (collectMetaData) {
            SequenceRecord f = recordReader.nextSequence();
            SequenceRecord l = labelsReader.nextSequence();
            featureSequence = f.getSequenceRecord();
            labelSequence = l.getSequenceRecord();
            meta.add(new RecordMetaDataComposable(f.getMetaData(), l.getMetaData()));
        } else {
            featureSequence = recordReader.sequenceRecord();
            labelSequence = labelsReader.sequenceRecord();
        }
        assertNonZeroLengthSequence(featureSequence, "features");
        assertNonZeroLengthSequence(labelSequence, "labels");
        INDArray features = getFeatures(featureSequence);
        //2d time series, with shape [timeSeriesLength,vectorSize]
        INDArray labels = getLabels(labelSequence);
        featureList.add(features);
        labelList.add(labels);
    }
    return nextMultipleSequenceReaders(featureList, labelList, meta);
}
Also used : RecordMetaData(org.datavec.api.records.metadata.RecordMetaData) RecordMetaDataComposable(org.datavec.api.records.metadata.RecordMetaDataComposable) SequenceRecord(org.datavec.api.records.SequenceRecord) INDArray(org.nd4j.linalg.api.ndarray.INDArray)

Example 4 with RecordMetaData

use of org.datavec.api.records.metadata.RecordMetaData in project deeplearning4j by deeplearning4j.

the class SequenceRecordReaderDataSetIterator method nextSingleSequenceReader.

private DataSet nextSingleSequenceReader(int num) {
    List<INDArray> listFeatures = new ArrayList<>(num);
    List<INDArray> listLabels = new ArrayList<>(num);
    List<RecordMetaData> meta = (collectMetaData ? new ArrayList<RecordMetaData>() : null);
    int minLength = 0;
    int maxLength = 0;
    for (int i = 0; i < num && hasNext(); i++) {
        List<List<Writable>> sequence;
        if (collectMetaData) {
            SequenceRecord sequenceRecord = recordReader.nextSequence();
            sequence = sequenceRecord.getSequenceRecord();
            meta.add(sequenceRecord.getMetaData());
        } else {
            sequence = recordReader.sequenceRecord();
        }
        assertNonZeroLengthSequence(sequence, "combined features and labels");
        INDArray[] fl = getFeaturesLabelsSingleReader(sequence);
        if (i == 0) {
            minLength = fl[0].size(0);
            maxLength = minLength;
        } else {
            minLength = Math.min(minLength, fl[0].size(0));
            maxLength = Math.max(maxLength, fl[0].size(0));
        }
        listFeatures.add(fl[0]);
        listLabels.add(fl[1]);
    }
    return getSingleSequenceReader(listFeatures, listLabels, minLength, maxLength, meta);
}
Also used : RecordMetaData(org.datavec.api.records.metadata.RecordMetaData) SequenceRecord(org.datavec.api.records.SequenceRecord) INDArray(org.nd4j.linalg.api.ndarray.INDArray)

Example 5 with RecordMetaData

use of org.datavec.api.records.metadata.RecordMetaData in project deeplearning4j by deeplearning4j.

the class RecordReaderDataSetIterator method loadFromMetaData.

/**
     * Load a multiple examples to a DataSet, using the provided RecordMetaData instances.
     *
     * @param list List of RecordMetaData instances to load from. Should have been produced by the record reader provided
     *             to the RecordReaderDataSetIterator constructor
     * @return DataSet with the specified examples
     * @throws IOException If an error occurs during loading of the data
     */
public DataSet loadFromMetaData(List<RecordMetaData> list) throws IOException {
    List<Record> records = recordReader.loadFromMetaData(list);
    List<DataSet> dataSets = new ArrayList<>();
    List<RecordMetaData> meta = new ArrayList<>();
    for (Record r : records) {
        dataSets.add(getDataSet(r.getRecord()));
        meta.add(r.getMetaData());
    }
    if (dataSets.isEmpty()) {
        return new DataSet();
    }
    DataSet ret = DataSet.merge(dataSets);
    ret.setExampleMetaData(meta);
    last = ret;
    if (preProcessor != null)
        preProcessor.preProcess(ret);
    if (recordReader.getLabels() != null)
        ret.setLabelNames(recordReader.getLabels());
    return ret;
}
Also used : RecordMetaData(org.datavec.api.records.metadata.RecordMetaData) DataSet(org.nd4j.linalg.dataset.DataSet) ArrayList(java.util.ArrayList) Record(org.datavec.api.records.Record)

Aggregations

RecordMetaData (org.datavec.api.records.metadata.RecordMetaData)9 INDArray (org.nd4j.linalg.api.ndarray.INDArray)5 DataSet (org.nd4j.linalg.dataset.DataSet)5 Record (org.datavec.api.records.Record)4 SequenceRecord (org.datavec.api.records.SequenceRecord)4 SequenceRecordReader (org.datavec.api.records.reader.SequenceRecordReader)4 RecordReader (org.datavec.api.records.reader.RecordReader)3 Test (org.junit.Test)3 ClassPathResource (org.nd4j.linalg.io.ClassPathResource)3 ArrayList (java.util.ArrayList)2 RecordMetaDataComposable (org.datavec.api.records.metadata.RecordMetaDataComposable)2 CollectionSequenceRecordReader (org.datavec.api.records.reader.impl.collection.CollectionSequenceRecordReader)2 CSVRecordReader (org.datavec.api.records.reader.impl.csv.CSVRecordReader)2 CSVSequenceRecordReader (org.datavec.api.records.reader.impl.csv.CSVSequenceRecordReader)2 FileSplit (org.datavec.api.split.FileSplit)2 Writable (org.datavec.api.writable.Writable)2 NDArrayWritable (org.datavec.common.data.NDArrayWritable)2 List (java.util.List)1 RecordMetaDataComposableMap (org.datavec.api.records.metadata.RecordMetaDataComposableMap)1 CollectionRecordReader (org.datavec.api.records.reader.impl.collection.CollectionRecordReader)1