alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (ExistingDataSetIterator.java)

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

datasetpreprocessor, existingdatasetiterator, illegalstateexception, iterable, iterator, list, nonnull, override, unsupportedoperationexception, util

The ExistingDataSetIterator.java Java example source code

package org.deeplearning4j.datasets.iterator;


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

import java.util.Iterator;
import java.util.List;

/**
 * This wrapper provides DataSetIterator interface to existing java Iterable<DataSet> and Iterator
 *
 * @author raver119@gmail.com
 */
public class ExistingDataSetIterator implements DataSetIterator {
    private transient DataSetPreProcessor preProcessor;

    private transient Iterable<DataSet> iterable;
    private transient Iterator<DataSet> iterator;
    private int totalExamples = 0;
    private int numFeatures = 0;
    private int numLabels = 0;
    private List<String> labels;


    public ExistingDataSetIterator(@NonNull Iterator<DataSet> iterator) {
        this.iterator = iterator;
    }

    public ExistingDataSetIterator(@NonNull Iterator<DataSet> iterator, @NonNull List labels) {
        this(iterator);
        this.labels = labels;
    }

    public ExistingDataSetIterator(@NonNull Iterable<DataSet> iterable) {
        this.iterable = iterable;
        this.iterator = iterable.iterator();
    }

    public ExistingDataSetIterator(@NonNull Iterable<DataSet> iterable, @NonNull List labels) {
        this(iterable);
        this.labels = labels;
    }


    public ExistingDataSetIterator(@NonNull Iterable<DataSet> iterable, int totalExamples, int numFeatures, int numLabels) {
        this(iterable);

        this.totalExamples = totalExamples;
        this.numFeatures = numFeatures;
        this.numLabels = numLabels;
    }

    @Override
    public DataSet next(int num) {
        // TODO: this might be changed
        throw new UnsupportedOperationException("next(int) isn't supported");
    }

    @Override
    public int totalExamples() {
        return totalExamples;
    }

    @Override
    public int inputColumns() {
        return numFeatures;
    }

    @Override
    public int totalOutcomes() {
        if (labels != null)
            return labels.size();

        return numLabels;
    }

    @Override
    public void reset() {
        if (iterable != null)
            this.iterator = iterable.iterator();
        else throw new IllegalStateException("To use reset() method you need to provide Iterable<DataSet>, not Iterator");
    }

    @Override
    public int batch() {
        return 0;
    }

    @Override
    public int cursor() {
        return 0;
    }

    @Override
    public int numExamples() {
        return totalExamples;
    }

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

    @Override
    public List<String> getLabels() {
        return labels;
    }

    @Override
    public boolean hasNext() {
        if (iterator != null)
            return iterator.hasNext();

        return false;
    }

    @Override
    public DataSet next() {
        if (preProcessor != null) {
            DataSet ds = iterator.next();
            preProcessor.preProcess(ds);
            return ds;
        } else return iterator.next();
    }

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

Other Java examples (source code examples)

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

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 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.