Search in sources :

Example 1 with CustomConfiguration

use of edu.cmu.ml.proppr.util.CustomConfiguration in project ProPPR by TeamCohen.

the class GradientFinder method main.

public static void main(String[] args) {
    try {
        int inputFiles = Configuration.USE_GROUNDED | Configuration.USE_INIT_PARAMS;
        int outputFiles = Configuration.USE_GRADIENT | Configuration.USE_PARAMS;
        int modules = Configuration.USE_TRAINER | Configuration.USE_SRW | Configuration.USE_SQUASHFUNCTION;
        int constants = Configuration.USE_THREADS | Configuration.USE_EPOCHS | Configuration.USE_FORCE | Configuration.USE_FIXEDWEIGHTS;
        CustomConfiguration c = new CustomConfiguration(args, inputFiles, outputFiles, constants, modules) {

            boolean relax;

            @Override
            protected Option checkOption(Option o) {
                if (PARAMS_FILE_OPTION.equals(o.getLongOpt()) || INIT_PARAMS_FILE_OPTION.equals(o.getLongOpt()))
                    o.setRequired(false);
                return o;
            }

            @Override
            protected void addCustomOptions(Options options, int[] flags) {
                options.addOption(Option.builder().longOpt("relaxFW").desc("Relax fixedWeight rules for gradient computation (used in ProngHorn)").optionalArg(true).build());
            }

            @Override
            protected void retrieveCustomSettings(CommandLine line, int[] flags, Options options) {
                if (groundedFile == null || !groundedFile.exists())
                    usageOptions(options, flags, "Must specify grounded file using --" + Configuration.GROUNDED_FILE_OPTION);
                if (gradientFile == null)
                    usageOptions(options, flags, "Must specify gradient using --" + Configuration.GRADIENT_FILE_OPTION);
                // default to 0 epochs
                if (!options.hasOption("epochs"))
                    this.epochs = 0;
                this.relax = false;
                if (options.hasOption("relaxFW"))
                    this.relax = true;
            }

            @Override
            public Object getCustomSetting(String name) {
                if ("relaxFW".equals(name))
                    return this.relax;
                return null;
            }
        };
        System.out.println(c.toString());
        ParamVector<String, ?> params = null;
        SymbolTable<String> masterFeatures = new SimpleSymbolTable<String>();
        File featureIndex = new File(c.groundedFile.getParent(), c.groundedFile.getName() + Grounder.FEATURE_INDEX_EXTENSION);
        if (featureIndex.exists()) {
            log.info("Reading feature index from " + featureIndex.getName() + "...");
            for (String line : new ParsedFile(featureIndex)) {
                masterFeatures.insert(line.trim());
            }
        }
        if (c.epochs > 0) {
            // train first
            log.info("Training for " + c.epochs + " epochs...");
            params = c.trainer.train(masterFeatures, new ParsedFile(c.groundedFile), new ArrayLearningGraphBuilder(), // create a parameter vector
            c.initParamsFile, c.epochs);
            if (c.paramsFile != null)
                ParamsFile.save(params, c.paramsFile, c);
        } else if (c.initParamsFile != null) {
            params = new SimpleParamVector<String>(Dictionary.load(new ParsedFile(c.initParamsFile)));
        } else if (c.paramsFile != null) {
            params = new SimpleParamVector<String>(Dictionary.load(new ParsedFile(c.paramsFile)));
        } else {
            params = new SimpleParamVector<String>();
        }
        // this lets prongHorn hold external features fixed for training, but still compute their gradient
        if (((Boolean) c.getCustomSetting("relaxFW"))) {
            log.info("Turning off fixedWeight rules");
            c.trainer.setFixedWeightRules(new FixedWeightRules());
        }
        ParamVector<String, ?> batchGradient = c.trainer.findGradient(masterFeatures, new ParsedFile(c.groundedFile), new ArrayLearningGraphBuilder(), params);
        ParamsFile.save(batchGradient, c.gradientFile, c);
    } catch (Throwable t) {
        t.printStackTrace();
        System.exit(-1);
    }
}
Also used : Options(org.apache.commons.cli.Options) CustomConfiguration(edu.cmu.ml.proppr.util.CustomConfiguration) SimpleParamVector(edu.cmu.ml.proppr.util.math.SimpleParamVector) CommandLine(org.apache.commons.cli.CommandLine) SimpleSymbolTable(edu.cmu.ml.proppr.util.SimpleSymbolTable) FixedWeightRules(edu.cmu.ml.proppr.learn.tools.FixedWeightRules) Option(org.apache.commons.cli.Option) ParsedFile(edu.cmu.ml.proppr.util.ParsedFile) File(java.io.File) ParamsFile(edu.cmu.ml.proppr.util.ParamsFile) ParsedFile(edu.cmu.ml.proppr.util.ParsedFile) ArrayLearningGraphBuilder(edu.cmu.ml.proppr.graph.ArrayLearningGraphBuilder)

Example 2 with CustomConfiguration

use of edu.cmu.ml.proppr.util.CustomConfiguration in project ProPPR by TeamCohen.

the class PathDprProver method main.

public static void main(String[] args) throws LogicProgramException {
    CustomConfiguration c = new CustomConfiguration(args, //input
    Configuration.USE_PARAMS, //output
    0, //constants
    Configuration.USE_WAM | Configuration.USE_SQUASHFUNCTION, //modules
    0) {

        String query;

        @Override
        protected void addCustomOptions(Options options, int[] flags) {
            options.getOption(Configuration.PARAMS_FILE_OPTION).setRequired(false);
            options.addOption(OptionBuilder.withLongOpt("query").withArgName("functor(arg1,Var1)").hasArg().isRequired().withDescription("specify query to print top paths for").create());
        //TODO: add prompt option (for large datasets)
        }

        @Override
        protected void retrieveCustomSettings(CommandLine line, int[] flags, Options options) {
            query = line.getOptionValue("query");
        }

        @Override
        public Object getCustomSetting(String name) {
            return query;
        }
    };
    PathDprProver p = new PathDprProver(c.apr);
    Query query = Query.parse((String) c.getCustomSetting(null));
    StateProofGraph pg = new StateProofGraph(query, c.apr, c.program, c.plugins);
    p.prove(pg, new StatusLogger());
}
Also used : StatusLogger(edu.cmu.ml.proppr.util.StatusLogger) Options(org.apache.commons.cli.Options) APROptions(edu.cmu.ml.proppr.util.APROptions) CommandLine(org.apache.commons.cli.CommandLine) Query(edu.cmu.ml.proppr.prove.wam.Query) CustomConfiguration(edu.cmu.ml.proppr.util.CustomConfiguration) StateProofGraph(edu.cmu.ml.proppr.prove.wam.StateProofGraph)

Aggregations

CustomConfiguration (edu.cmu.ml.proppr.util.CustomConfiguration)2 CommandLine (org.apache.commons.cli.CommandLine)2 Options (org.apache.commons.cli.Options)2 ArrayLearningGraphBuilder (edu.cmu.ml.proppr.graph.ArrayLearningGraphBuilder)1 FixedWeightRules (edu.cmu.ml.proppr.learn.tools.FixedWeightRules)1 Query (edu.cmu.ml.proppr.prove.wam.Query)1 StateProofGraph (edu.cmu.ml.proppr.prove.wam.StateProofGraph)1 APROptions (edu.cmu.ml.proppr.util.APROptions)1 ParamsFile (edu.cmu.ml.proppr.util.ParamsFile)1 ParsedFile (edu.cmu.ml.proppr.util.ParsedFile)1 SimpleSymbolTable (edu.cmu.ml.proppr.util.SimpleSymbolTable)1 StatusLogger (edu.cmu.ml.proppr.util.StatusLogger)1 SimpleParamVector (edu.cmu.ml.proppr.util.math.SimpleParamVector)1 File (java.io.File)1 Option (org.apache.commons.cli.Option)1