use of org.datavec.api.writable.Writable in project deeplearning4j by deeplearning4j.
the class RecordReaderDataSetiteratorTest method testSeqRRDSIArrayWritableTwoReaders.
@Test
public void testSeqRRDSIArrayWritableTwoReaders() {
List<List<Writable>> sequence1 = new ArrayList<>();
sequence1.add(Arrays.asList((Writable) new NDArrayWritable(Nd4j.create(new double[] { 1, 2, 3 })), new IntWritable(100)));
sequence1.add(Arrays.asList((Writable) new NDArrayWritable(Nd4j.create(new double[] { 4, 5, 6 })), new IntWritable(200)));
List<List<Writable>> sequence2 = new ArrayList<>();
sequence2.add(Arrays.asList((Writable) new NDArrayWritable(Nd4j.create(new double[] { 7, 8, 9 })), new IntWritable(300)));
sequence2.add(Arrays.asList((Writable) new NDArrayWritable(Nd4j.create(new double[] { 10, 11, 12 })), new IntWritable(400)));
SequenceRecordReader rrFeatures = new CollectionSequenceRecordReader(Arrays.asList(sequence1, sequence2));
List<List<Writable>> sequence1L = new ArrayList<>();
sequence1L.add(Arrays.asList((Writable) new NDArrayWritable(Nd4j.create(new double[] { 100, 200, 300 })), new IntWritable(101)));
sequence1L.add(Arrays.asList((Writable) new NDArrayWritable(Nd4j.create(new double[] { 400, 500, 600 })), new IntWritable(201)));
List<List<Writable>> sequence2L = new ArrayList<>();
sequence2L.add(Arrays.asList((Writable) new NDArrayWritable(Nd4j.create(new double[] { 700, 800, 900 })), new IntWritable(301)));
sequence2L.add(Arrays.asList((Writable) new NDArrayWritable(Nd4j.create(new double[] { 1000, 1100, 1200 })), new IntWritable(401)));
SequenceRecordReader rrLabels = new CollectionSequenceRecordReader(Arrays.asList(sequence1L, sequence2L));
SequenceRecordReaderDataSetIterator iter = new SequenceRecordReaderDataSetIterator(rrFeatures, rrLabels, 2, -1, true);
//2 examples, 4 values per time step, 2 time steps
INDArray expFeatures = Nd4j.create(2, 4, 2);
expFeatures.tensorAlongDimension(0, 1, 2).assign(Nd4j.create(new double[][] { { 1, 4 }, { 2, 5 }, { 3, 6 }, { 100, 200 } }));
expFeatures.tensorAlongDimension(1, 1, 2).assign(Nd4j.create(new double[][] { { 7, 10 }, { 8, 11 }, { 9, 12 }, { 300, 400 } }));
INDArray expLabels = Nd4j.create(2, 4, 2);
expLabels.tensorAlongDimension(0, 1, 2).assign(Nd4j.create(new double[][] { { 100, 400 }, { 200, 500 }, { 300, 600 }, { 101, 201 } }));
expLabels.tensorAlongDimension(1, 1, 2).assign(Nd4j.create(new double[][] { { 700, 1000 }, { 800, 1100 }, { 900, 1200 }, { 301, 401 } }));
DataSet ds = iter.next();
assertEquals(expFeatures, ds.getFeatureMatrix());
assertEquals(expLabels, ds.getLabels());
}
use of org.datavec.api.writable.Writable in project deeplearning4j by deeplearning4j.
the class RecordReaderDataSetiteratorTest method testSeqRRDSIArrayWritableOneReader.
@Test
public void testSeqRRDSIArrayWritableOneReader() {
List<List<Writable>> sequence1 = new ArrayList<>();
sequence1.add(Arrays.asList((Writable) new NDArrayWritable(Nd4j.create(new double[] { 1, 2, 3 })), new IntWritable(0)));
sequence1.add(Arrays.asList((Writable) new NDArrayWritable(Nd4j.create(new double[] { 4, 5, 6 })), new IntWritable(1)));
List<List<Writable>> sequence2 = new ArrayList<>();
sequence2.add(Arrays.asList((Writable) new NDArrayWritable(Nd4j.create(new double[] { 7, 8, 9 })), new IntWritable(2)));
sequence2.add(Arrays.asList((Writable) new NDArrayWritable(Nd4j.create(new double[] { 10, 11, 12 })), new IntWritable(3)));
SequenceRecordReader rr = new CollectionSequenceRecordReader(Arrays.asList(sequence1, sequence2));
SequenceRecordReaderDataSetIterator iter = new SequenceRecordReaderDataSetIterator(rr, 2, 4, 1, false);
DataSet ds = iter.next();
//2 examples, 3 values per time step, 2 time steps
INDArray expFeatures = Nd4j.create(2, 3, 2);
expFeatures.tensorAlongDimension(0, 1, 2).assign(Nd4j.create(new double[][] { { 1, 4 }, { 2, 5 }, { 3, 6 } }));
expFeatures.tensorAlongDimension(1, 1, 2).assign(Nd4j.create(new double[][] { { 7, 10 }, { 8, 11 }, { 9, 12 } }));
INDArray expLabels = Nd4j.create(2, 4, 2);
expLabels.tensorAlongDimension(0, 1, 2).assign(Nd4j.create(new double[][] { { 1, 0 }, { 0, 1 }, { 0, 0 }, { 0, 0 } }));
expLabels.tensorAlongDimension(1, 1, 2).assign(Nd4j.create(new double[][] { { 0, 0 }, { 0, 0 }, { 1, 0 }, { 0, 1 } }));
assertEquals(expFeatures, ds.getFeatureMatrix());
assertEquals(expLabels, ds.getLabels());
}
use of org.datavec.api.writable.Writable in project deeplearning4j by deeplearning4j.
the class TestRecordReaders method testClassIndexOutsideOfRangeRRMDSI.
@Test
public void testClassIndexOutsideOfRangeRRMDSI() {
Collection<Collection<Collection<Writable>>> c = new ArrayList<>();
Collection<Collection<Writable>> seq1 = new ArrayList<>();
seq1.add(Arrays.<Writable>asList(new DoubleWritable(0.0), new IntWritable(0)));
seq1.add(Arrays.<Writable>asList(new DoubleWritable(0.0), new IntWritable(1)));
c.add(seq1);
Collection<Collection<Writable>> seq2 = new ArrayList<>();
seq2.add(Arrays.<Writable>asList(new DoubleWritable(0.0), new IntWritable(0)));
seq2.add(Arrays.<Writable>asList(new DoubleWritable(0.0), new IntWritable(2)));
c.add(seq2);
CollectionSequenceRecordReader csrr = new CollectionSequenceRecordReader(c);
DataSetIterator dsi = new SequenceRecordReaderDataSetIterator(csrr, 2, 2, 1);
try {
DataSet ds = dsi.next();
fail("Expected exception");
} catch (DL4JException e) {
System.out.println("testClassIndexOutsideOfRangeRRMDSI(): " + e.getMessage());
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
use of org.datavec.api.writable.Writable in project deeplearning4j by deeplearning4j.
the class SequenceVectorsTest method buildGraph.
private static Graph<Blogger, Double> buildGraph() throws IOException, InterruptedException {
File nodes = new File("/ext/Temp/BlogCatalog/nodes.csv");
CSVRecordReader reader = new CSVRecordReader(0, ",");
reader.initialize(new FileSplit(nodes));
List<Blogger> bloggers = new ArrayList<>();
int cnt = 0;
while (reader.hasNext()) {
List<Writable> lines = new ArrayList<>(reader.next());
Blogger blogger = new Blogger(lines.get(0).toInt());
bloggers.add(blogger);
cnt++;
}
reader.close();
Graph<Blogger, Double> graph = new Graph<>(bloggers, true);
// load edges
File edges = new File("/ext/Temp/BlogCatalog/edges.csv");
reader = new CSVRecordReader(0, ",");
reader.initialize(new FileSplit(edges));
while (reader.hasNext()) {
List<Writable> lines = new ArrayList<>(reader.next());
int from = lines.get(0).toInt();
int to = lines.get(1).toInt();
graph.addEdge(from - 1, to - 1, 1.0, false);
}
logger.info("Connected on 0: [" + graph.getConnectedVertices(0).size() + "]");
logger.info("Connected on 1: [" + graph.getConnectedVertices(1).size() + "]");
logger.info("Connected on 3: [" + graph.getConnectedVertices(3).size() + "]");
assertEquals(119, graph.getConnectedVertices(0).size());
assertEquals(9, graph.getConnectedVertices(1).size());
assertEquals(6, graph.getConnectedVertices(3).size());
return graph;
}
use of org.datavec.api.writable.Writable in project deeplearning4j by deeplearning4j.
the class CSVRecordToINDArray method convert.
@Override
public INDArray convert(Collection<Collection<Writable>> records) {
INDArray ret = Nd4j.create(records.size(), records.iterator().next().size());
int count = 0;
for (Collection<Writable> record : records) {
ret.putRow(count++, RecordConverter.toArray(record));
}
return ret;
}
Aggregations