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.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.AbstractFMTrainer; 026import org.tribuo.common.sgd.FMParameters; 027import org.tribuo.common.sgd.SGDObjective; 028import org.tribuo.math.StochasticGradientOptimiser; 029import org.tribuo.provenance.ModelProvenance; 030 031import java.util.logging.Logger; 032 033/** 034 * A trainer for a classification factorization machine using SGD. 035 * <p> 036 * See: 037 * <pre> 038 * Rendle, S. 039 * Factorization machines. 040 * 2010 IEEE International Conference on Data Mining 041 * </pre> 042 */ 043public class FMClassificationTrainer extends AbstractFMTrainer<Label, Integer, FMClassificationModel> { 044 private static final Logger logger = Logger.getLogger(FMClassificationTrainer.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 factorization machine. 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 * @param factorizedDimSize Size of the factorized feature representation. 059 * @param variance The variance of the initializer. 060 */ 061 public FMClassificationTrainer(LabelObjective objective, StochasticGradientOptimiser optimiser, int epochs, 062 int loggingInterval, int minibatchSize, long seed, 063 int factorizedDimSize, double variance) { 064 super(optimiser, epochs, loggingInterval, minibatchSize, seed, factorizedDimSize, variance); 065 this.objective = objective; 066 } 067 068 /** 069 * Constructs an SGD trainer for a factorization machine. 070 * <p> 071 * Sets the minibatch size to 1. 072 * 073 * @param objective The objective function to optimise. 074 * @param optimiser The gradient optimiser to use. 075 * @param epochs The number of epochs (complete passes through the training data). 076 * @param loggingInterval Log the loss after this many iterations. If -1 don't log anything. 077 * @param seed A seed for the random number generator, used to shuffle the examples before each epoch. 078 * @param factorizedDimSize Size of the factorized feature representation. 079 * @param variance The variance of the initializer. 080 */ 081 public FMClassificationTrainer(LabelObjective objective, StochasticGradientOptimiser optimiser, int epochs, 082 int loggingInterval, long seed, 083 int factorizedDimSize, double variance) { 084 this(objective, optimiser, epochs, loggingInterval, 1, seed, factorizedDimSize, variance); 085 } 086 087 /** 088 * Constructs an SGD trainer for a factorization machine. 089 * <p> 090 * Sets the minibatch size to 1 and the logging interval to 1000. 091 * 092 * @param objective The objective function to optimise. 093 * @param optimiser The gradient optimiser to use. 094 * @param epochs The number of epochs (complete passes through the training data). 095 * @param seed A seed for the random number generator, used to shuffle the examples before each epoch. 096 * @param factorizedDimSize Size of the factorized feature representation. 097 * @param variance The variance of the initializer. 098 */ 099 public FMClassificationTrainer(LabelObjective objective, StochasticGradientOptimiser optimiser, int epochs, 100 long seed, int factorizedDimSize, double variance) { 101 this(objective, optimiser, epochs, 1000, 1, seed, factorizedDimSize, variance); 102 } 103 104 /** 105 * For olcut. 106 */ 107 private FMClassificationTrainer() { 108 super(); 109 } 110 111 @Override 112 protected Integer getTarget(ImmutableOutputInfo<Label> outputInfo, Label output) { 113 return outputInfo.getID(output); 114 } 115 116 @Override 117 protected SGDObjective<Integer> getObjective() { 118 return objective; 119 } 120 121 @Override 122 protected FMClassificationModel createModel(String name, ModelProvenance provenance, ImmutableFeatureMap featureMap, ImmutableOutputInfo<Label> outputInfo, FMParameters parameters) { 123 return new FMClassificationModel(name, provenance, featureMap, outputInfo, parameters, objective.getNormalizer(), objective.isProbabilistic()); 124 } 125 126 @Override 127 protected String getModelClassName() { 128 return FMClassificationModel.class.getName(); 129 } 130 131 @Override 132 public String toString() { 133 return "FMClassificationTrainer(objective=" + objective.toString() + ",optimiser=" + optimiser.toString() + 134 ",epochs=" + epochs + ",minibatchSize=" + minibatchSize + ",seed=" + seed + 135 ",factorizedDimSize=" + factorizedDimSize + ",variance=" + variance + 136 ")"; 137 } 138}