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

Java example source code file (DataSetLossCalculator.java)

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

datasetiterator, datasetlosscalculator, override, scorecalculator, string

The DataSetLossCalculator.java Java example source code

package org.deeplearning4j.earlystopping.scorecalc;

import org.nd4j.linalg.dataset.api.iterator.DataSetIterator;
import org.deeplearning4j.nn.graph.ComputationGraph;
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;
import org.nd4j.linalg.dataset.DataSet;

/** Given a DataSetIterator: calculate the total loss for the model on that data set.
 * Typically used to calculate the loss on a test set.
 * Note: For early stopping on a {@link ComputationGraph} use {@link DataSetLossCalculatorCG}
 */
public class DataSetLossCalculator implements ScoreCalculator<MultiLayerNetwork>{

    private DataSetIterator dataSetIterator;
    private boolean average;

    /**Calculate the score (loss function value) on a given data set (usually a test set)
     *
     * @param dataSetIterator Data set to calculate the score for
     * @param average Whether to return the average (sum of loss / N) or just (sum of loss)
     */
    public DataSetLossCalculator(DataSetIterator dataSetIterator, boolean average) {
        this.dataSetIterator = dataSetIterator;
        this.average = average;
    }

    @Override
    public double calculateScore(MultiLayerNetwork network) {
        dataSetIterator.reset();

        double lossSum = 0.0;
        int exCount = 0;
        while(dataSetIterator.hasNext()){
            DataSet dataSet = dataSetIterator.next();
            if(dataSet==null) break;
            int nEx = dataSet.getFeatureMatrix().size(0);
            lossSum += network.score(dataSet) * nEx;
            exCount += nEx;
        }

        if(average) return lossSum / exCount;
        else return lossSum;
    }

    @Override
    public String toString(){
        return "DataSetLossCalculator(" + dataSetIterator + ",average="+average+")";
    }
}

Other Java examples (source code examples)

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