001/*
002 * Copyright (c) 2015-2020, Oracle and/or its affiliates. All rights reserved.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.tribuo.classification.sgd.linear;
018
019import com.oracle.labs.mlrg.olcut.config.Config;
020import org.tribuo.ImmutableFeatureMap;
021import org.tribuo.ImmutableOutputInfo;
022import org.tribuo.classification.Label;
023import org.tribuo.classification.sgd.LabelObjective;
024import org.tribuo.classification.sgd.objectives.LogMulticlass;
025import org.tribuo.common.sgd.AbstractLinearSGDTrainer;
026import org.tribuo.common.sgd.SGDObjective;
027import org.tribuo.math.LinearParameters;
028import org.tribuo.math.StochasticGradientOptimiser;
029import org.tribuo.provenance.ModelProvenance;
030
031import java.util.logging.Logger;
032
033/**
034 * A trainer for a linear classifier using SGD.
035 * <p>
036 * See:
037 * <pre>
038 * Bottou L.
039 * "Large-Scale Machine Learning with Stochastic Gradient Descent"
040 * Proceedings of COMPSTAT, 2010.
041 * </pre>
042 */
043public class LinearSGDTrainer extends AbstractLinearSGDTrainer<Label,Integer,LinearSGDModel> {
044    private static final Logger logger = Logger.getLogger(LinearSGDTrainer.class.getName());
045
046    @Config(description = "The classification objective function to use.")
047    private LabelObjective objective = new LogMulticlass();
048
049    /**
050     * Constructs an SGD trainer for a linear model.
051     *
052     * @param objective       The objective function to optimise.
053     * @param optimiser       The gradient optimiser to use.
054     * @param epochs          The number of epochs (complete passes through the training data).
055     * @param loggingInterval Log the loss after this many iterations. If -1 don't log anything.
056     * @param minibatchSize   The size of any minibatches.
057     * @param seed            A seed for the random number generator, used to shuffle the examples before each epoch.
058     */
059    public LinearSGDTrainer(LabelObjective objective, StochasticGradientOptimiser optimiser, int epochs, int loggingInterval, int minibatchSize, long seed) {
060        super(optimiser, epochs, loggingInterval, minibatchSize, seed);
061        this.objective = objective;
062    }
063
064    /**
065     * Constructs an SGD trainer for a linear model.
066     * <p>
067     * Sets the minibatch size to 1.
068     *
069     * @param objective       The objective function to optimise.
070     * @param optimiser       The gradient optimiser to use.
071     * @param epochs          The number of epochs (complete passes through the training data).
072     * @param loggingInterval Log the loss after this many iterations. If -1 don't log anything.
073     * @param seed            A seed for the random number generator, used to shuffle the examples before each epoch.
074     */
075    public LinearSGDTrainer(LabelObjective objective, StochasticGradientOptimiser optimiser, int epochs, int loggingInterval, long seed) {
076        this(objective, optimiser, epochs, loggingInterval, 1, seed);
077    }
078
079    /**
080     * Constructs an SGD trainer for a linear model.
081     * <p>
082     * Sets the minibatch size to 1 and the logging interval to 1000.
083     *
084     * @param objective The objective function to optimise.
085     * @param optimiser The gradient optimiser to use.
086     * @param epochs    The number of epochs (complete passes through the training data).
087     * @param seed      A seed for the random number generator, used to shuffle the examples before each epoch.
088     */
089    public LinearSGDTrainer(LabelObjective objective, StochasticGradientOptimiser optimiser, int epochs, long seed) {
090        this(objective, optimiser, epochs, 1000, 1, seed);
091    }
092
093    /**
094     * For olcut.
095     */
096    private LinearSGDTrainer() {
097        super();
098    }
099
100    @Override
101    protected Integer getTarget(ImmutableOutputInfo<Label> outputInfo, Label output) {
102        return outputInfo.getID(output);
103    }
104
105    @Override
106    protected SGDObjective<Integer> getObjective() {
107        return objective;
108    }
109
110    @Override
111    protected LinearSGDModel createModel(String name, ModelProvenance provenance, ImmutableFeatureMap featureMap, ImmutableOutputInfo<Label> outputInfo, LinearParameters parameters) {
112        return new LinearSGDModel(name, provenance, featureMap, outputInfo, parameters, objective.getNormalizer(), objective.isProbabilistic());
113    }
114
115    @Override
116    protected String getModelClassName() {
117        return LinearSGDModel.class.getName();
118    }
119
120    @Override
121    public String toString() {
122        return "LinearSGDTrainer(objective=" + objective.toString() + ",optimiser=" + optimiser.toString() + ",epochs=" + epochs + ",minibatchSize=" + minibatchSize + ",seed=" + seed + ")";
123    }
124}