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.common.sgd; 018 019import com.oracle.labs.mlrg.olcut.config.Config; 020import com.oracle.labs.mlrg.olcut.config.PropertyException; 021import org.tribuo.Output; 022import org.tribuo.math.StochasticGradientOptimiser; 023 024import java.util.SplittableRandom; 025import java.util.logging.Logger; 026 027/** 028 * A trainer for a quadratic factorization machine model which uses SGD. 029 * <p> 030 * It's an {@link AbstractSGDTrainer} operating on {@link FMParameters}. 031 * <p> 032 * See: 033 * <pre> 034 * Rendle, S. 035 * Factorization machines. 036 * 2010 IEEE International Conference on Data Mining 037 * </pre> 038 */ 039public abstract class AbstractFMTrainer<T extends Output<T>, U, V extends AbstractFMModel<T>> extends AbstractSGDTrainer<T, U, V, FMParameters> { 040 private static final Logger logger = Logger.getLogger(AbstractFMTrainer.class.getName()); 041 042 @Config(mandatory = true, description = "The size of the factorized feature representation.") 043 protected int factorizedDimSize; 044 045 @Config(mandatory = true, description = "The variance of the initializer.") 046 protected double variance; 047 048 /** 049 * Constructs an SGD trainer for a factorization machine. 050 * 051 * @param optimiser The gradient optimiser to use. 052 * @param epochs The number of epochs (complete passes through the training data). 053 * @param loggingInterval Log the loss after this many iterations. If -1 don't log anything. 054 * @param minibatchSize The size of any minibatches. 055 * @param seed A seed for the random number generator, used to shuffle the examples before each epoch. 056 * @param factorizedDimSize Size of the factorized feature representation. 057 * @param variance The variance of the initializer. 058 */ 059 protected AbstractFMTrainer(StochasticGradientOptimiser optimiser, int epochs, int loggingInterval, 060 int minibatchSize, long seed, int factorizedDimSize, double variance) { 061 super(optimiser, epochs, loggingInterval, minibatchSize, seed, false); 062 this.factorizedDimSize = factorizedDimSize; 063 this.variance = variance; 064 postConfig(); 065 } 066 067 /** 068 * For olcut. 069 */ 070 protected AbstractFMTrainer() { 071 super(false); 072 } 073 074 @Override 075 public void postConfig() { 076 super.postConfig(); 077 if (factorizedDimSize < 1) { 078 throw new PropertyException("", "factorizedDimSize", "Value must be positive."); 079 } 080 if (variance <= 0.0) { 081 throw new PropertyException("", "variance", "Value must be positive."); 082 } 083 } 084 085 /** 086 * Returns the default model name. 087 * 088 * @return The default model name. 089 */ 090 @Override 091 protected String getName() { 092 return "factorization-machine-model"; 093 } 094 095 /** 096 * Constructs the trainable parameters object, in this case a {@link FMParameters} containing 097 * a weight matrix for the feature weights and a series of weight matrices for the factorized 098 * feature representation. 099 * 100 * @param numFeatures The number of input features. 101 * @param numOutputs The number of output dimensions. 102 * @param localRNG The RNG to use for parameter initialisation. 103 * @return The trainable parameters. 104 */ 105 @Override 106 protected FMParameters createParameters(int numFeatures, int numOutputs, SplittableRandom localRNG) { 107 return new FMParameters(localRNG, numFeatures, numOutputs, factorizedDimSize, variance); 108 } 109 110}