All Downloads are FREE. Search and download functionalities are using the official Maven repository.

gov.sandia.cognition.learning.algorithm.CompositeBatchLearnerPair Maven / Gradle / Ivy

There is a newer version: 4.0.1
Show newest version
/*
 * File:            CompositeBatchLearnerPair.java
 * Authors:         Justin Basilico
 * Project:         Cognitive Foundry
 * 
 * Copyright 2011. All rights reserved.
 */

package gov.sandia.cognition.learning.algorithm;

import gov.sandia.cognition.evaluator.CompositeEvaluatorPair;
import gov.sandia.cognition.evaluator.Evaluator;
import gov.sandia.cognition.learning.algorithm.baseline.ConstantLearner;
import gov.sandia.cognition.util.AbstractCloneableSerializable;
import gov.sandia.cognition.util.Pair;
import java.util.ArrayList;
import java.util.Collection;

/**
 * Composes together a pair of batch (typically unsupervised) learners. It
 * takes the input data and passes it to the first learner to learn an evaluator.
 * It then takes that first evaluator, evaluates it on it to all of the input
 * data, and passes the resulting values as input to the second learner. It
 * takes the evaluator learned from the second learner and then composes the
 * two evaluators together to create a composite evaluator. Thus, this class
 * can be used to layer learning algorithms.
 *
 * @param   
 *      The type of the input data. It is passed to the first learning
 *      algorithm to learn the evaluator that takes this type and returns the
 *      intermediate type.
 * @param   
 *      The type of the output of the first learned function that is used as
 *      input to the second learner. It is also the input type for the second
 *      learned evaluator that returns the output type.
 * @param   
 *      The type of output of the evaluator learned by the second learner. It
 *      is also the output type of the composite evaluator pair.
 * @author  Justin Basilico
 * @since   3.3.3
 * @see     InputOutputTransformedBatchLearner
 */
public class CompositeBatchLearnerPair
    extends AbstractCloneableSerializable
    implements BatchLearner, CompositeEvaluatorPair>,
        Pair, ? extends Evaluator>, BatchLearner, ? extends Evaluator>>
{

    /** The first learner that is trained on the input data. */
    protected BatchLearner, ? extends Evaluator> firstLearner;

    /** The second learner that is trained on the output of the evaluator
     *  created by the first learner. */
    protected BatchLearner, ? extends Evaluator> secondLearner;

    /**
     * Creates a new, empty {@link CompositeBatchLearnerPair}.
     */
    public CompositeBatchLearnerPair()
    {
        this(null, null);
    }

    /**
     * Creates a new {@link CompositeBatchLearnerPair} with the given two
     * learner.
     *
     * @param   firstLearner
     *      The first learner that is trained on the input data.
     * @param   secondLearner
     *      The second learner that is trained based on the output of the first
     *      learner.
     */
    public CompositeBatchLearnerPair(
        final BatchLearner, ? extends Evaluator> firstLearner,
        final BatchLearner, ? extends Evaluator> secondLearner)
    {
        super();
        
        this.firstLearner = firstLearner;
        this.secondLearner = secondLearner;
    }

    /**
     * Learn by calling the first learner on all the input values. Then the
     * resulting evaluator is applied to all of the input data to create a
     * set of intermediate data. The second learner is then called on all the
     * intermediate data to create the second evaluator that maps to the
     * output type.
     *
     * @param   data
     *      The training data.
     * @return
     *      The composite evaluator pair that applies the two learned
     *      evaluators in sequence.
     */
    @Override
    public CompositeEvaluatorPair learn(
        final Collection data)
    {
        // Take the input data and train the first learner on it.
        final Evaluator
            firstResult = this.getFirstLearner().learn(data);

        // Now transform the input data using the first evaluator.
        final ArrayList transformedData =
            new ArrayList(data.size());
        for (InputType input : data)
        {
            transformedData.add(firstResult.evaluate(input));
        }

        // Now take the transformed data and train the second learner on it.
        final Evaluator
            secondResult = this.getSecondLearner().learn(transformedData);

        // Return the composed evaluator.
        return CompositeEvaluatorPair.create(firstResult, secondResult);

    }

    @Override
    public BatchLearner, ? extends Evaluator> getFirst()
    {
        return this.getFirstLearner();
    }

    @Override
    public BatchLearner, ? extends Evaluator> getSecond()
    {
        return this.getSecondLearner();
    }

    /**
     * Gets the first learner that is applied to the input data.
     *
     * @return
     *      The first learner.
     */
    public BatchLearner, ? extends Evaluator> getFirstLearner()
    {
        return this.firstLearner;
    }

    /**
     * Sets the first learner that is applied to the input data.
     *
     * @param   firstLearner
     *      The first learner.
     */
    public void setFirstLearner(
        final BatchLearner, ? extends Evaluator> firstLearner)
    {
        this.firstLearner = firstLearner;
    }

    /**
     * Gets the second learner that is applied to the output of the first
     * learner.
     *
     * @return
     *      The second learner.
     */
    public BatchLearner, ? extends Evaluator> getSecondLearner()
    {
        return this.secondLearner;
    }

    /**
     * Sets the second learner that is applied to the output of the first
     * learner.
     *
     * @param   secondLearner
     *      The second learner.
     */
    public void setSecondLearner(
        final BatchLearner, ? extends Evaluator> secondLearner)
    {
        this.secondLearner = secondLearner;
    }

    /**
     * Creates a new {@link CompositeBatchLearnerPair} from the given learners.
     *
     * @param   
     *      The type of the input data. It is passed to the first learning
     *      algorithm to learn the evaluator that takes this type and returns
     *      the intermediate type.
     * @param   
     *      The type of the output of the first learned function that is used as
     *      input to the second learner. It is also the input type for the second
     *      learned evaluator that returns the output type.
     * @param   
     *      The type of output of the evaluator learned by the second learner.
     *      It is also the output type of the composite evaluator pair.
     * @param   firstLearner
     *      The first learner that is trained on the input data.
     * @param   secondLearner
     *      The second learner that is trained based on the output of the first
     *      learner.
     * @return
     *      A new composite evaluator pair of the given learners.
     */
    public static  CompositeBatchLearnerPair create(
        final BatchLearner, ? extends Evaluator> firstLearner,
        final BatchLearner, ? extends Evaluator> secondLearner)
    {
        return new CompositeBatchLearnerPair(
            firstLearner, secondLearner);
    }

    /**
     * Creates a new {@link CompositeBatchLearnerPair} from the given input
     * transform and learner.
     *
     * @param   
     *      The type of the input data. It is passed to the input transform
     *      takes this type and returns the intermediate type.
     * @param   
     *      The type of the output of the input transform is used as input to
     *      the learner. It is also the input type for the learned evaluator
     *      that returns the output type.
     * @param   
     *      The type of output of the learned evaluator.  It is also the output
     *      type of the composite evaluator pair.
     * @param   inputTransform
     *      The input transform to apply to the input data.
     * @param   learner
     *      The learner that is trained based on the output of the transform.
     * @return
     *      A new composite evaluator pair of the given transform and learner.
     */
    public static  CompositeBatchLearnerPair createInputTransformed(
        final Evaluator inputTransform,
        final BatchLearner, ? extends Evaluator> learner)
    {
        return new CompositeBatchLearnerPair(
            ConstantLearner.create(inputTransform), learner);
    }

    /**
     * Creates a new {@link CompositeBatchLearnerPair} from the given learner
     * and output transform..
     *
     * @param   
     *      The type of the input data. It is passed to the learning algorithm
     *      to learn the evaluator that takes this type and returns the
     *      intermediate type.
     * @param   
     *      The type of the output of the first learned function that is used as
     *      input to the output transform.
     * @param   
     *      The type of output of the output transform. It is also the output
     *      type of the composite evaluator pair.
     * @param   learner
     *      The learner that is trained on the input data.
     * @param   outputTransform
     *      The output transform that is composed with the learned function.
     * @return
     *      A new composite evaluator pair of the given learner and transform.
     */
    public static  CompositeBatchLearnerPair createOutputTransformed(
        final BatchLearner, ? extends Evaluator> learner,
        final Evaluator outputTransform)
    {
        return new CompositeBatchLearnerPair(
            learner, ConstantLearner.create(outputTransform));
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy