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.ArgumentException;
020import com.oracle.labs.mlrg.olcut.config.Option;
021import org.tribuo.Trainer;
022import org.tribuo.classification.ClassificationOptions;
023import org.tribuo.classification.sgd.LabelObjective;
024import org.tribuo.classification.sgd.objectives.Hinge;
025import org.tribuo.classification.sgd.objectives.LogMulticlass;
026import org.tribuo.math.optimisers.GradientOptimiserOptions;
027
028import java.util.logging.Logger;
029
030/**
031 * CLI options for training a linear classifier.
032 */
033public class LinearSGDOptions implements ClassificationOptions<LinearSGDTrainer> {
034    private static final Logger logger = Logger.getLogger(LinearSGDOptions.class.getName());
035
036    /**
037     * Available loss types.
038     */
039    public enum LossEnum {
040        /**
041         * Hinge loss.
042         */
043        HINGE,
044        /**
045         * Log loss, i.e., cross-entropy.
046         */
047        LOG
048    }
049
050    /**
051     * The gradient descent optimiser options.
052     */
053    public GradientOptimiserOptions sgoOptions;
054
055    /**
056     * Number of SGD epochs. Defaults to 5.
057     */
058    @Option(longName = "sgd-epochs", usage = "Number of SGD epochs. Defaults to 5.")
059    public int sgdEpochs = 5;
060    /**
061     * Loss function. Defaults to LOG.
062     */
063    @Option(longName = "sgd-objective", usage = "Loss function. Defaults to LOG.")
064    public LossEnum sgdObjective = LossEnum.LOG;
065    /**
066     * Log the objective after n examples. Defaults to 100.
067     */
068    @Option(longName = "sgd-logging-interval", usage = "Log the objective after <int> examples. Defaults to 100.")
069    public int sgdLoggingInterval = 100;
070    /**
071     * Minibatch size. Defaults to 1.
072     */
073    @Option(longName = "sgd-minibatch-size", usage = "Minibatch size. Defaults to 1.")
074    public int sgdMinibatchSize = 1;
075    /**
076     * Sets the random seed for the LinearSGDTrainer.
077     */
078    @Option(longName = "sgd-seed", usage = "Sets the random seed for the LinearSGDTrainer.")
079    public long sgdSeed = Trainer.DEFAULT_SEED;
080
081    /**
082     * Returns the loss function specified in the arguments.
083     *
084     * @return The loss function.
085     */
086    public LabelObjective getLoss() {
087        switch (sgdObjective) {
088            case HINGE:
089                return new Hinge();
090            case LOG:
091                return new LogMulticlass();
092            default:
093                throw new ArgumentException("sgd-objective", "Unknown loss function " + sgdObjective);
094        }
095    }
096
097    @Override
098    public LinearSGDTrainer getTrainer() {
099        logger.info(String.format("Set logging interval to %d", sgdLoggingInterval));
100        return new LinearSGDTrainer(getLoss(), sgoOptions.getOptimiser(), sgdEpochs, sgdLoggingInterval, sgdMinibatchSize, sgdSeed);
101    }
102}