001/* 002 * Copyright (c) 2020, 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 org.tribuo.Output; 020import org.tribuo.math.LinearParameters; 021import org.tribuo.math.StochasticGradientOptimiser; 022 023import java.util.SplittableRandom; 024import java.util.logging.Logger; 025 026/** 027 * A trainer for a linear model which uses SGD. 028 * <p> 029 * It's an {@link AbstractSGDTrainer} operating on {@link LinearParameters}, with 030 * the bias folded into the features. 031 * <p> 032 * See: 033 * <pre> 034 * Bottou L. 035 * "Large-Scale Machine Learning with Stochastic Gradient Descent" 036 * Proceedings of COMPSTAT, 2010. 037 * </pre> 038 */ 039public abstract class AbstractLinearSGDTrainer<T extends Output<T>,U,V extends AbstractLinearSGDModel<T>> extends AbstractSGDTrainer<T,U,V,LinearParameters> { 040 private static final Logger logger = Logger.getLogger(AbstractLinearSGDTrainer.class.getName()); 041 042 /** 043 * Constructs an SGD trainer for a linear model. 044 * @param optimiser The gradient optimiser to use. 045 * @param epochs The number of epochs (complete passes through the training data). 046 * @param loggingInterval Log the loss after this many iterations. If -1 don't log anything. 047 * @param minibatchSize The size of any minibatches. 048 * @param seed A seed for the random number generator, used to shuffle the examples before each epoch. 049 */ 050 protected AbstractLinearSGDTrainer(StochasticGradientOptimiser optimiser, int epochs, int loggingInterval, int minibatchSize, long seed) { 051 super(optimiser,epochs,loggingInterval,minibatchSize,seed,true); 052 postConfig(); 053 } 054 055 /** 056 * For olcut. 057 */ 058 protected AbstractLinearSGDTrainer() { 059 super(true); 060 } 061 062 /** 063 * Returns the default model name. 064 * @return The default model name. 065 */ 066 @Override 067 protected String getName() { 068 return "linear-sgd-model"; 069 } 070 071 /** 072 * Constructs the trainable parameters object, in this case a {@link LinearParameters} containing 073 * a single weight matrix. 074 * @param numFeatures The number of input features. 075 * @param numOutputs The number of output dimensions. 076 * @param localRNG The RNG to use for parameter initialisation. 077 * @return The trainable parameters. 078 */ 079 @Override 080 protected LinearParameters createParameters(int numFeatures, int numOutputs, SplittableRandom localRNG) { 081 return new LinearParameters(numFeatures+1,numOutputs); 082 } 083 084}