- 阅读权限
- 255
- 威望
- 0 级
- 论坛币
- 50288 个
- 通用积分
- 83.6306
- 学术水平
- 253 点
- 热心指数
- 300 点
- 信用等级
- 208 点
- 经验
- 41518 点
- 帖子
- 3256
- 精华
- 14
- 在线时间
- 766 小时
- 注册时间
- 2006-5-4
- 最后登录
- 2022-11-6
|
- package org.scalaml.supervised.hmm
- import org.scalaml.core.Types.ScalaMl._
- import org.scalaml.core.Design.{PipeOperator, Model}
- import org.scalaml.core.XTSeries
- import org.scalaml.core.Matrix
- import scala.util.{Try, Success, Failure}
- import scala.annotation.implicitNotFound
- import org.apache.log4j.Logger
- import org.scalaml.util.DisplayUtils
- import HMM._
- object HMMForm extends Enumeration {
- type HMMForm = Value
- val EVALUATION, DECODING = Value
- }
- import HMMForm._
- abstract class HMMModel(val lambda: HMMLambda, val obs: Array[Int]) extends Model {
- import HMMModel._
-
- check(obs)
- }
- object HMMModel {
- private def check(obs: Array[Int]): Unit =
- require(!obs.isEmpty, "HMMModel.check Cannot create a model with undefined observations")
- }
- protected class Pass(lambda: HMMLambda, obs: Array[Int]) extends HMMModel(lambda, obs) {
- protected var alphaBeta: Matrix[Double] = _
- protected val ct = Array.fill(lambda.getT)(0.0)
- /**
- * Compute and apply the normalization factor ct for the computation of Alpha
- * [Formula M3] and Beta probabilities [Formula M7] for the observation at index t
- * @param t Index of the observation.
- */
- protected def normalize(t: Int): Unit = {
- import HMMConfig._
- require(t >= 0 && t < lambda.getT, s"HMMModel.normalize Incorrect observation index t= $t")
-
- ct.update(t, foldLeft(lambda.getN, (s, n) => s + alphaBeta(t, n)))
- alphaBeta /= (t, ct(t))
- }
- def getAlphaBeta: Matrix[Double] = alphaBeta
- }
- @implicitNotFound("HMM Conversion from DblVector to type T undefined")
- final protected class HMM[@specialized T <% Array[Int]](
- lambda: HMMLambda,
- form: HMMForm,
- maxIters: Int)
- (implicit f: DblVector => T) extends PipeOperator[DblVector, HMMPredictor] {
-
- check(maxIters)
-
- private val logger = Logger.getLogger("HMM")
- private[this] val state = HMMState(lambda, maxIters)
-
- override def |> : PartialFunction[DblVector, HMMPredictor] = {
- case obs: DblVector if( !obs.isEmpty) => {
- Try {
- form match {
- case EVALUATION => evaluate(obs)
- case DECODING => decode(obs)
- }
- } match {
- case Success(prediction) => prediction
- case Failure(e) =>
- DisplayUtils.error("HMM.|> ", logger, e)
- nullHMMPredictor
- }
- }
- }
-
- def decode(obs: T): HMMPredictor = (ViterbiPath(lambda, obs).maxDelta, state.QStar())
-
- def evaluate(obs: T): HMMPredictor = (-Alpha(lambda, obs).logProb, obs)
-
- final def getModel: HMMLambda = lambda
- }
- object HMM {
- type HMMPredictor = (Double, Array[Int])
- val nullHMMPredictor = (-1.0, Array.empty[Int])
-
- def apply[T <% Array[Int]](
- lambda: HMMLambda,
- form: HMMForm,
- maxIters: Int)
- (implicit f: DblVector => T): HMM[T] = new HMM[T](lambda, form, maxIters)
- def apply[T <% Array[Int]](
- lambda: HMMLambda,
- form: HMMForm)
- (implicit f: DblVector => T): HMM[T] = new HMM[T](lambda, form, HMMState.DEFAULT_MAXITERS)
-
-
- def apply[T <% Array[Int]](
- config: HMMConfig,
- obsIndx: Array[Int],
- form: HMMForm,
- maxIters: Int,
- eps: Double)
- (implicit f: DblVector => T): Option[HMM[T]] = {
-
- val baumWelchEM = new BaumWelchEM(config, obsIndx, maxIters, eps)
- baumWelchEM.maxLikelihood.map(_ => new HMM[T](baumWelchEM.lambda, form, maxIters))
- }
-
- val MAX_NUM_ITERS = 1024
- private def check(maxIters: Int): Unit = {
- require(maxIters > 1 && maxIters < MAX_NUM_ITERS,
- s"HMM.check Maximum number of iterations to train a HMM $maxIters is out of bounds")
- }
- }
- // ---------------------------------------- EOF ------------------------------------------------------------
复制代码
|
|