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;
018
019import com.oracle.labs.mlrg.olcut.config.ConfigurationManager;
020import com.oracle.labs.mlrg.olcut.config.Options;
021import com.oracle.labs.mlrg.olcut.config.UsageException;
022import org.tribuo.Trainer;
023import org.tribuo.classification.Label;
024import org.tribuo.classification.TrainTestHelper;
025import org.tribuo.classification.ensemble.ClassificationEnsembleOptions;
026import org.tribuo.classification.sgd.linear.LinearSGDOptions;
027import org.tribuo.data.DataOptions;
028
029import java.io.IOException;
030import java.util.logging.Logger;
031
032/**
033 * Build and run a classifier for a standard dataset using LinearSGDTrainer.
034 */
035public class TrainTest {
036
037    private static final Logger logger = Logger.getLogger(TrainTest.class.getName());
038
039    /**
040     * Command line options.
041     */
042    public static class TrainTestOptions implements Options {
043        @Override
044        public String getOptionsDescription() {
045            return "Trains and tests a linear SGD model on the specified datasets.";
046        }
047
048        /**
049         * The data loading options.
050         */
051        public DataOptions general;
052        /**
053         * The linear SGD trainer options.
054         */
055        public LinearSGDOptions trainerOptions;
056        /**
057         * The ensemble options.
058         */
059        public ClassificationEnsembleOptions ensembleOptions;
060    }
061
062    /**
063     * Runs a TrainTest CLI.
064     * @param args the command line arguments
065     * @throws IOException if there is any error reading the examples.
066     */
067    public static void main(String[] args) throws IOException {
068        TrainTestOptions o = new TrainTestOptions();
069        try (ConfigurationManager cm = new ConfigurationManager(args,o)){
070            Trainer<Label> trainer = o.trainerOptions.getTrainer();
071            trainer = o.ensembleOptions.wrapTrainer(trainer);
072            TrainTestHelper.run(cm, o.general, trainer);
073        } catch (UsageException e) {
074            logger.info(e.getMessage());
075        }
076    }
077}