Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File "pomegranate/hmm.pyx", line 1047, in pomegranate.hmm.HiddenMarkovModel.bake UnboundLocalError: local variable 'dist' referenced before assignment #1005

Closed
smalik89 opened this issue Nov 12, 2022 · 4 comments

Comments

@smalik89
Copy link

smalik89 commented Nov 12, 2022

Describe the bug
Trying to do a supervised training with pomegranate.hmm.HiddenMarkovModel. Expecting to train the model and test on observations. However this is the err even though not using dist:

Pomegranate Version: 0.14.4:

    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-8-391a7a4c3790>", line 2, in <module>
    model.bake()
  File "pomegranate/hmm.pyx", line 1047, in pomegranate.hmm.HiddenMarkovModel.bake
UnboundLocalError: local variable 'dist' referenced before assignment

To Reproduce

    model = HiddenMarkovModel()
    model.bake() # error here
    model.fit([3.2, 6.7, 10.55, 19.55], labels=[1, 2, 3, 4], algorithm='labeled') 
    all_pred = model.predict([2.33, 1.22, 1.4, 10.6])

Thank you so much for your thoughts on this.

@smalik89 smalik89 changed the title [BUG] model.bake() File "pomegranate/hmm.pyx", line 1047, in pomegranate.hmm.HiddenMarkovModel.bake UnboundLocalError: local variable 'dist' referenced before assignment File "pomegranate/hmm.pyx", line 1047, in pomegranate.hmm.HiddenMarkovModel.bake UnboundLocalError: local variable 'dist' referenced before assignment Nov 12, 2022
@jmschrei
Copy link
Owner

You need to either initialize the model with a structure by adding edges and nodes using the add_edge and add_nodes method, the from_matrix method, or you need to use the from_samples method to learn a dense HMM directly from data. Using the fit method only updates parameters given a structure, but you haven't given it a structure.

@smalik89
Copy link
Author

smalik89 commented Nov 22, 2022

Hi @jmschrei ! I used the from_samples method however, still getting an error with 'dist'. I used the documentation to set up a supervised prediction however still get an issue. Can you point me in the right direction or any recommendations?

    # Supervised method that calculates the transition matrix:
    d1 = State(UniformDistribution.from_samples([3.243221498397177, 3.210684537495482, 3.227662201472816,
        3.286410817416738, 3.290573650708864, 3.286058136226862, 3.266480693857006]))
    d2 = State(UniformDistribution.from_samples([3.449282367485096, 1.97317859465635, 1.897551432353011,
         3.454609351559659, 3.127357456033111, 1.779308337786426, 3.802891929694426, 3.359766157565077, 2.959428499979418]))
    d3 = State(UniformDistribution.from_samples([1.892812118441474, 1.589353118681066, 2.09269978285637,
         2.104391496570218, 1.656771181054144]))
    model = HiddenMarkovModel()
    model.add_states(d1, d2, d3)
    model.bake()
    model.fit([3.2, 6.7, 10.55], labels=[1, 2, 3], algorithm='labeled')
    all_pred = model.predict([2.33, 1.22, 1.4, 10.6])

Issue:

    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-8-760ac26056e1>", line 9, in <module>
    model.bake()
  File "pomegranate/hmm.pyx", line 1047, in pomegranate.hmm.HiddenMarkovModel.bake
UnboundLocalError: local variable 'dist' referenced before assignment

Many Thanks for your help with this, really appreciate it!

@CristiFati
Copy link

CristiFati commented Dec 2, 2022

Not sure where to post this (as Discussions aren`t enabled in this repository), so here's (what I consider) the 2nd best place.

There's a big discrepancy between the versions published on [PyPI]: Pomegranate and the corresponding tags (which should indicate the source code states used to build those versions) in this repository, so when a problem arises (and it will definitely will, as there are some places in the code that don't handle "special" cases) it's hard to watch (and fix) such a problem.
Would it be possible to also list the latest tags (e.g v.14.*)?

@jmschrei
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants