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;
}
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);
}
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);
}
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);
}
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;
}
Aggregations