public boolean train( InstanceList ilist, InstanceList validation, InstanceList testing, TransducerEvaluator eval) { assert (ilist.size() > 0); if (emissionEstimator == null) { emissionEstimator = new Multinomial.LaplaceEstimator[numStates()]; transitionEstimator = new Multinomial.LaplaceEstimator[numStates()]; emissionMultinomial = new Multinomial[numStates()]; transitionMultinomial = new Multinomial[numStates()]; Alphabet transitionAlphabet = new Alphabet(); for (int i = 0; i < numStates(); i++) transitionAlphabet.lookupIndex(((State) states.get(i)).getName(), true); for (int i = 0; i < numStates(); i++) { emissionEstimator[i] = new Multinomial.LaplaceEstimator(inputAlphabet); transitionEstimator[i] = new Multinomial.LaplaceEstimator(transitionAlphabet); emissionMultinomial[i] = new Multinomial(getUniformArray(inputAlphabet.size()), inputAlphabet); transitionMultinomial[i] = new Multinomial(getUniformArray(transitionAlphabet.size()), transitionAlphabet); } initialEstimator = new Multinomial.LaplaceEstimator(transitionAlphabet); } for (Instance instance : ilist) { FeatureSequence input = (FeatureSequence) instance.getData(); FeatureSequence output = (FeatureSequence) instance.getTarget(); new SumLatticeDefault(this, input, output, new Incrementor()); } initialMultinomial = initialEstimator.estimate(); for (int i = 0; i < numStates(); i++) { emissionMultinomial[i] = emissionEstimator[i].estimate(); transitionMultinomial[i] = transitionEstimator[i].estimate(); getState(i).setInitialWeight(initialMultinomial.logProbability(getState(i).getName())); } return true; }
public void estimate() { Alphabet transitionAlphabet = getTransitionAlphabet(); initialMultinomial = initialEstimator.estimate(); initialEstimator = new Multinomial.LaplaceEstimator(transitionAlphabet); for (int i = 0; i < numStates(); i++) { State s = (State) getState(i); emissionMultinomial[i] = emissionEstimator[i].estimate(); transitionMultinomial[i] = transitionEstimator[i].estimate(); s.setInitialWeight(initialMultinomial.logProbability(s.getName())); // reset estimators emissionEstimator[i] = new Multinomial.LaplaceEstimator(inputAlphabet); transitionEstimator[i] = new Multinomial.LaplaceEstimator(transitionAlphabet); } }
/** * Separate initialization of initial/transitions and emissions. All probabilities are * proportional to (1+Uniform[0,1])^noise. * * @author kedarb * @param random Random object (if null use uniform distribution) * @param noise Noise exponent to use. If zero, then uniform distribution. */ public void initTransitions(Random random, double noise) { Alphabet transitionAlphabet = getTransitionAlphabet(); initialMultinomial = new Multinomial( getRandomArray(transitionAlphabet.size(), random, noise), transitionAlphabet); initialEstimator = new Multinomial.LaplaceEstimator(transitionAlphabet); transitionMultinomial = new Multinomial[numStates()]; transitionEstimator = new Multinomial.LaplaceEstimator[numStates()]; for (int i = 0; i < numStates(); i++) { transitionMultinomial[i] = new Multinomial( getRandomArray(transitionAlphabet.size(), random, noise), transitionAlphabet); transitionEstimator[i] = new Multinomial.LaplaceEstimator(transitionAlphabet); // set state's initial weight State s = (State) getState(i); s.setInitialWeight(initialMultinomial.logProbability(s.getName())); } }