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

Java example source code file (IteratorDataSetIterator.java)

This example Java source code file (IteratorDataSetIterator.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

arraylist, dataset, datasetpreprocessor, iteratordatasetiterator, linkedlist, list, nosuchelementexception, not, override, reset, unsupportedoperationexception, used, util

The IteratorDataSetIterator.java Java example source code

package org.deeplearning4j.datasets.iterator;

import org.nd4j.linalg.dataset.DataSet;
import org.nd4j.linalg.dataset.api.DataSetPreProcessor;
import org.nd4j.linalg.dataset.api.iterator.DataSetIterator;

import java.util.*;

 * A DataSetIterator that works on an Iterator<DataSet>, combining and splitting the input DataSet objects as
 * required to get a consistent batch size.
 * Typically used in Spark training, but may be used elsewhere.
 * NOTE: some methods are not supported here.
public class IteratorDataSetIterator implements DataSetIterator {

    private final Iterator<DataSet> iterator;
    private final int batchSize;
    private final LinkedList<DataSet> queued; //Used when splitting larger examples than we want to return in a batch
    private DataSetPreProcessor preProcessor;

    private int inputColumns = -1;
    private int totalOutcomes = -1;

    private int cursor = 0;

    public IteratorDataSetIterator(Iterator<DataSet> iterator, int batchSize){
        this.iterator = iterator;
        this.batchSize = batchSize;
        this.queued = new LinkedList<>();

    public boolean hasNext() {
        return !queued.isEmpty() || iterator.hasNext();

    public DataSet next() {
        return next(batchSize);

    public DataSet next(int num) {
        if(!hasNext()) throw new NoSuchElementException();

        List<DataSet> list = new ArrayList<>();
        int countSoFar = 0;
        while((!queued.isEmpty() || iterator.hasNext()) && countSoFar < batchSize){
            DataSet next;
                next = queued.removeFirst();
            } else {
                next = iterator.next();
            int nExamples = next.numExamples();
            if( countSoFar + nExamples <= batchSize ){
                //Add the entire DataSet as-is
            } else {
                //Otherwise, split it
                DataSet toKeep = (DataSet)next.getRange(0,batchSize-countSoFar);
                DataSet toCache = (DataSet)next.getRange(batchSize-countSoFar, nExamples);

            countSoFar += nExamples;

        if(inputColumns == -1){
            //Set columns etc for later use
            DataSet temp = list.get(0);
            inputColumns = temp.getFeatureMatrix().size(1);
            totalOutcomes = temp.getLabels().size(1);

        DataSet out;
        if(list.size() == 1){
            out = list.get(0);
        } else {
            out = DataSet.merge(list);

        if(preProcessor != null) preProcessor.preProcess(out);
        cursor += out.numExamples();
        return out;

    public int totalExamples() {
        throw new UnsupportedOperationException("Not supported");

    public int inputColumns() {
        if(inputColumns != -1) return inputColumns;
        return inputColumns;

    public int totalOutcomes() {
        if(totalOutcomes != -1) return totalOutcomes;
        return totalOutcomes;

    public void reset() {
        throw new UnsupportedOperationException("Reset not supported");

    public int batch() {
        return batchSize;

    public int cursor() {
        return cursor;

    public int numExamples() {
        return totalExamples();

    public void setPreProcessor(DataSetPreProcessor preProcessor) {
        this.preProcessor = preProcessor;

    public List<String> getLabels() {
        return null;

    public void remove() {
        throw new UnsupportedOperationException("Not supported");

    private void prefetchBatchSetInputOutputValues(){
        if(!iterator.hasNext()) return;
        DataSet next = iterator.next();
        inputColumns = next.getFeatureMatrix().size(1);
        totalOutcomes = next.getLabels().size(1);

Other Java examples (source code examples)

Here is a short list of links related to this Java IteratorDataSetIterator.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.