Search in sources :

Example 1 with SquaredLoss

use of org.tribuo.regression.sgd.objectives.SquaredLoss in project tribuo by oracle.

the class TestSGDLinear method testNegativeInvocationCount.

@Test
public void testNegativeInvocationCount() {
    assertThrows(IllegalArgumentException.class, () -> {
        LinearSGDTrainer t = new LinearSGDTrainer(new SquaredLoss(), new AdaGrad(0.1, 0.1), 5, 1000);
        t.setInvocationCount(-1);
    });
}
Also used : AbstractLinearSGDTrainer(org.tribuo.common.sgd.AbstractLinearSGDTrainer) SquaredLoss(org.tribuo.regression.sgd.objectives.SquaredLoss) AdaGrad(org.tribuo.math.optimisers.AdaGrad) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with SquaredLoss

use of org.tribuo.regression.sgd.objectives.SquaredLoss in project tribuo by oracle.

the class TrainTest method main.

/**
 * Runs a TrainTest CLI.
 * @param args the command line arguments
 * @throws IOException if there is any error reading the examples.
 */
public static void main(String[] args) throws IOException {
    // 
    // Use the labs format logging.
    LabsLogFormatter.setAllLogFormatters();
    SGDOptions o = new SGDOptions();
    ConfigurationManager cm;
    try {
        cm = new ConfigurationManager(args, o);
    } catch (UsageException e) {
        logger.info(e.getMessage());
        return;
    }
    if (o.general.trainingPath == null || o.general.testingPath == null) {
        logger.info(cm.usage());
        return;
    }
    logger.info("Configuring gradient optimiser");
    RegressionObjective obj = null;
    switch(o.loss) {
        case ABSOLUTE:
            obj = new AbsoluteLoss();
            break;
        case SQUARED:
            obj = new SquaredLoss();
            break;
        case HUBER:
            obj = new Huber();
            break;
        default:
            logger.warning("Unknown objective function " + o.loss);
            logger.info(cm.usage());
            return;
    }
    StochasticGradientOptimiser grad = o.gradientOptions.getOptimiser();
    logger.info(String.format("Set logging interval to %d", o.loggingInterval));
    RegressionFactory factory = new RegressionFactory();
    Pair<Dataset<Regressor>, Dataset<Regressor>> data = o.general.load(factory);
    Dataset<Regressor> train = data.getA();
    Dataset<Regressor> test = data.getB();
    Trainer<Regressor> trainer = new LinearSGDTrainer(obj, grad, o.epochs, o.loggingInterval, o.minibatchSize, o.general.seed);
    logger.info("Training using " + trainer.toString());
    final long trainStart = System.currentTimeMillis();
    Model<Regressor> model = trainer.train(train);
    final long trainStop = System.currentTimeMillis();
    logger.info("Finished training regressor " + Util.formatDuration(trainStart, trainStop));
    final long testStart = System.currentTimeMillis();
    RegressionEvaluation evaluation = factory.getEvaluator().evaluate(model, test);
    final long testStop = System.currentTimeMillis();
    logger.info("Finished evaluating model " + Util.formatDuration(testStart, testStop));
    System.out.println(evaluation.toString());
    if (o.general.outputPath != null) {
        o.general.saveModel(model);
    }
}
Also used : UsageException(com.oracle.labs.mlrg.olcut.config.UsageException) LinearSGDTrainer(org.tribuo.regression.sgd.linear.LinearSGDTrainer) SquaredLoss(org.tribuo.regression.sgd.objectives.SquaredLoss) RegressionFactory(org.tribuo.regression.RegressionFactory) Dataset(org.tribuo.Dataset) Huber(org.tribuo.regression.sgd.objectives.Huber) AbsoluteLoss(org.tribuo.regression.sgd.objectives.AbsoluteLoss) RegressionEvaluation(org.tribuo.regression.evaluation.RegressionEvaluation) StochasticGradientOptimiser(org.tribuo.math.StochasticGradientOptimiser) Regressor(org.tribuo.regression.Regressor) ConfigurationManager(com.oracle.labs.mlrg.olcut.config.ConfigurationManager)

Example 3 with SquaredLoss

use of org.tribuo.regression.sgd.objectives.SquaredLoss in project tribuo by oracle.

the class EnsembleExportTest method testHeterogeneousRegressionExport.

@Test
public void testHeterogeneousRegressionExport() throws IOException, OrtException {
    // Prep data
    DataSource<Regressor> trainSource = new NonlinearGaussianDataSource(100, 1L);
    MutableDataset<Regressor> train = new MutableDataset<>(trainSource);
    DataSource<Regressor> testSource = new NonlinearGaussianDataSource(100, 2L);
    MutableDataset<Regressor> test = new MutableDataset<>(testSource);
    // Train model
    SquaredLoss loss = new SquaredLoss();
    AdaGrad adagrad = new AdaGrad(0.1, 0.1);
    org.tribuo.regression.sgd.linear.LinearSGDTrainer lr = new org.tribuo.regression.sgd.linear.LinearSGDTrainer(loss, adagrad, 2, 1L);
    BaggingTrainer<Regressor> t = new BaggingTrainer<>(lr, AVERAGING, 5);
    WeightedEnsembleModel<Regressor> bagModel = (WeightedEnsembleModel<Regressor>) t.train(train);
    FMRegressionTrainer fmT = new FMRegressionTrainer(loss, adagrad, 2, 100, 1, 1L, 5, 0.1, true);
    AbstractFMModel<Regressor> fmModel = fmT.train(train);
    WeightedEnsembleModel<Regressor> ensemble = WeightedEnsembleModel.createEnsembleFromExistingModels("Bag+FM", Arrays.asList(bagModel, fmModel), AVERAGING, new float[] { 0.3f, 0.7f });
    // Write out model
    Path onnxFile = Files.createTempFile("tribuo-bagging-test", ".onnx");
    ensemble.saveONNXModel("org.tribuo.ensemble.test", 1, onnxFile);
    OnnxTestUtils.onnxRegressorComparison(ensemble, onnxFile, test, 1e-5);
    onnxFile.toFile().delete();
}
Also used : Path(java.nio.file.Path) SquaredLoss(org.tribuo.regression.sgd.objectives.SquaredLoss) NonlinearGaussianDataSource(org.tribuo.regression.example.NonlinearGaussianDataSource) FMRegressionTrainer(org.tribuo.regression.sgd.fm.FMRegressionTrainer) Regressor(org.tribuo.regression.Regressor) WeightedEnsembleModel(org.tribuo.ensemble.WeightedEnsembleModel) MutableDataset(org.tribuo.MutableDataset) AdaGrad(org.tribuo.math.optimisers.AdaGrad) BaggingTrainer(org.tribuo.ensemble.BaggingTrainer) Test(org.junit.jupiter.api.Test)

Example 4 with SquaredLoss

use of org.tribuo.regression.sgd.objectives.SquaredLoss in project tribuo by oracle.

the class EnsembleExportTest method testHomogenousRegressionExport.

@Test
public void testHomogenousRegressionExport() throws IOException, OrtException {
    // Prep data
    DataSource<Regressor> trainSource = new NonlinearGaussianDataSource(100, 1L);
    MutableDataset<Regressor> train = new MutableDataset<>(trainSource);
    DataSource<Regressor> testSource = new NonlinearGaussianDataSource(100, 2L);
    MutableDataset<Regressor> test = new MutableDataset<>(testSource);
    // Train model
    org.tribuo.regression.sgd.linear.LinearSGDTrainer lr = new org.tribuo.regression.sgd.linear.LinearSGDTrainer(new SquaredLoss(), new AdaGrad(0.1, 0.1), 2, 1L);
    BaggingTrainer<Regressor> t = new BaggingTrainer<>(lr, AVERAGING, 5);
    WeightedEnsembleModel<Regressor> ensemble = (WeightedEnsembleModel<Regressor>) t.train(train);
    // Write out model
    Path onnxFile = Files.createTempFile("tribuo-bagging-test", ".onnx");
    ensemble.saveONNXModel("org.tribuo.ensemble.test", 1, onnxFile);
    OnnxTestUtils.onnxRegressorComparison(ensemble, onnxFile, test, 1e-5);
    onnxFile.toFile().delete();
}
Also used : Path(java.nio.file.Path) SquaredLoss(org.tribuo.regression.sgd.objectives.SquaredLoss) NonlinearGaussianDataSource(org.tribuo.regression.example.NonlinearGaussianDataSource) Regressor(org.tribuo.regression.Regressor) WeightedEnsembleModel(org.tribuo.ensemble.WeightedEnsembleModel) MutableDataset(org.tribuo.MutableDataset) AdaGrad(org.tribuo.math.optimisers.AdaGrad) BaggingTrainer(org.tribuo.ensemble.BaggingTrainer) Test(org.junit.jupiter.api.Test)

Example 5 with SquaredLoss

use of org.tribuo.regression.sgd.objectives.SquaredLoss in project tribuo by oracle.

the class TrainTest method main.

/**
 * @param args the command line arguments
 * @throws IOException if there is any error reading the examples.
 */
public static void main(String[] args) throws IOException {
    // 
    // Use the labs format logging.
    LabsLogFormatter.setAllLogFormatters();
    FMRegressionOptions o = new FMRegressionOptions();
    ConfigurationManager cm;
    try {
        cm = new ConfigurationManager(args, o);
    } catch (UsageException e) {
        logger.info(e.getMessage());
        return;
    }
    if (o.general.trainingPath == null || o.general.testingPath == null) {
        logger.info(cm.usage());
        return;
    }
    logger.info("Configuring gradient optimiser");
    RegressionObjective obj = null;
    switch(o.loss) {
        case ABSOLUTE:
            obj = new AbsoluteLoss();
            break;
        case SQUARED:
            obj = new SquaredLoss();
            break;
        case HUBER:
            obj = new Huber();
            break;
        default:
            logger.warning("Unknown objective function " + o.loss);
            logger.info(cm.usage());
            return;
    }
    StochasticGradientOptimiser grad = o.gradientOptions.getOptimiser();
    logger.info(String.format("Set logging interval to %d", o.loggingInterval));
    RegressionFactory factory = new RegressionFactory();
    Pair<Dataset<Regressor>, Dataset<Regressor>> data = o.general.load(factory);
    Dataset<Regressor> train = data.getA();
    Dataset<Regressor> test = data.getB();
    logger.info("Feature domain - " + train.getFeatureIDMap());
    Trainer<Regressor> trainer = new FMRegressionTrainer(obj, grad, o.epochs, o.loggingInterval, o.minibatchSize, o.general.seed, o.factorSize, o.variance, o.standardise);
    logger.info("Training using " + trainer.toString());
    final long trainStart = System.currentTimeMillis();
    Model<Regressor> model = trainer.train(train);
    final long trainStop = System.currentTimeMillis();
    logger.info("Finished training regressor " + Util.formatDuration(trainStart, trainStop));
    final long testStart = System.currentTimeMillis();
    RegressionEvaluation evaluation = factory.getEvaluator().evaluate(model, test);
    final long testStop = System.currentTimeMillis();
    logger.info("Finished evaluating model " + Util.formatDuration(testStart, testStop));
    System.out.println(evaluation.toString());
    if (o.general.outputPath != null) {
        o.general.saveModel(model);
    }
}
Also used : UsageException(com.oracle.labs.mlrg.olcut.config.UsageException) RegressionObjective(org.tribuo.regression.sgd.RegressionObjective) SquaredLoss(org.tribuo.regression.sgd.objectives.SquaredLoss) RegressionFactory(org.tribuo.regression.RegressionFactory) Dataset(org.tribuo.Dataset) Huber(org.tribuo.regression.sgd.objectives.Huber) AbsoluteLoss(org.tribuo.regression.sgd.objectives.AbsoluteLoss) RegressionEvaluation(org.tribuo.regression.evaluation.RegressionEvaluation) StochasticGradientOptimiser(org.tribuo.math.StochasticGradientOptimiser) Regressor(org.tribuo.regression.Regressor) ConfigurationManager(com.oracle.labs.mlrg.olcut.config.ConfigurationManager)

Aggregations

SquaredLoss (org.tribuo.regression.sgd.objectives.SquaredLoss)7 Regressor (org.tribuo.regression.Regressor)6 Test (org.junit.jupiter.api.Test)5 AdaGrad (org.tribuo.math.optimisers.AdaGrad)5 Dataset (org.tribuo.Dataset)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 AbstractLinearSGDTrainer (org.tribuo.common.sgd.AbstractLinearSGDTrainer)3 RegressionEvaluation (org.tribuo.regression.evaluation.RegressionEvaluation)3 ConfigurationManager (com.oracle.labs.mlrg.olcut.config.ConfigurationManager)2 UsageException (com.oracle.labs.mlrg.olcut.config.UsageException)2 Path (java.nio.file.Path)2 MutableDataset (org.tribuo.MutableDataset)2 BaggingTrainer (org.tribuo.ensemble.BaggingTrainer)2 WeightedEnsembleModel (org.tribuo.ensemble.WeightedEnsembleModel)2 StochasticGradientOptimiser (org.tribuo.math.StochasticGradientOptimiser)2 RegressionFactory (org.tribuo.regression.RegressionFactory)2 NonlinearGaussianDataSource (org.tribuo.regression.example.NonlinearGaussianDataSource)2 AbsoluteLoss (org.tribuo.regression.sgd.objectives.AbsoluteLoss)2 Huber (org.tribuo.regression.sgd.objectives.Huber)2 Tensor (org.tribuo.math.la.Tensor)1