Package de.jungblut.nlp
Class HMM
- java.lang.Object
-
- All Implemented Interfaces:
Classifier,Predictor,org.apache.hadoop.io.Writable
public final class HMM extends AbstractClassifier implements org.apache.hadoop.io.Writable
Hidden Markov Model implementation for multiple observations for all three types of problems HMM aims to solve (Decoding, likelihood estimation, unsupervised/supervised learning).- Author:
- thomas.jungblut
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description de.jungblut.math.DoubleMatrixdecode(de.jungblut.math.DoubleVector[] observationSequence, de.jungblut.math.DoubleVector[] featuresPerHiddenState)Decodes the given observation sequence (features) with the current HMM.doubleestimateLikelihood(de.jungblut.math.DoubleVector[] observationSequence)Likelihood estimation on the current HMM.de.jungblut.math.DoubleMatrixgetEmissionProbabilitiyMatrix()de.jungblut.math.DoubleVectorgetHiddenPriorProbability()intgetNumHiddenStates()intgetNumVisibleStates()de.jungblut.math.DoubleMatrixgetTransitionProbabilityMatrix()de.jungblut.math.DoubleVectorpredict(de.jungblut.math.DoubleVector features)Classifies the given features.de.jungblut.math.DoubleVectorpredict(de.jungblut.math.DoubleVector features, de.jungblut.math.DoubleVector previousOutcome)voidreadFields(java.io.DataInput in)voidtrain(de.jungblut.math.DoubleVector[] features, de.jungblut.math.DoubleVector[] outcome)Trains this classifier with the given features and the outcome.voidtrainSupervised(de.jungblut.math.DoubleVector[] features, de.jungblut.math.DoubleVector[] outcome)Trains the current models parameters by executing a forwad pass over the given observations (hidden and visible states).voidtrainUnsupervised(de.jungblut.math.DoubleVector[] features, double epsilon, int maxIterations, boolean verbose)Trains the current models parameters by executing a baum-welch expectation maximization algorithm.voidwrite(java.io.DataOutput out)-
Methods inherited from class de.jungblut.classification.AbstractClassifier
train
-
Methods inherited from class de.jungblut.classification.AbstractPredictor
extractPredictedClass, extractPredictedClass, predictedClass, predictedClass, predictProbability
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface de.jungblut.classification.Predictor
asClassifier, extractPredictedClass, extractPredictedClass, predictedClass, predictedClass, predictProbability
-
-
-
-
Method Detail
-
estimateLikelihood
public double estimateLikelihood(de.jungblut.math.DoubleVector[] observationSequence)
Likelihood estimation on the current HMM. It estimates the likelihood that the given observation sequence is about to happen. P( O | lambda ) where O is the observation sequence and lambda are the HMM's parameters. This is done by executing the forward algorithm with the given observations clamped to the visible states.- Parameters:
observationSequence- the given sequence of observations (features).- Returns:
- the likelihood (not a probability!) that the given sequence is about to happen.
-
decode
public de.jungblut.math.DoubleMatrix decode(de.jungblut.math.DoubleVector[] observationSequence, de.jungblut.math.DoubleVector[] featuresPerHiddenState)Decodes the given observation sequence (features) with the current HMM. This discovers the best hidden state sequence Q that is derived by executing the Viterbi algorithm with the given observations and the HMM's parameters lambda. This is a proxy toViterbiUtilsdecode(DoubleVector[], DoubleVector[]).- Parameters:
observationSequence- the given sequence of features.- Returns:
- a matrix containing the predicted hidden state on each row vector.
-
trainUnsupervised
public void trainUnsupervised(de.jungblut.math.DoubleVector[] features, double epsilon, int maxIterations, boolean verbose)Trains the current models parameters by executing a baum-welch expectation maximization algorithm. TODO this should also be log-scaled for accuracy.- Parameters:
features- the visible state activations (the vector will be traversed for non-zero entries, so the value actually doesn't matter).epsilon- the absolute difference in the train model to the previous. If smaller than given value the iterations are stopped and the training finishes.maxIterations- if the epsilon threshold is never reached, the maximum iterations usually applies by stopping computation after given number of iterations.verbose- when set to true it will print information about the expectimax values per iteration.
-
trainSupervised
public void trainSupervised(de.jungblut.math.DoubleVector[] features, de.jungblut.math.DoubleVector[] outcome)Trains the current models parameters by executing a forwad pass over the given observations (hidden and visible states). Probabilities are +1 smoothed while counting in case there would be zero probability somewhere. This method is compatible to the Classifier#train method so this model can be used as a simple classifier.- Parameters:
features- the visible state activations (the vector will be traversed for non-zero entries, so the value actually doesn't matter).outcome- the outcome that was assigned to the given features. This can be in the binary case a single element vector (0d or 1d), or in the multi-class case a vector which index denotes the class (from zero to numHiddenStates, activation is again 0d or 1d). Note that in the multi-class case just a single state can be turned on, so the classes are mutual exclusive.
-
train
public void train(de.jungblut.math.DoubleVector[] features, de.jungblut.math.DoubleVector[] outcome)Description copied from interface:ClassifierTrains this classifier with the given features and the outcome.- Specified by:
trainin interfaceClassifier- Overrides:
trainin classAbstractClassifieroutcome- the outcome must have classes labeled as doubles. E.G. in the binary case you have a single element and decide between 0d and 1d. In higher dimensional cases you have each of these single elements mapped to a dimension.
-
predict
public de.jungblut.math.DoubleVector predict(de.jungblut.math.DoubleVector features)
Description copied from interface:PredictorClassifies the given features.
-
predict
public de.jungblut.math.DoubleVector predict(de.jungblut.math.DoubleVector features, de.jungblut.math.DoubleVector previousOutcome)
-
getNumHiddenStates
public int getNumHiddenStates()
-
getNumVisibleStates
public int getNumVisibleStates()
-
getEmissionProbabilitiyMatrix
public de.jungblut.math.DoubleMatrix getEmissionProbabilitiyMatrix()
-
getHiddenPriorProbability
public de.jungblut.math.DoubleVector getHiddenPriorProbability()
-
getTransitionProbabilityMatrix
public de.jungblut.math.DoubleMatrix getTransitionProbabilityMatrix()
-
write
public void write(java.io.DataOutput out) throws java.io.IOException- Specified by:
writein interfaceorg.apache.hadoop.io.Writable- Throws:
java.io.IOException
-
readFields
public void readFields(java.io.DataInput in) throws java.io.IOException- Specified by:
readFieldsin interfaceorg.apache.hadoop.io.Writable- Throws:
java.io.IOException
-
-