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 {HINGE, LOG} 040 041 public GradientOptimiserOptions sgoOptions; 042 043 @Option(longName = "sgd-epochs", usage = "Number of SGD epochs. Defaults to 5.") 044 public int sgdEpochs = 5; 045 @Option(longName = "sgd-objective", usage = "Loss function. Defaults to LOG.") 046 public LossEnum sgdObjective = LossEnum.LOG; 047 @Option(longName = "sgd-logging-interval", usage = "Log the objective after <int> examples. Defaults to 100.") 048 public int sgdLoggingInterval = 100; 049 @Option(longName = "sgd-minibatch-size", usage = "Minibatch size. Defaults to 1.") 050 public int sgdMinibatchSize = 1; 051 @Option(longName = "sgd-seed", usage = "Sets the random seed for the LinearSGDTrainer.") 052 private long sgdSeed = Trainer.DEFAULT_SEED; 053 054 /** 055 * Returns the loss function specified in the arguments. 056 * @return The loss function. 057 */ 058 public LabelObjective getLoss() { 059 switch (sgdObjective) { 060 case HINGE: 061 return new Hinge(); 062 case LOG: 063 return new LogMulticlass(); 064 default: 065 throw new ArgumentException("sgd-objective", "Unknown loss function " + sgdObjective); 066 } 067 } 068 069 @Override 070 public LinearSGDTrainer getTrainer() { 071 logger.info(String.format("Set logging interval to %d", sgdLoggingInterval)); 072 return new LinearSGDTrainer(getLoss(), sgoOptions.getOptimiser(), sgdEpochs, sgdLoggingInterval, sgdMinibatchSize, sgdSeed); 073 } 074}