001/*
002 * Copyright (c) 2021, 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.fm;
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 factorization machine classifier.
032 */
033public class FMClassificationOptions implements ClassificationOptions<FMClassificationTrainer> {
034    private static final Logger logger = Logger.getLogger(FMClassificationOptions.class.getName());
035
036    /**
037     * Available loss types.
038     */
039    public enum LossEnum {
040        /**
041         * Hinge loss (like an SVM).
042         */
043        HINGE,
044        /**
045         * Log loss (i.e., a logistic regression).
046         */
047        LOG
048    }
049
050    /**
051     * The gradient descent algorithm options.
052     */
053    public GradientOptimiserOptions sgoOptions;
054
055    /**
056     * Number of SGD epochs.
057     */
058    @Option(longName = "fm-epochs", usage = "Number of SGD epochs.")
059    public int fmEpochs = 5;
060    /**
061     * Loss function.
062     */
063    @Option(longName = "fm-objective", usage = "Loss function.")
064    public LossEnum fmObjective = LossEnum.LOG;
065    /**
066     * Log the objective after n examples.
067     */
068    @Option(longName = "fm-logging-interval", usage = "Log the objective after <int> examples.")
069    public int fmLoggingInterval = 100;
070    /**
071     * Minibatch size.
072     */
073    @Option(longName = "fm-minibatch-size", usage = "Minibatch size.")
074    public int fmMinibatchSize = 1;
075    /**
076     * Sets the random seed for the FMClassificationTrainer.
077     */
078    @Option(longName = "fm-seed", usage = "Sets the random seed for the FMClassificationTrainer.")
079    private long fmSeed = Trainer.DEFAULT_SEED;
080    /**
081     * Factor size.
082     */
083    @Option(longName = "fm-factor-size", usage = "Factor size.")
084    public int fmFactorSize = 5;
085    /**
086     * Variance of the initialization gaussian.
087     */
088    @Option(longName = "fm-variance", usage = "Variance of the initialization gaussian.")
089    public double fmVariance = 0.5;
090
091    /**
092     * Returns the loss function specified in the arguments.
093     * @return The loss function.
094     */
095    public LabelObjective getLoss() {
096        switch (fmObjective) {
097            case HINGE:
098                return new Hinge();
099            case LOG:
100                return new LogMulticlass();
101            default:
102                throw new ArgumentException("sgd-objective", "Unknown loss function " + fmObjective);
103        }
104    }
105
106    @Override
107    public FMClassificationTrainer getTrainer() {
108        logger.info(String.format("Set logging interval to %d", fmLoggingInterval));
109        return new FMClassificationTrainer(getLoss(), sgoOptions.getOptimiser(), fmEpochs, fmLoggingInterval,
110                fmMinibatchSize, fmSeed, fmFactorSize, fmVariance);
111    }
112}