use of org.nd4j.linalg.io.ClassPathResource in project deeplearning4j by deeplearning4j.
the class RecordReaderDataSetiteratorTest method testSequenceRecordReaderSingleReaderMetaData.
@Test
public void testSequenceRecordReaderSingleReaderMetaData() 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);
iteratorClassification.setCollectMetaData(true);
iteratorRegression.setCollectMetaData(true);
while (iteratorClassification.hasNext()) {
DataSet ds = iteratorClassification.next();
DataSet fromMeta = iteratorClassification.loadFromMetaData(ds.getExampleMetaData(RecordMetaData.class));
assertEquals(ds, fromMeta);
}
while (iteratorRegression.hasNext()) {
DataSet ds = iteratorRegression.next();
DataSet fromMeta = iteratorRegression.loadFromMetaData(ds.getExampleMetaData(RecordMetaData.class));
assertEquals(ds, fromMeta);
}
}
use of org.nd4j.linalg.io.ClassPathResource in project deeplearning4j by deeplearning4j.
the class RecordReaderDataSetiteratorTest method testSequenceRecordReaderMeta.
@Test
public void testSequenceRecordReaderMeta() 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);
iter.setCollectMetaData(true);
assertEquals(3, iter.inputColumns());
assertEquals(4, iter.totalOutcomes());
while (iter.hasNext()) {
DataSet ds = iter.next();
List<RecordMetaData> meta = ds.getExampleMetaData(RecordMetaData.class);
DataSet fromMeta = iter.loadFromMetaData(meta);
assertEquals(ds, fromMeta);
}
}
use of org.nd4j.linalg.io.ClassPathResource in project deeplearning4j by deeplearning4j.
the class TestComputationGraphNetwork method testIrisFitMultiDataSetIterator.
@Test
public void testIrisFitMultiDataSetIterator() throws Exception {
RecordReader rr = new CSVRecordReader(0, ",");
rr.initialize(new FileSplit(new ClassPathResource("iris.txt").getTempFileFromArchive()));
MultiDataSetIterator iter = new RecordReaderMultiDataSetIterator.Builder(10).addReader("iris", rr).addInput("iris", 0, 3).addOutputOneHot("iris", 4, 3).build();
ComputationGraphConfiguration config = new NeuralNetConfiguration.Builder().optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).learningRate(0.1).graphBuilder().addInputs("in").addLayer("dense", new DenseLayer.Builder().nIn(4).nOut(2).build(), "in").addLayer("out", new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT).nIn(2).nOut(3).build(), "dense").setOutputs("out").pretrain(false).backprop(true).build();
ComputationGraph cg = new ComputationGraph(config);
cg.init();
cg.fit(iter);
rr.reset();
iter = new RecordReaderMultiDataSetIterator.Builder(10).addReader("iris", rr).addInput("iris", 0, 3).addOutputOneHot("iris", 4, 3).build();
while (iter.hasNext()) {
cg.fit(iter.next());
}
}
use of org.nd4j.linalg.io.ClassPathResource in project deeplearning4j by deeplearning4j.
the class TestGraphLoading method testEdgeListGraphLoading.
@Test
public void testEdgeListGraphLoading() throws IOException {
ClassPathResource cpr = new ClassPathResource("testgraph_7vertices.txt");
IGraph<String, String> graph = GraphLoader.loadUndirectedGraphEdgeListFile(cpr.getTempFileFromArchive().getAbsolutePath(), 7, ",");
System.out.println(graph);
assertEquals(graph.numVertices(), 7);
int[][] edges = { { 1, 2 }, { 0, 2, 4 }, { 0, 1, 3, 4 }, { 2, 4, 5 }, { 1, 2, 3, 5, 6 }, { 3, 4, 6 }, { 4, 5 } };
for (int i = 0; i < 7; i++) {
assertEquals(edges[i].length, graph.getVertexDegree(i));
int[] connectedVertices = graph.getConnectedVertexIndices(i);
for (int j = 0; j < edges[i].length; j++) {
assertTrue(ArrayUtils.contains(connectedVertices, edges[i][j]));
}
}
}
use of org.nd4j.linalg.io.ClassPathResource in project deeplearning4j by deeplearning4j.
the class TestGraphLoading method testGraphLoading.
@Test
public void testGraphLoading() throws IOException {
ClassPathResource cpr = new ClassPathResource("simplegraph.txt");
EdgeLineProcessor<String> edgeLineProcessor = new DelimitedEdgeLineProcessor(",", false, "//");
VertexFactory<String> vertexFactory = new StringVertexFactory("v_%d");
Graph<String, String> graph = GraphLoader.loadGraph(cpr.getTempFileFromArchive().getAbsolutePath(), edgeLineProcessor, vertexFactory, 10, false);
System.out.println(graph);
for (int i = 0; i < 10; i++) {
List<Edge<String>> edges = graph.getEdgesOut(i);
assertEquals(2, edges.size());
//expect for example 0->1 and 9->0
Edge<String> first = edges.get(0);
if (first.getFrom() == i) {
//undirected edge: i -> i+1 (or 9 -> 0)
assertEquals(i, first.getFrom());
assertEquals((i + 1) % 10, first.getTo());
} else {
//undirected edge: i-1 -> i (or 9 -> 0)
assertEquals((i + 10 - 1) % 10, first.getFrom());
assertEquals(i, first.getTo());
}
Edge<String> second = edges.get(1);
assertNotEquals(first.getFrom(), second.getFrom());
if (second.getFrom() == i) {
//undirected edge: i -> i+1 (or 9 -> 0)
assertEquals(i, second.getFrom());
assertEquals((i + 1) % 10, second.getTo());
} else {
//undirected edge: i-1 -> i (or 9 -> 0)
assertEquals((i + 10 - 1) % 10, second.getFrom());
assertEquals(i, second.getTo());
}
}
}
Aggregations