    001    package calhoun.analysis.crf.solver;
    002
    003    import java.io.File;
    004    import java.util.List;
    005
    006    import calhoun.analysis.crf.CRFTraining;
    007    import calhoun.analysis.crf.Conrad;
    008    import calhoun.analysis.crf.ModelManager;
    009    import calhoun.analysis.crf.io.TrainingSequence;
    010
    011    /** an optimizer that uses the weights from another model as the seed for a new optimization.  Allows a second pass optimization on an already trained model. */
    012    public class SeededOptimizer implements CRFTraining {
    013
    014            CRFTraining seededOptimizer;
    015            File seedModel;
    016
    017            public double[] optimize(ModelManager fm, List<? extends TrainingSequence<?>> data) {
    018                    try {
    019                            Conrad seed = Conrad.read(seedModel.getPath());
    020                            seededOptimizer.setStarts(seed.getWeights());
    021                            return seededOptimizer.optimize(fm ,data);
    022                    }
    023                    catch(Exception ex) {
    024                            throw new RuntimeException(ex);
    025                    }
    026            }
    027
    028            /** starting weights are ignored for a SeededOptimizer, since they are always taken from the seeded model. */
    029            public void setStarts(double[] weights) {
    030                    throw new UnsupportedOperationException("Cannot set starts on a SeededOptimizer.");
    031            }
    032
    033            /**
    034             * @return the seededOptimizer
    035             */
    036            public CRFTraining getSeededOptimizer() {
    037                    return seededOptimizer;
    038            }
    039
    040            /**
    041             * @param seededOptimizer the seededOptimizer to set
    042             */
    043            public void setSeededOptimizer(CRFTraining seededOptimizer) {
    044                    this.seededOptimizer = seededOptimizer;
    045            }
    046
    047            /**
    048             * @return the seedModel
    049             */
    050            public File getSeedModel() {
    051                    return seedModel;
    052            }
    053
    054            /**
    055             * @param seedModel the seedModel to set
    056             */
    057            public void setSeedModel(File seedModel) {
    058                    this.seedModel = seedModel;
    059            }
    060    }