home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (AbstractDataSetIteratorTest.java)

This example Java source code file (AbstractDataSetIteratorTest.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

abstractdatasetiteratortest, atomicinteger, exception, floatsdatasetiterator, indarray, iterable, override, pair, util

The AbstractDataSetIteratorTest.java Java example source code

package org.deeplearning4j.datasets.iterator;

import org.apache.commons.lang3.RandomUtils;
import org.deeplearning4j.berkeley.Pair;
import org.junit.Test;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.dataset.DataSet;

import java.util.Iterator;
import java.util.concurrent.atomic.AtomicInteger;

import static org.junit.Assert.*;

/**
 * Created by raver on 16.06.2016.
 */
public class AbstractDataSetIteratorTest {
    @Test
    public void next() throws Exception {
        int numFeatures = 128;
        int batchSize = 10;
        int numRows = 1000;
        AtomicInteger cnt = new AtomicInteger(0);
        FloatsDataSetIterator iterator = new FloatsDataSetIterator(floatIterable(numRows, numFeatures), batchSize);

        assertTrue(iterator.hasNext());

        while (iterator.hasNext()) {
            DataSet dataSet = iterator.next();

            INDArray features = dataSet.getFeatures();

            assertEquals(batchSize, features.rows());
            assertEquals(numFeatures, features.columns());
            cnt.incrementAndGet();
        }

        assertEquals(numRows/batchSize, cnt.get());
    }


    protected static Iterable<Pair floatIterable(final int totalRows, final int numColumns) {
        return new Iterable<Pair() {
            @Override
            public Iterator<Pair iterator() {
                return new Iterator<Pair() {
                    private AtomicInteger cnt = new AtomicInteger(0);

                    @Override
                    public boolean hasNext() {
                        return cnt.incrementAndGet() <= totalRows;
                    }

                    @Override
                    public Pair<float[], float[]> next() {
                        float features[] = new float[numColumns];
                        float labels[] = new float[numColumns];
                        for (int i = 0; i < numColumns; i++) {
                            features[i] = (float) i;
                            labels[i] = RandomUtils.nextFloat(0, 5);
                        }
                        return Pair.makePair(features, labels);
                    }

                    @Override
                    public void remove() {
                        // no-op
                    }
                };
            }
        };
    }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java AbstractDataSetIteratorTest.java source code file:



my book on functional programming

 

new blog posts

 

Copyright 1998-2019 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.