hmm

package
v0.0.0-...-0953495 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 23, 2015 License: BSD-3-Clause Imports: 15 Imported by: 0

Documentation

Overview

Package hmm provides an implementation of hidden Markov models. It is designed for applications in temporal pattern recognition.

The package can support any output distribution of type model.Modeler.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MakeLeftToRight

func MakeLeftToRight(ns int, selfProb, skipProb float64) *narray.NArray

MakeLeftToRight creates a transition probability matrix for a left-to-right HMM.

  • ns is the total number of states including entry/exit. Must be 3 or greater.
  • selfProb is the prob of the self loop with value between 0 and 1.
  • skipProb is the prob of skipping next state. Make it zero for no skips.
  • for ns=3, skipProb is set to zero.

Types

type Assigner

type Assigner interface {
	Assign(labels []string) (modelNames []string)
}

Assigner assigns a sequence of hmm model names to a sequence of labels. This interface hides the details of how the assignment is done. For example, a trivial assigner may assign the label name to the model name:

Input labels:       []string{"a", "b", "c"}
Output model names: []string{"a", "b", "c"}

or it may use a dictionary as is the case in speech recognition:

Input labels:       []string{"HELLO","WORLD"}
Output model names: []string{"HH","AH0","L","OW1","W","ER1","L","D"}

type DirectAssigner

type DirectAssigner struct{}

DirectAssigner implements the Assigner interface. Model names correspond one-to-one to the label names.

func (DirectAssigner) Assign

func (a DirectAssigner) Assign(labels []string) []string

Assign returns a sequence of model names.

type MapAssigner

type MapAssigner map[string][]string

MapAssigner implements the Assigner interface. Labes are mapped using a dictionary.

func (MapAssigner) Assign

func (a MapAssigner) Assign(labels []string) []string

Assign returns a sequence of model names.

type Model

type Model struct {
	// Model type.
	Type string `json:"type"`
	// Model name.
	ModelName string `json:"name"`
	// Model Set
	Set *Set `json:"hmm_set"`
	// contains filtered or unexported fields
}

Model is a hidden Markov model.

func NewModel

func NewModel(options ...Option) *Model

NewModel creates a new HMM.

func ReadJSON

func ReadJSON(r io.Reader) (*Model, error)

ReadJSON unmarshals json data from an io.Reader anc creates a new HMM model.

func ReadJSONFile

func ReadJSONFile(fn string) (*Model, error)

ReadJSONFile unmarshals json data from a file.

func (*Model) Clear

func (m *Model) Clear()

Clear accumulators.

func (*Model) Dim

func (m *Model) Dim() int

Dim is the dimensionality of the observation vector.

func (*Model) Estimate

func (m *Model) Estimate() error

Estimate will update HMM parameters from counts.

func (*Model) LogProb

func (m *Model) LogProb(observation interface{}) float64

LogProb returns the log probability.

func (*Model) Name

func (m *Model) Name() string

Name returns the name of the model.

func (*Model) Prob

func (m *Model) Prob(observation interface{}) float64

Prob returns the probability.

func (*Model) Random

func (m *Model) Random(r *rand.Rand) (interface{}, []int, error)

func (*Model) SetFlags

func (m *Model) SetFlags(useAlignments, updateTP, updateOP bool)

SetFlags sets various flags in the model.

func (*Model) String

func (m *Model) String() string

String prints the model.

func (*Model) ToJSON

func (m *Model) ToJSON() (string, error)

ToJSON returns a json string.

func (*Model) Update

func (m *Model) Update(x model.Observer, w func(model.Obs) float64) error

Update updates sufficient statistics using an observation stream.

func (*Model) UpdateOne

func (m *Model) UpdateOne(o model.Obs, w float64)

UpdateOne updates model using a single weighted sample.

func (*Model) WriteJSON

func (m *Model) WriteJSON(w io.Writer) error

WriteJSON writes HMM model to an io.Writer.

func (*Model) WriteJSONFile

func (m *Model) WriteJSONFile(fn string) error

WriteJSONFile writes to a file.

type Net

type Net struct {
	// Model name.
	Name string `json:"name"`

	// State transition probabilities. (ns x ns matrix)
	A *narray.NArray `json:"trans_prob"`
	// Output probabilities. (ns x 1 vector)
	B []model.Modeler `json:"output_prob"`
	// Accumulator for transition probabilities.
	TrAcc *narray.NArray `json:"tr_acc,omitempty"`
	// Accumulator for global occupation counts.
	OccAcc *narray.NArray `json:"occ_acc,omitempty"`
	// contains filtered or unexported fields
}

Net is an hmm network with a single non-emmiting entry state (index 0) and a single non-emmiting exit state (index ns-1) where ns is the total number of states. The HMMs must have a left-to-right topology. That is, transitions can only go from state i to state j where j >= i.

func (*Net) Graph

func (m *Net) Graph() *graph.Graph

Graph converts an HMM Net object to a graph where states are nodes. Like an HMM net, the graph contains a single entry and exit non-emmiting state.

type ObsSlice

type ObsSlice []model.Modeler

ObsSlice type is a slice of modelers.

type Option

type Option func(*Model)

Option type is used to pass options to NewModel().

func MaxGenLen

func MaxGenLen(n int) Option

MaxGenLen option sets the length of the sequences created by the MODEL generator. Default is 100.

func Name

func Name(name string) Option

Name is an option to set the model name.

func OAssign

func OAssign(assigner Assigner) Option

OAssign is an option to set the label to model assigner.

func OSet

func OSet(set *Set) Option

OSet is an option to set the collection of HMM networks.

func UpdateOP

func UpdateOP(flag bool) Option

UpdateOP option to update output PDF. Default is true.

func UpdateTP

func UpdateTP(flag bool) Option

UpdateTP option to update state transition probabilities. Default is true.

func UseAlignments

func UseAlignments(flag bool) Option

UseAlignments option. When true, the output probability densities are estimated using alignment info. The alignment info must be available in the obs object which shoudl implement the model.ALigner interface. Will panic if the alignment data is missing.

type Set

type Set struct {
	Nets []*Net `json:"networks"`
	// contains filtered or unexported fields
}

Set is a collection of unique HMMs.

func NewSet

func NewSet(nets ...*Net) (*Set, error)

NewSet creates a new set of hmms.

func (*Set) NewNet

func (ms *Set) NewNet(name string, a *narray.NArray, b []model.Modeler) (*Net, error)

NewNet creates a new HMM network.

func (*Set) SearchGraph

func (set *Set) SearchGraph() *graph.Graph

SearchGraph creates a graph based on a set of HMM networks.

func (*Set) String

func (ms *Set) String() string

String prints the model set.

func (*Set) ToJSON

func (ms *Set) ToJSON() (string, error)

ToJSON returns a json string.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL