<commit_before>package edu.neu.ccs.pyramid.experiment;

import edu.neu.ccs.pyramid.classification.boosting.lktb.LKTBConfig;
import edu.neu.ccs.pyramid.classification.boosting.lktb.LKTBTrainer;
import edu.neu.ccs.pyramid.classification.boosting.lktb.LKTreeBoost;
import edu.neu.ccs.pyramid.configuration.Config;
import edu.neu.ccs.pyramid.dataset.ClfDataSet;
import edu.neu.ccs.pyramid.dataset.DataSetType;
import edu.neu.ccs.pyramid.dataset.TRECFormat;
import edu.neu.ccs.pyramid.eval.Accuracy;
import edu.neu.ccs.pyramid.regression.Regressor;
import edu.neu.ccs.pyramid.regression.probabilistic_regression_tree.ProbRegStump;
import edu.neu.ccs.pyramid.regression.probabilistic_regression_tree.ProbRegStumpTrainer;
import edu.neu.ccs.pyramid.regression.regression_tree.LeafOutputType;
import edu.neu.ccs.pyramid.regression.regression_tree.RegressionTree;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.util.List;

/**
 * hard tree vs expectation tree vs hybrid tree, accuracy test, on fiji, with shrinkage, without newton
 * Created by chengli on 6/1/15.
 */
public class Exp117 {
    public static void main(String[] args) throws Exception{
        if (args.length !=1){
            throw new IllegalArgumentException("please specify the config file");
        }

        Config config = new Config(args[0]);
        System.out.println(config);
        File outputFolder = new File(config.getString("output.folder"));
        outputFolder.mkdirs();
        FileUtils.cleanDirectory(outputFolder);
        train_hard(config);
        train_expectation(config);
        train_hybrid(config);
    }

    static void train_hard(Config config) throws Exception{
        File outputFolder = new File(config.getString("output.folder"),"hard_tree");
        File inputFolder = new File(config.getString("input.folder"));
        ClfDataSet dataSet = TRECFormat.loadClfDataSet(new File(inputFolder, "train.trec"),
                DataSetType.CLF_SPARSE, true);
        System.out.println(dataSet.getMetaInfo());
        ClfDataSet testSet = TRECFormat.loadClfDataSet(new File(inputFolder, "test.trec"),
                DataSetType.CLF_SPARSE, true);

        LKTreeBoost lkTreeBoost = new LKTreeBoost(dataSet.getNumClasses());

        LKTBConfig trainConfig = new LKTBConfig.Builder(dataSet)
                .numLeaves(2).learningRate(0.1).numSplitIntervals(50).minDataPerLeaf(1)
                .dataSamplingRate(1).featureSamplingRate(1)
                .randomLevel(1)
                .setLeafOutputType(LeafOutputType.AVERAGE)
                .considerHardTree(true)
                .considerExpectationTree(false)
                .considerProbabilisticTree(false)
                .build();

        LKTBTrainer lktbTrainer = new LKTBTrainer(trainConfig,lkTreeBoost);

        File trainFile = new File(outputFolder,"train_acc");
        File testFile = new File(outputFolder,"test_acc");
        File typeFile = new File(outputFolder,"type");

        for (int i=0;i<config.getInt("iterations");i++){
            System.out.println("iteration "+i);
            System.out.println("boosting accuracy = "+ Accuracy.accuracy(lkTreeBoost, dataSet));

            lktbTrainer.iterate();


            FileUtils.writeStringToFile(trainFile,""+Accuracy.accuracy(lkTreeBoost, dataSet)+"\n",true);
            FileUtils.writeStringToFile(testFile,""+Accuracy.accuracy(lkTreeBoost, testSet)+"\n",true);
            List<Regressor> regressors = lkTreeBoost.getRegressors(0);
            Regressor regressor = regressors.get(i);
            if (regressor instanceof RegressionTree){
                FileUtils.writeStringToFile(typeFile,"hard tree"+"\n",true);

            }
            if (regressor instanceof ProbRegStump){
                ProbRegStump probRegStump = (ProbRegStump)regressor;
                if (probRegStump.getLossType()== ProbRegStumpTrainer.LossType.SquaredLossOfExpectation){
                    FileUtils.writeStringToFile(typeFile,"expectation tree"+"\n",true);
                }
                if (probRegStump.getLossType()== ProbRegStumpTrainer.LossType.ExpectationOfSquaredLoss){
                    FileUtils.writeStringToFile(typeFile,"probabilistic tree"+"\n",true);
                }
            }
        }

    }


    static void train_hybrid(Config config) throws Exception{
        File outputFolder = new File(config.getString("output.folder"),"hybrid_tree");
        File inputFolder = new File(config.getString("input.folder"));
        ClfDataSet dataSet = TRECFormat.loadClfDataSet(new File(inputFolder, "train.trec"),
                DataSetType.CLF_SPARSE, true);
        System.out.println(dataSet.getMetaInfo());
        ClfDataSet testSet = TRECFormat.loadClfDataSet(new File(inputFolder, "test.trec"),
                DataSetType.CLF_SPARSE, true);

        LKTreeBoost lkTreeBoost = new LKTreeBoost(dataSet.getNumClasses());

        LKTBConfig trainConfig = new LKTBConfig.Builder(dataSet)
                .numLeaves(2).learningRate(0.1).numSplitIntervals(50).minDataPerLeaf(1)
                .dataSamplingRate(1).featureSamplingRate(1)
                .randomLevel(1)
                .setLeafOutputType(LeafOutputType.AVERAGE)
                .considerHardTree(true)
                .considerExpectationTree(true)
                .considerProbabilisticTree(false)
                .build();

        LKTBTrainer lktbTrainer = new LKTBTrainer(trainConfig,lkTreeBoost);

        File trainFile = new File(outputFolder,"train_acc");
        File testFile = new File(outputFolder,"test_acc");
        File typeFile = new File(outputFolder,"type");

        for (int i=0;i<config.getInt("iterations");i++){
            System.out.println("iteration "+i);
            System.out.println("boosting accuracy = "+ Accuracy.accuracy(lkTreeBoost, dataSet));

            lktbTrainer.iterate();


            FileUtils.writeStringToFile(trainFile,""+Accuracy.accuracy(lkTreeBoost, dataSet)+"\n",true);
            FileUtils.writeStringToFile(testFile,""+Accuracy.accuracy(lkTreeBoost, testSet)+"\n",true);
            List<Regressor> regressors = lkTreeBoost.getRegressors(0);
            Regressor regressor = regressors.get(i);
            if (regressor instanceof RegressionTree){
                FileUtils.writeStringToFile(typeFile,"hard tree"+", ",true);

            }
            if (regressor instanceof ProbRegStump){
                ProbRegStump probRegStump = (ProbRegStump)regressor;
                if (probRegStump.getLossType()== ProbRegStumpTrainer.LossType.SquaredLossOfExpectation){
                    FileUtils.writeStringToFile(typeFile,"expectation tree"+", ",true);
                }
                if (probRegStump.getLossType()== ProbRegStumpTrainer.LossType.ExpectationOfSquaredLoss){
                    FileUtils.writeStringToFile(typeFile,"probabilistic tree"+", ",true);
                }
            }
        }

    }

    static void train_expectation(Config config) throws Exception{
        File outputFolder = new File(config.getString("output.folder"),"expectation_tree");
        File inputFolder = new File(config.getString("input.folder"));
        ClfDataSet dataSet = TRECFormat.loadClfDataSet(new File(inputFolder, "train.trec"),
                DataSetType.CLF_SPARSE, true);
        System.out.println(dataSet.getMetaInfo());
        ClfDataSet testSet = TRECFormat.loadClfDataSet(new File(inputFolder, "test.trec"),
                DataSetType.CLF_SPARSE, true);

        LKTreeBoost lkTreeBoost = new LKTreeBoost(dataSet.getNumClasses());

        LKTBConfig trainConfig = new LKTBConfig.Builder(dataSet)
                .numLeaves(2).learningRate(0.1).numSplitIntervals(50).minDataPerLeaf(1)
                .dataSamplingRate(1).featureSamplingRate(1)
                .randomLevel(1)
                .setLeafOutputType(LeafOutputType.AVERAGE)
                .considerHardTree(false)
                .considerExpectationTree(true)
                .considerProbabilisticTree(false)
                .build();

        LKTBTrainer lktbTrainer = new LKTBTrainer(trainConfig,lkTreeBoost);

        File trainFile = new File(outputFolder,"train_acc");
        File testFile = new File(outputFolder,"test_acc");
        File typeFile = new File(outputFolder,"type");

        for (int i=0;i<config.getInt("iterations");i++){
            System.out.println("iteration "+i);
            System.out.println("boosting accuracy = "+ Accuracy.accuracy(lkTreeBoost, dataSet));

            lktbTrainer.iterate();


            FileUtils.writeStringToFile(trainFile,""+Accuracy.accuracy(lkTreeBoost, dataSet)+"\n",true);
            FileUtils.writeStringToFile(testFile,""+Accuracy.accuracy(lkTreeBoost, testSet)+"\n",true);
            List<Regressor> regressors = lkTreeBoost.getRegressors(0);<commit_msg>more output in exp117<commit_after>package edu.neu.ccs.pyramid.experiment;

import edu.neu.ccs.pyramid.classification.boosting.lktb.LKTBConfig;
import edu.neu.ccs.pyramid.classification.boosting.lktb.LKTBTrainer;
import edu.neu.ccs.pyramid.classification.boosting.lktb.LKTreeBoost;
import edu.neu.ccs.pyramid.configuration.Config;
import edu.neu.ccs.pyramid.dataset.ClfDataSet;
import edu.neu.ccs.pyramid.dataset.DataSetType;
import edu.neu.ccs.pyramid.dataset.TRECFormat;
import edu.neu.ccs.pyramid.eval.Accuracy;
import edu.neu.ccs.pyramid.regression.Regressor;
import edu.neu.ccs.pyramid.regression.probabilistic_regression_tree.ProbRegStump;
import edu.neu.ccs.pyramid.regression.probabilistic_regression_tree.ProbRegStumpTrainer;
import edu.neu.ccs.pyramid.regression.regression_tree.LeafOutputType;
import edu.neu.ccs.pyramid.regression.regression_tree.RegressionTree;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.time.StopWatch;

import java.io.File;
import java.util.List;

/**
 * hard tree vs expectation tree vs hybrid tree, accuracy test, on fiji, with shrinkage, without newton
 * Created by chengli on 6/1/15.
 */
public class Exp117 {
    public static void main(String[] args) throws Exception{
        if (args.length !=1){
            throw new IllegalArgumentException("please specify the config file");
        }

        Config config = new Config(args[0]);
        System.out.println(config);
        File outputFolder = new File(config.getString("output.folder"));
        outputFolder.mkdirs();
        FileUtils.cleanDirectory(outputFolder);
        train_hard(config);
        train_expectation(config);
        train_hybrid(config);
    }

    static void train_hard(Config config) throws Exception{
        File outputFolder = new File(config.getString("output.folder"),"hard_tree");
        File inputFolder = new File(config.getString("input.folder"));
        ClfDataSet dataSet = TRECFormat.loadClfDataSet(new File(inputFolder, "train.trec"),
                DataSetType.CLF_SPARSE, true);
        System.out.println(dataSet.getMetaInfo());
        ClfDataSet testSet = TRECFormat.loadClfDataSet(new File(inputFolder, "test.trec"),
                DataSetType.CLF_SPARSE, true);

        LKTreeBoost lkTreeBoost = new LKTreeBoost(dataSet.getNumClasses());

        LKTBConfig trainConfig = new LKTBConfig.Builder(dataSet)
                .numLeaves(2).learningRate(0.1).numSplitIntervals(50).minDataPerLeaf(1)
                .dataSamplingRate(1).featureSamplingRate(1)
                .randomLevel(1)
                .setLeafOutputType(LeafOutputType.AVERAGE)
                .considerHardTree(true)
                .considerExpectationTree(false)
                .considerProbabilisticTree(false)
                .build();

        LKTBTrainer lktbTrainer = new LKTBTrainer(trainConfig,lkTreeBoost);

        File trainFile = new File(outputFolder,"train_acc");
        File testFile = new File(outputFolder,"test_acc");
        File typeFile = new File(outputFolder,"type");


        for (int i=0;i<config.getInt("iterations");i++){
            StopWatch stopWatch = new StopWatch();
            stopWatch.start();
            System.out.println("iteration "+i);
            System.out.println("boosting accuracy = "+ Accuracy.accuracy(lkTreeBoost, dataSet));

            System.out.println("time spent on accuracy = "+stopWatch);

            lktbTrainer.iterate();
            System.out.println("time spent on one iteration = "+stopWatch);

            FileUtils.writeStringToFile(trainFile, "" + Accuracy.accuracy(lkTreeBoost, dataSet) + "\n", true);
            FileUtils.writeStringToFile(testFile,""+Accuracy.accuracy(lkTreeBoost, testSet)+"\n",true);
            List<Regressor> regressors = lkTreeBoost.getRegressors(0);
            Regressor regressor = regressors.get(i);
            if (regressor instanceof RegressionTree){
                FileUtils.writeStringToFile(typeFile,"hard tree"+"\n",true);

            }
            if (regressor instanceof ProbRegStump){
                ProbRegStump probRegStump = (ProbRegStump)regressor;
                if (probRegStump.getLossType()== ProbRegStumpTrainer.LossType.SquaredLossOfExpectation){
                    FileUtils.writeStringToFile(typeFile,"expectation tree"+"\n",true);
                }
                if (probRegStump.getLossType()== ProbRegStumpTrainer.LossType.ExpectationOfSquaredLoss){
                    FileUtils.writeStringToFile(typeFile,"probabilistic tree"+"\n",true);
                }
            }
        }

    }


    static void train_hybrid(Config config) throws Exception{
        File outputFolder = new File(config.getString("output.folder"),"hybrid_tree");
        File inputFolder = new File(config.getString("input.folder"));
        ClfDataSet dataSet = TRECFormat.loadClfDataSet(new File(inputFolder, "train.trec"),
                DataSetType.CLF_SPARSE, true);
        System.out.println(dataSet.getMetaInfo());
        ClfDataSet testSet = TRECFormat.loadClfDataSet(new File(inputFolder, "test.trec"),
                DataSetType.CLF_SPARSE, true);

        LKTreeBoost lkTreeBoost = new LKTreeBoost(dataSet.getNumClasses());

        LKTBConfig trainConfig = new LKTBConfig.Builder(dataSet)
                .numLeaves(2).learningRate(0.1).numSplitIntervals(50).minDataPerLeaf(1)
                .dataSamplingRate(1).featureSamplingRate(1)
                .randomLevel(1)
                .setLeafOutputType(LeafOutputType.AVERAGE)
                .considerHardTree(true)
                .considerExpectationTree(true)
                .considerProbabilisticTree(false)
                .build();

        LKTBTrainer lktbTrainer = new LKTBTrainer(trainConfig,lkTreeBoost);

        File trainFile = new File(outputFolder,"train_acc");
        File testFile = new File(outputFolder,"test_acc");
        File typeFile = new File(outputFolder,"type");

        for (int i=0;i<config.getInt("iterations");i++){
            StopWatch stopWatch = new StopWatch();
            stopWatch.start();
            System.out.println("iteration "+i);
            System.out.println("boosting accuracy = " + Accuracy.accuracy(lkTreeBoost, dataSet));

            System.out.println("time spent on accuracy = "+stopWatch);

            lktbTrainer.iterate();
            System.out.println("time spent on one iteration = "+stopWatch);

            FileUtils.writeStringToFile(trainFile,""+Accuracy.accuracy(lkTreeBoost, dataSet)+"\n",true);
            FileUtils.writeStringToFile(testFile,""+Accuracy.accuracy(lkTreeBoost, testSet)+"\n",true);
            List<Regressor> regressors = lkTreeBoost.getRegressors(0);
            Regressor regressor = regressors.get(i);
            if (regressor instanceof RegressionTree){
                FileUtils.writeStringToFile(typeFile,"hard tree"+", ",true);

            }
            if (regressor instanceof ProbRegStump){
                ProbRegStump probRegStump = (ProbRegStump)regressor;
                if (probRegStump.getLossType()== ProbRegStumpTrainer.LossType.SquaredLossOfExpectation){
                    FileUtils.writeStringToFile(typeFile,"expectation tree"+", ",true);
                }
                if (probRegStump.getLossType()== ProbRegStumpTrainer.LossType.ExpectationOfSquaredLoss){
                    FileUtils.writeStringToFile(typeFile,"probabilistic tree"+", ",true);
                }
            }
        }

    }

    static void train_expectation(Config config) throws Exception{
        File outputFolder = new File(config.getString("output.folder"),"expectation_tree");
        File inputFolder = new File(config.getString("input.folder"));
        ClfDataSet dataSet = TRECFormat.loadClfDataSet(new File(inputFolder, "train.trec"),
                DataSetType.CLF_SPARSE, true);
        System.out.println(dataSet.getMetaInfo());
        ClfDataSet testSet = TRECFormat.loadClfDataSet(new File(inputFolder, "test.trec"),
                DataSetType.CLF_SPARSE, true);

        LKTreeBoost lkTreeBoost = new LKTreeBoost(dataSet.getNumClasses());

        LKTBConfig trainConfig = new LKTBConfig.Builder(dataSet)
                .numLeaves(2).learningRate(0.1).numSplitIntervals(50).minDataPerLeaf(1)
                .dataSamplingRate(1).featureSamplingRate(1)
                .randomLevel(1)
                .setLeafOutputType(LeafOutputType.AVERAGE)
                .considerHardTree(false)
                .considerExpectationTree(true)
                .considerProbabilisticTree(false)
                .build();

        LKTBTrainer lktbTrainer = new LKTBTrainer(trainConfig,lkTreeBoost);

        File trainFile = new File(outputFolder,"train_acc");
        File testFile = new File(outputFolder,"test_acc");
        File typeFile = new File(outputFolder,"type");

        for (int i=0;i<config.getInt("iterations");i++){
            StopWatch stopWatch = new StopWatch();
            stopWatch.start();
            System.out.println("iteration "+i);
            System.out.println("boosting accuracy = "+ Accuracy.accuracy(lkTreeBoost, dataSet));

            System.out.println("time spent on accuracy = "+stopWatch);

            lktbTrainer.iterate();
            System.out.println("time spent on one iteration = " + stopWatch);


            FileUtils.writeStringToFile(trainFile,""+Accuracy.accuracy(lkTreeBoost, dataSet)+"\n",true);
            FileUtils.writeStringToFile(testFile,""+Accuracy.accuracy(lkTreeBoost, testSet)+"\n",true);
            List<Regressor> regressors = lkTreeBoost.getRegressors(0);
{'fraction_non_alphanumeric': 0.11204359673024523, 'fraction_numerical': 0.0047956403269754765, 'mean_word_length': 2.8846316680779003, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '528159', 'n_tokens_mistral': 5788, 'n_tokens_neox': 5439, 'n_words': 811}