instantiate new objects

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Felix Steffenhagen

    #1

    instantiate new objects

    Hello @ all,

    i'm a newbie in python and have written a module for computations in a
    bayesian network.

    The module can be found at:


    In this module i define four classes.
    - cdp (conditional probability [distribution]) consisting of cdp_entry
    objects
    - graph ( a graph class )
    - bayesNet ( the bayesian network, a subclass of graph )


    My problem is the following:
    I have a global method test() in the module, where i want to test my
    implementation of the em-learning algorithm, for learning parameters in
    a bayesian network.
    When I import this module in the python environment and then run the
    test method, everything seems to be ok, and the calculations that are
    hardcoded in this method seems to be correct.
    In the test method i instantiate a bayesNet object for the further
    calculations.
    The results that are computed and printed are probability distributions
    in the bayesian network, but this is not important for the problem.
    The problem comes when i want to run the test method again for a second
    time. What happens is that the "same" expressions/computations listed
    in the test method leads to another results, even if the commands are
    the same.
    I can only imagine that this comes out of some objects that are not
    destroyed and influence the new bayesNet object that is created there.

    If there is a problem with the instantiations, it is in the test method
    which is at the end of the file bayes.py (see above)
    Does someone see the problem there???

    If you need some more information about what happens in the module
    please write me a mail, but i hope the comments are enough to understand
    the problem.

    If you think this is too much off-topic we can discuss the problem out
    of the newsgroup.

    thanks in advance,
    Felix Steffenhagen
  • Michael Spencer

    #2
    Re: instantiate new objects

    Felix Steffenhagen wrote:
    [snip][color=blue]
    > In:[/color]
    [color=blue]
    > http://www.informatik.uni-freiburg.d...ffenh/bayes.py[/color]
    [color=blue]
    > [bayes.test gives different results each time it is called][/color]

    Without looking in the slightest at what you are implementing or how, this
    implies that state is maintained between calls to test

    The question is where/how is the state maintained?

    1) global module variables? - you don't have any
    2) attributes of global objects?

    I used:[color=blue][color=green][color=darkred]
    >>> def attrs(obj):[/color][/color][/color]
    ... return dict((name,geta ttr(obj,name)) for name in dir(obj) if not
    callable(getatt r(obj, name)) and name not in ("__doc__","__m odule__"))

    to verify that the classes are not gaining (non-callable) attributes

    3) as mutable default parameters?

    See:
    line 135: def __init__(self,V ,E,p=[]):
    line 150: def generate_cpd(se lf,node, rest, values={}):
    line 360: def computeJPD(self , rest, values={}):

    I guess one (or more) of these is the culprit

    Before running:

    Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32
    Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
    >>> bayesNet.genera te_cpd.func_def aults[/color][/color][/color]
    ({},)[color=blue][color=green][color=darkred]
    >>> bayesNet.__init __.func_default s[/color][/color][/color]
    ([],)[color=blue][color=green][color=darkred]
    >>> bayesNet.comput eJPD.func_defau lts[/color][/color][/color]
    ({},)[color=blue][color=green][color=darkred]
    >>> test()[/color][/color][/color]
    V = {'a': [0, 1], 'b': [0, 1]}
    [snip results]

    After test:
    [color=blue][color=green][color=darkred]
    >>> bayesNet.genera te_cpd.func_def aults[/color][/color][/color]
    ({'a': 1, 'b': 1},)[color=blue][color=green][color=darkred]
    >>> bayesNet.__init __.func_default s[/color][/color][/color]
    ([],)[color=blue][color=green][color=darkred]
    >>> bayesNet.comput eJPD.func_defau lts[/color][/color][/color]
    ({'a': 1, 'b': 1},)[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    HTH

    Michael


    Comment

    • Felix Steffenhagen

      #3
      Re: instantiate new objects

      The default mutual parameters in the method bayes.generate_ cpd(...)
      was the problem, thanks alot for the hint and for this code snippet
      to find such problems :-).

      Greetings,
      Felix

      Michael Spencer wrote:
      [color=blue]
      > Without looking in the slightest at what you are implementing or how,
      > this implies that state is maintained between calls to test
      >
      > The question is where/how is the state maintained?
      >.
      >.
      >.
      > 3) as mutable default parameters?
      >
      > See:
      > line 135: def __init__(self,V ,E,p=[]):
      > line 150: def generate_cpd(se lf,node, rest, values={}):
      > line 360: def computeJPD(self , rest, values={}):
      >
      > I guess one (or more) of these is the culprit
      >
      > Before running:
      >
      > Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on
      > win32
      > Type "help", "copyright" , "credits" or "license" for more information.[color=green][color=darkred]
      > >>> bayesNet.genera te_cpd.func_def aults[/color][/color]
      > ({},)[color=green][color=darkred]
      > >>> bayesNet.__init __.func_default s[/color][/color]
      > ([],)[color=green][color=darkred]
      > >>> bayesNet.comput eJPD.func_defau lts[/color][/color]
      > ({},)[color=green][color=darkred]
      > >>> test()[/color][/color]
      > V = {'a': [0, 1], 'b': [0, 1]}
      > [snip results]
      >
      > After test:
      >[color=green][color=darkred]
      > >>> bayesNet.genera te_cpd.func_def aults[/color][/color]
      > ({'a': 1, 'b': 1},)[color=green][color=darkred]
      > >>> bayesNet.__init __.func_default s[/color][/color]
      > ([],)[color=green][color=darkred]
      > >>> bayesNet.comput eJPD.func_defau lts[/color][/color]
      > ({'a': 1, 'b': 1},)[color=green][color=darkred]
      > >>>[/color][/color]
      >
      > HTH
      >
      > Michael
      >
      >[/color]

      Comment

      Working...