use of org.nd4j.linalg.io.ClassPathResource in project deeplearning4j by deeplearning4j.
the class EvalTest method testEvaluationWithMetaData.
@Test
public void testEvaluationWithMetaData() throws Exception {
RecordReader csv = new CSVRecordReader();
csv.initialize(new FileSplit(new ClassPathResource("iris.txt").getTempFileFromArchive()));
int batchSize = 10;
int labelIdx = 4;
int numClasses = 3;
RecordReaderDataSetIterator rrdsi = new RecordReaderDataSetIterator(csv, batchSize, labelIdx, numClasses);
NormalizerStandardize ns = new NormalizerStandardize();
ns.fit(rrdsi);
rrdsi.setPreProcessor(ns);
rrdsi.reset();
Nd4j.getRandom().setSeed(12345);
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().seed(12345).iterations(1).optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).updater(Updater.SGD).learningRate(0.1).list().layer(0, new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT).activation(Activation.SOFTMAX).nIn(4).nOut(3).build()).pretrain(false).backprop(true).build();
MultiLayerNetwork net = new MultiLayerNetwork(conf);
net.init();
for (int i = 0; i < 4; i++) {
net.fit(rrdsi);
rrdsi.reset();
}
Evaluation e = new Evaluation();
//*** New: Enable collection of metadata (stored in the DataSets) ***
rrdsi.setCollectMetaData(true);
while (rrdsi.hasNext()) {
DataSet ds = rrdsi.next();
//*** New - cross dependencies here make types difficult, usid Object internally in DataSet for this***
List<RecordMetaData> meta = ds.getExampleMetaData(RecordMetaData.class);
INDArray out = net.output(ds.getFeatures());
//*** New - evaluate and also store metadata ***
e.eval(ds.getLabels(), out, meta);
}
System.out.println(e.stats());
System.out.println("\n\n*** Prediction Errors: ***");
//*** New - get list of prediction errors from evaluation ***
List<Prediction> errors = e.getPredictionErrors();
List<RecordMetaData> metaForErrors = new ArrayList<>();
for (Prediction p : errors) {
metaForErrors.add((RecordMetaData) p.getRecordMetaData());
}
//*** New - dynamically load a subset of the data, just for prediction errors ***
DataSet ds = rrdsi.loadFromMetaData(metaForErrors);
INDArray output = net.output(ds.getFeatures());
int count = 0;
for (Prediction t : errors) {
System.out.println(t + "\t\tRaw Data: " + //*** New - load subset of data from MetaData object (usually batched for efficiency) ***
csv.loadFromMetaData((RecordMetaData) t.getRecordMetaData()).getRecord() + "\tNormalized: " + ds.getFeatureMatrix().getRow(count) + "\tLabels: " + ds.getLabels().getRow(count) + "\tNetwork predictions: " + output.getRow(count));
count++;
}
int errorCount = errors.size();
double expAcc = 1.0 - errorCount / 150.0;
assertEquals(expAcc, e.accuracy(), 1e-5);
ConfusionMatrix<Integer> confusion = e.getConfusionMatrix();
int[] actualCounts = new int[3];
int[] predictedCounts = new int[3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
//(actual,predicted)
int entry = confusion.getCount(i, j);
List<Prediction> list = e.getPredictions(i, j);
assertEquals(entry, list.size());
actualCounts[i] += entry;
predictedCounts[j] += entry;
}
}
for (int i = 0; i < 3; i++) {
List<Prediction> actualClassI = e.getPredictionsByActualClass(i);
List<Prediction> predictedClassI = e.getPredictionByPredictedClass(i);
assertEquals(actualCounts[i], actualClassI.size());
assertEquals(predictedCounts[i], predictedClassI.size());
}
}
use of org.nd4j.linalg.io.ClassPathResource in project deeplearning4j by deeplearning4j.
the class RecordReaderDataSetiteratorTest method testSequenceRecordReader.
@Test
public void testSequenceRecordReader() throws Exception {
//need to manually extract
for (int i = 0; i < 3; i++) {
new ClassPathResource(String.format("csvsequence_%d.txt", i)).getTempFileFromArchive();
new ClassPathResource(String.format("csvsequencelabels_%d.txt", i)).getTempFileFromArchive();
}
ClassPathResource resource = new ClassPathResource("csvsequence_0.txt");
String featuresPath = resource.getTempFileFromArchive().getAbsolutePath().replaceAll("0", "%d");
resource = new ClassPathResource("csvsequencelabels_0.txt");
String labelsPath = resource.getTempFileFromArchive().getAbsolutePath().replaceAll("0", "%d");
SequenceRecordReader featureReader = new CSVSequenceRecordReader(1, ",");
SequenceRecordReader labelReader = new CSVSequenceRecordReader(1, ",");
featureReader.initialize(new NumberedFileInputSplit(featuresPath, 0, 2));
labelReader.initialize(new NumberedFileInputSplit(labelsPath, 0, 2));
SequenceRecordReaderDataSetIterator iter = new SequenceRecordReaderDataSetIterator(featureReader, labelReader, 1, 4, false);
assertEquals(3, iter.inputColumns());
assertEquals(4, iter.totalOutcomes());
List<DataSet> dsList = new ArrayList<>();
while (iter.hasNext()) {
dsList.add(iter.next());
}
//3 files
assertEquals(3, dsList.size());
for (int i = 0; i < 3; i++) {
DataSet ds = dsList.get(i);
INDArray features = ds.getFeatureMatrix();
INDArray labels = ds.getLabels();
//1 example in mini-batch
assertEquals(1, features.size(0));
assertEquals(1, labels.size(0));
//3 values per line/time step
assertEquals(3, features.size(1));
//1 value per line, but 4 possible values -> one-hot vector
assertEquals(4, labels.size(1));
//sequence length = 4
assertEquals(4, features.size(2));
assertEquals(4, labels.size(2));
}
//Check features vs. expected:
INDArray expF0 = Nd4j.create(1, 3, 4);
expF0.tensorAlongDimension(0, 1).assign(Nd4j.create(new double[] { 0, 1, 2 }));
expF0.tensorAlongDimension(1, 1).assign(Nd4j.create(new double[] { 10, 11, 12 }));
expF0.tensorAlongDimension(2, 1).assign(Nd4j.create(new double[] { 20, 21, 22 }));
expF0.tensorAlongDimension(3, 1).assign(Nd4j.create(new double[] { 30, 31, 32 }));
assertEquals(dsList.get(0).getFeatureMatrix(), expF0);
INDArray expF1 = Nd4j.create(1, 3, 4);
expF1.tensorAlongDimension(0, 1).assign(Nd4j.create(new double[] { 100, 101, 102 }));
expF1.tensorAlongDimension(1, 1).assign(Nd4j.create(new double[] { 110, 111, 112 }));
expF1.tensorAlongDimension(2, 1).assign(Nd4j.create(new double[] { 120, 121, 122 }));
expF1.tensorAlongDimension(3, 1).assign(Nd4j.create(new double[] { 130, 131, 132 }));
assertEquals(dsList.get(1).getFeatureMatrix(), expF1);
INDArray expF2 = Nd4j.create(1, 3, 4);
expF2.tensorAlongDimension(0, 1).assign(Nd4j.create(new double[] { 200, 201, 202 }));
expF2.tensorAlongDimension(1, 1).assign(Nd4j.create(new double[] { 210, 211, 212 }));
expF2.tensorAlongDimension(2, 1).assign(Nd4j.create(new double[] { 220, 221, 222 }));
expF2.tensorAlongDimension(3, 1).assign(Nd4j.create(new double[] { 230, 231, 232 }));
assertEquals(dsList.get(2).getFeatureMatrix(), expF2);
//Check labels vs. expected:
INDArray expL0 = Nd4j.create(1, 4, 4);
expL0.tensorAlongDimension(0, 1).assign(Nd4j.create(new double[] { 1, 0, 0, 0 }));
expL0.tensorAlongDimension(1, 1).assign(Nd4j.create(new double[] { 0, 1, 0, 0 }));
expL0.tensorAlongDimension(2, 1).assign(Nd4j.create(new double[] { 0, 0, 1, 0 }));
expL0.tensorAlongDimension(3, 1).assign(Nd4j.create(new double[] { 0, 0, 0, 1 }));
assertEquals(dsList.get(0).getLabels(), expL0);
INDArray expL1 = Nd4j.create(1, 4, 4);
expL1.tensorAlongDimension(0, 1).assign(Nd4j.create(new double[] { 0, 0, 0, 1 }));
expL1.tensorAlongDimension(1, 1).assign(Nd4j.create(new double[] { 0, 0, 1, 0 }));
expL1.tensorAlongDimension(2, 1).assign(Nd4j.create(new double[] { 0, 1, 0, 0 }));
expL1.tensorAlongDimension(3, 1).assign(Nd4j.create(new double[] { 1, 0, 0, 0 }));
assertEquals(dsList.get(1).getLabels(), expL1);
INDArray expL2 = Nd4j.create(1, 4, 4);
expL2.tensorAlongDimension(0, 1).assign(Nd4j.create(new double[] { 0, 1, 0, 0 }));
expL2.tensorAlongDimension(1, 1).assign(Nd4j.create(new double[] { 1, 0, 0, 0 }));
expL2.tensorAlongDimension(2, 1).assign(Nd4j.create(new double[] { 0, 0, 0, 1 }));
expL2.tensorAlongDimension(3, 1).assign(Nd4j.create(new double[] { 0, 0, 1, 0 }));
assertEquals(dsList.get(2).getLabels(), expL2);
}
use of org.nd4j.linalg.io.ClassPathResource in project deeplearning4j by deeplearning4j.
the class RecordReaderDataSetiteratorTest method testSequenceRecordReaderSingleReader.
@Test
public void testSequenceRecordReaderSingleReader() throws Exception {
//need to manually extract
for (int i = 0; i < 3; i++) {
new ClassPathResource(String.format("csvsequenceSingle_%d.txt", i)).getTempFileFromArchive();
}
ClassPathResource resource = new ClassPathResource("csvsequenceSingle_0.txt");
String path = resource.getTempFileFromArchive().getAbsolutePath().replaceAll("0", "%d");
SequenceRecordReader reader = new CSVSequenceRecordReader(1, ",");
reader.initialize(new NumberedFileInputSplit(path, 0, 2));
SequenceRecordReaderDataSetIterator iteratorClassification = new SequenceRecordReaderDataSetIterator(reader, 1, 3, 0, false);
SequenceRecordReader reader2 = new CSVSequenceRecordReader(1, ",");
reader2.initialize(new NumberedFileInputSplit(path, 0, 2));
SequenceRecordReaderDataSetIterator iteratorRegression = new SequenceRecordReaderDataSetIterator(reader2, 1, 3, 0, true);
INDArray expF0 = Nd4j.create(1, 2, 4);
expF0.tensorAlongDimension(0, 1).assign(Nd4j.create(new double[] { 1, 2 }));
expF0.tensorAlongDimension(1, 1).assign(Nd4j.create(new double[] { 11, 12 }));
expF0.tensorAlongDimension(2, 1).assign(Nd4j.create(new double[] { 21, 22 }));
expF0.tensorAlongDimension(3, 1).assign(Nd4j.create(new double[] { 31, 32 }));
INDArray expF1 = Nd4j.create(1, 2, 4);
expF1.tensorAlongDimension(0, 1).assign(Nd4j.create(new double[] { 101, 102 }));
expF1.tensorAlongDimension(1, 1).assign(Nd4j.create(new double[] { 111, 112 }));
expF1.tensorAlongDimension(2, 1).assign(Nd4j.create(new double[] { 121, 122 }));
expF1.tensorAlongDimension(3, 1).assign(Nd4j.create(new double[] { 131, 132 }));
INDArray expF2 = Nd4j.create(1, 2, 4);
expF2.tensorAlongDimension(0, 1).assign(Nd4j.create(new double[] { 201, 202 }));
expF2.tensorAlongDimension(1, 1).assign(Nd4j.create(new double[] { 211, 212 }));
expF2.tensorAlongDimension(2, 1).assign(Nd4j.create(new double[] { 221, 222 }));
expF2.tensorAlongDimension(3, 1).assign(Nd4j.create(new double[] { 231, 232 }));
INDArray[] expF = new INDArray[] { expF0, expF1, expF2 };
//Expected out for classification:
INDArray expOut0 = Nd4j.create(1, 3, 4);
expOut0.tensorAlongDimension(0, 1).assign(Nd4j.create(new double[] { 1, 0, 0 }));
expOut0.tensorAlongDimension(1, 1).assign(Nd4j.create(new double[] { 0, 1, 0 }));
expOut0.tensorAlongDimension(2, 1).assign(Nd4j.create(new double[] { 0, 0, 1 }));
expOut0.tensorAlongDimension(3, 1).assign(Nd4j.create(new double[] { 1, 0, 0 }));
INDArray expOut1 = Nd4j.create(1, 3, 4);
expOut1.tensorAlongDimension(0, 1).assign(Nd4j.create(new double[] { 0, 1, 0 }));
expOut1.tensorAlongDimension(1, 1).assign(Nd4j.create(new double[] { 0, 0, 1 }));
expOut1.tensorAlongDimension(2, 1).assign(Nd4j.create(new double[] { 1, 0, 0 }));
expOut1.tensorAlongDimension(3, 1).assign(Nd4j.create(new double[] { 0, 0, 1 }));
INDArray expOut2 = Nd4j.create(1, 3, 4);
expOut2.tensorAlongDimension(0, 1).assign(Nd4j.create(new double[] { 0, 1, 0 }));
expOut2.tensorAlongDimension(1, 1).assign(Nd4j.create(new double[] { 1, 0, 0 }));
expOut2.tensorAlongDimension(2, 1).assign(Nd4j.create(new double[] { 0, 1, 0 }));
expOut2.tensorAlongDimension(3, 1).assign(Nd4j.create(new double[] { 0, 0, 1 }));
INDArray[] expOutClassification = new INDArray[] { expOut0, expOut1, expOut2 };
//Expected out for regression:
INDArray expOutR0 = Nd4j.create(1, 1, 4);
expOutR0.tensorAlongDimension(0, 1).assign(Nd4j.create(new double[] { 0 }));
expOutR0.tensorAlongDimension(1, 1).assign(Nd4j.create(new double[] { 1 }));
expOutR0.tensorAlongDimension(2, 1).assign(Nd4j.create(new double[] { 2 }));
expOutR0.tensorAlongDimension(3, 1).assign(Nd4j.create(new double[] { 0 }));
INDArray expOutR1 = Nd4j.create(1, 1, 4);
expOutR1.tensorAlongDimension(0, 1).assign(Nd4j.create(new double[] { 1 }));
expOutR1.tensorAlongDimension(1, 1).assign(Nd4j.create(new double[] { 2 }));
expOutR1.tensorAlongDimension(2, 1).assign(Nd4j.create(new double[] { 0 }));
expOutR1.tensorAlongDimension(3, 1).assign(Nd4j.create(new double[] { 2 }));
INDArray expOutR2 = Nd4j.create(1, 1, 4);
expOutR2.tensorAlongDimension(0, 1).assign(Nd4j.create(new double[] { 1 }));
expOutR2.tensorAlongDimension(1, 1).assign(Nd4j.create(new double[] { 0 }));
expOutR2.tensorAlongDimension(2, 1).assign(Nd4j.create(new double[] { 1 }));
expOutR2.tensorAlongDimension(3, 1).assign(Nd4j.create(new double[] { 2 }));
INDArray[] expOutRegression = new INDArray[] { expOutR0, expOutR1, expOutR2 };
int countC = 0;
while (iteratorClassification.hasNext()) {
DataSet ds = iteratorClassification.next();
INDArray f = ds.getFeatures();
INDArray l = ds.getLabels();
assertNull(ds.getFeaturesMaskArray());
assertNull(ds.getLabelsMaskArray());
assertArrayEquals(new int[] { 1, 2, 4 }, f.shape());
//One-hot representation
assertArrayEquals(new int[] { 1, 3, 4 }, l.shape());
assertEquals(expF[countC], f);
assertEquals(expOutClassification[countC++], l);
}
assertEquals(3, countC);
assertEquals(3, iteratorClassification.totalOutcomes());
int countF = 0;
while (iteratorRegression.hasNext()) {
DataSet ds = iteratorRegression.next();
INDArray f = ds.getFeatures();
INDArray l = ds.getLabels();
assertNull(ds.getFeaturesMaskArray());
assertNull(ds.getLabelsMaskArray());
assertArrayEquals(new int[] { 1, 2, 4 }, f.shape());
//Regression (single output)
assertArrayEquals(new int[] { 1, 1, 4 }, l.shape());
assertEquals(expF[countF], f);
assertEquals(expOutRegression[countF++], l);
}
assertEquals(3, countF);
assertEquals(1, iteratorRegression.totalOutcomes());
}
use of org.nd4j.linalg.io.ClassPathResource in project deeplearning4j by deeplearning4j.
the class RecordReaderDataSetiteratorTest method testSequenceRecordReaderSingleReaderWithEmptySequenceThrows.
@Test(expected = ZeroLengthSequenceException.class)
public void testSequenceRecordReaderSingleReaderWithEmptySequenceThrows() throws Exception {
SequenceRecordReader reader = new CSVSequenceRecordReader(1, ",");
reader.initialize(new FileSplit(new ClassPathResource("empty.txt").getTempFileFromArchive()));
new SequenceRecordReaderDataSetIterator(reader, 1, -1, 1, true).next();
}
use of org.nd4j.linalg.io.ClassPathResource in project deeplearning4j by deeplearning4j.
the class RecordReaderDataSetiteratorTest method testRecordReaderMetaData.
@Test
public void testRecordReaderMetaData() throws Exception {
RecordReader csv = new CSVRecordReader();
csv.initialize(new FileSplit(new ClassPathResource("iris.txt").getTempFileFromArchive()));
int batchSize = 10;
int labelIdx = 4;
int numClasses = 3;
RecordReaderDataSetIterator rrdsi = new RecordReaderDataSetIterator(csv, batchSize, labelIdx, numClasses);
rrdsi.setCollectMetaData(true);
while (rrdsi.hasNext()) {
DataSet ds = rrdsi.next();
List<RecordMetaData> meta = ds.getExampleMetaData(RecordMetaData.class);
int i = 0;
for (RecordMetaData m : meta) {
Record r = csv.loadFromMetaData(m);
INDArray row = ds.getFeatureMatrix().getRow(i);
System.out.println(m.getLocation() + "\t" + r.getRecord() + "\t" + row);
for (int j = 0; j < 4; j++) {
double exp = r.getRecord().get(j).toDouble();
double act = row.getDouble(j);
assertEquals(exp, act, 1e-6);
}
i++;
}
System.out.println();
DataSet fromMeta = rrdsi.loadFromMetaData(meta);
assertEquals(ds, fromMeta);
}
}
Aggregations