jmschrei / pomegranate

Fast, flexible and easy to use probabilistic modelling in Python.
http://pomegranate.readthedocs.org/en/latest/
MIT License
3.29k stars 591 forks source link

The transition matrix doesn't seem to get updated when HHM is initialized with model name, start state and end state. #998

Closed li-xie closed 1 year ago

li-xie commented 1 year ago

To Reproduce Windows 10 python 3.10.2 numpy 1.23.3 scipy 1.9.1 networkx 2.8.7 joblib 1.2.0 pomegranate 0.14.8 When I run an example from the tutorial as below:

from pomegranate import *

s1 = State( NormalDistribution( 3, 1 ), name="s1" )
s2 = State( NormalDistribution( 6, 2 ), name="s2" )

model = HiddenMarkovModel('test', s2, s1)
model.add_states( [s1, s2] )
model.add_transition( s1, s2, 0.2 )
model.add_transition( s2, s1, 0.5 )
model.add_transition( s1, s1, 0.8 )
model.add_transition( s2, s2, 0.5 )
model.bake()
model.fit( [[5, 2, 3, 4, 7, 3, 6, 3, 5, 2, 4], [5, 7, 2, 3, 5, 1, 3, 5, 6, 2]])
tm = model.dense_transition_matrix()
print(f'transition matrix after fitting is: [[{tm[0,0]}, {tm[0,1]}],[{tm[1,0]},{tm[1,1]}]]')

The output is: transition matrix after fitting is: [[0.8, 0.2],[0.5,0.5]] The transition matrix doesn't seem to be updated. However, if I omit the model name during initialization and use model = HiddenMarkovModel(s2, s1) , the transition matrix gets updated. Did I misunderstand anything? Can anyone try to see if this behavior can be duplicated? Thanks in advance!

smalik89 commented 1 year ago

Good question! I also had a similar issue- how do you view the original transition matrix vs. the final transition matrix? Sometimes model.dense_transition_matrix() outputs one or the other. For example here is the issue when we integrate a 3rd state and simply want to view the transition matrix:

    # trying to output transition matrix
    s1 = State(NormalDistribution(3, 1), name="s1")
    s2 = State(NormalDistribution(6, 2), name="s2")
    s3 = State(NormalDistribution(6, 2), name="s3")
    model = HiddenMarkovModel('test', s1, s2, s3)
    model.add_states([s1, s2])
    model.add_transition(s1, s2, 0.2)
    model.add_transition(s2, s1, 0.5)
    model.add_transition(s1, s1, 0.8)
    model.add_transition(s2, s2, 0.5)
    model.add_transition(s2, s3, 0.5)
    model.bake()
    tm = model.dense_transition_matrix()
    model.fit([[5, 2, 3, 4, 7, 3, 6, 3, 5, 2, 4], [5, 7, 2, 3, 5, 1, 3, 5, 6, 2]])
    print(tm)
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-26-39283a627f8e>", line 5, in <module>
    model = HiddenMarkovModel('test', s1, s2, s3)
  File "pomegranate/hmm.pyx", line 197, in pomegranate.hmm.HiddenMarkovModel.__init__
TypeError: __init__() takes at most 3 positional arguments (4 given)
jmschrei commented 1 year ago

You are not initializing the HMM correctly. Please see the tutorials for advice.

smalik89 commented 1 year ago

Thank you @jmschrei! what am I missing? Or is there a tutorial you can point me to?

jmschrei commented 1 year ago

Please see the tutorial folder in the repo.

jmschrei commented 1 year ago

Thank you for opening an issue. pomegranate has recently been rewritten from the ground up to use PyTorch instead of Cython (v1.0.0), and so all issues are being closed as they are likely out of date. Please re-open or start a new issue if a related issue is still present in the new codebase.