org.nd4j.linalg.dataset.api.iterator.CachingDataSetIterator Java Examples

The following examples show how to use org.nd4j.linalg.dataset.api.iterator.CachingDataSetIterator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CachingDataSetIteratorTest.java    From nd4j with Apache License 2.0 6 votes vote down vote up
private void runDataSetTest(DataSetCache cache) {
    int rows = 500;
    int inputColumns = 100;
    int outputColumns = 2;
    DataSet dataSet = new DataSet(Nd4j.ones(rows, inputColumns), Nd4j.zeros(rows, outputColumns));

    int batchSize = 10;
    int totalNumberOfSamples = 50;
    int expectedNumberOfDataSets = totalNumberOfSamples / batchSize;
    DataSetIterator it = new SamplingDataSetIterator(dataSet, batchSize, totalNumberOfSamples);

    String namespace = "test-namespace";

    CachingDataSetIterator cachedIt = new CachingDataSetIterator(it, cache, namespace);
    PreProcessor preProcessor = new PreProcessor();
    cachedIt.setPreProcessor(preProcessor);

    assertDataSetCacheGetsCompleted(cache, namespace, cachedIt);

    assertPreProcessingGetsCached(expectedNumberOfDataSets, it, cachedIt, preProcessor);

    assertCachingDataSetIteratorHasAllTheData(rows, inputColumns, outputColumns, dataSet, it, cachedIt);
}
 
Example #2
Source File: CachingDataSetIteratorTest.java    From nd4j with Apache License 2.0 6 votes vote down vote up
private void assertPreProcessingGetsCached(int expectedNumberOfDataSets, DataSetIterator it,
                CachingDataSetIterator cachedIt, PreProcessor preProcessor) {

    assertSame(preProcessor, cachedIt.getPreProcessor());
    assertSame(preProcessor, it.getPreProcessor());

    cachedIt.reset();
    it.reset();

    while (cachedIt.hasNext()) {
        cachedIt.next();
    }

    assertEquals(expectedNumberOfDataSets, preProcessor.getCallCount());

    cachedIt.reset();
    it.reset();

    while (cachedIt.hasNext()) {
        cachedIt.next();
    }

    assertEquals(expectedNumberOfDataSets, preProcessor.getCallCount());
}
 
Example #3
Source File: CachingDataSetIteratorTest.java    From nd4j with Apache License 2.0 6 votes vote down vote up
private void assertCachingDataSetIteratorHasAllTheData(int rows, int inputColumns, int outputColumns,
                DataSet dataSet, DataSetIterator it, CachingDataSetIterator cachedIt) {
    cachedIt.reset();
    it.reset();

    dataSet.setFeatures(Nd4j.zeros(rows, inputColumns));
    dataSet.setLabels(Nd4j.ones(rows, outputColumns));

    while (it.hasNext()) {
        assertTrue(cachedIt.hasNext());

        DataSet cachedDs = cachedIt.next();
        assertEquals(1000.0, cachedDs.getFeatureMatrix().sumNumber());
        assertEquals(0.0, cachedDs.getLabels().sumNumber());

        DataSet ds = it.next();
        assertEquals(0.0, ds.getFeatureMatrix().sumNumber());
        assertEquals(20.0, ds.getLabels().sumNumber());
    }

    assertFalse(cachedIt.hasNext());
    assertFalse(it.hasNext());
}
 
Example #4
Source File: CachingDataSetIteratorTest.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
private void runDataSetTest(DataSetCache cache) {
    int rows = 500;
    int inputColumns = 100;
    int outputColumns = 2;
    DataSet dataSet = new DataSet(Nd4j.ones(rows, inputColumns), Nd4j.zeros(rows, outputColumns));

    int batchSize = 10;
    int totalNumberOfSamples = 50;
    int expectedNumberOfDataSets = totalNumberOfSamples / batchSize;
    DataSetIterator it = new SamplingDataSetIterator(dataSet, batchSize, totalNumberOfSamples);

    String namespace = "test-namespace";

    CachingDataSetIterator cachedIt = new CachingDataSetIterator(it, cache, namespace);
    PreProcessor preProcessor = new PreProcessor();
    cachedIt.setPreProcessor(preProcessor);

    assertDataSetCacheGetsCompleted(cache, namespace, cachedIt);

    assertPreProcessingGetsCached(expectedNumberOfDataSets, it, cachedIt, preProcessor);

    assertCachingDataSetIteratorHasAllTheData(rows, inputColumns, outputColumns, dataSet, it, cachedIt);
}
 
Example #5
Source File: CachingDataSetIteratorTest.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
private void assertPreProcessingGetsCached(int expectedNumberOfDataSets, DataSetIterator it,
                CachingDataSetIterator cachedIt, PreProcessor preProcessor) {

    assertSame(preProcessor, cachedIt.getPreProcessor());
    assertSame(preProcessor, it.getPreProcessor());

    cachedIt.reset();
    it.reset();

    while (cachedIt.hasNext()) {
        cachedIt.next();
    }

    assertEquals(expectedNumberOfDataSets, preProcessor.getCallCount());

    cachedIt.reset();
    it.reset();

    while (cachedIt.hasNext()) {
        cachedIt.next();
    }

    assertEquals(expectedNumberOfDataSets, preProcessor.getCallCount());
}
 
Example #6
Source File: CachingDataSetIteratorTest.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
private void assertCachingDataSetIteratorHasAllTheData(int rows, int inputColumns, int outputColumns,
                DataSet dataSet, DataSetIterator it, CachingDataSetIterator cachedIt) {
    cachedIt.reset();
    it.reset();

    dataSet.setFeatures(Nd4j.zeros(rows, inputColumns));
    dataSet.setLabels(Nd4j.ones(rows, outputColumns));

    while (it.hasNext()) {
        assertTrue(cachedIt.hasNext());

        DataSet cachedDs = cachedIt.next();
        assertEquals(1000.0, cachedDs.getFeatures().sumNumber());
        assertEquals(0.0, cachedDs.getLabels().sumNumber());

        DataSet ds = it.next();
        assertEquals(0.0, ds.getFeatures().sumNumber());
        assertEquals(20.0, ds.getLabels().sumNumber());
    }

    assertFalse(cachedIt.hasNext());
    assertFalse(it.hasNext());
}