Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #71266 > unrolled thread

Question on Debugging a code line

Started bysubhabangalore@gmail.com
First post2014-05-10 12:27 -0700
Last post2014-05-11 13:03 +0100
Articles 5 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Question on Debugging a code line subhabangalore@gmail.com - 2014-05-10 12:27 -0700
    Re: Question on Debugging a code line MRAB <python@mrabarnett.plus.com> - 2014-05-10 21:44 +0100
    Re: Question on Debugging a code line subhabangalore@gmail.com - 2014-05-10 23:20 -0700
      Re: Question on Debugging a code line subhabangalore@gmail.com - 2014-05-11 00:45 -0700
        Re: Question on Debugging a code line Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-05-11 13:03 +0100

#71266 — Question on Debugging a code line

Fromsubhabangalore@gmail.com
Date2014-05-10 12:27 -0700
SubjectQuestion on Debugging a code line
Message-ID<283e285b-4ab3-4ec7-a8be-4f1e047d9645@googlegroups.com>
Dear Room,

I was trying to go through a code given in http://en.wikipedia.org/wiki/Forward%E2%80%93backward_algorithm[ Forward Backward is an algorithm of Machine Learning-I am not talking on that
I am just trying to figure out a query on its Python coding.]

I came across the following codes.

>>> states = ('Healthy', 'Fever')
>>> end_state = 'E'
>>> observations = ('normal', 'cold', 'dizzy')
>>> start_probability = {'Healthy': 0.6, 'Fever': 0.4}
>>> transition_probability = {
   'Healthy' : {'Healthy': 0.69, 'Fever': 0.3, 'E': 0.01},
   'Fever' : {'Healthy': 0.4, 'Fever': 0.59, 'E': 0.01},
   }
>>> emission_probability = {
   'Healthy' : {'normal': 0.5, 'cold': 0.4, 'dizzy': 0.1},
   'Fever' : {'normal': 0.1, 'cold': 0.3, 'dizzy': 0.6},
   }

def fwd_bkw(x, states, a_0, a, e, end_st):
    L = len(x)
    fwd = []
    f_prev = {} #THE PROBLEM 
    # forward part of the algorithm
    for i, x_i in enumerate(x):
        f_curr = {}
        for st in states:
            if i == 0:
                # base case for the forward part
                prev_f_sum = a_0[st]
            else:
                prev_f_sum = sum(f_prev[k]*a[k][st] for k in states) ##
 
            f_curr[st] = e[st][x_i] * prev_f_sum
 
        fwd.append(f_curr)
        f_prev = f_curr
 
    p_fwd = sum(f_curr[k]*a[k][end_st] for k in states)

As this value was being called in prev_f_sum = sum(f_prev[k]*a[k][st] for k in states marked ## 
I wanted to know what values it is generating.
So, I had made the following experiment, after 
for i, x_i in enumerate(x): 
I had put print f_prev 
but I am not getting how f_prev is getting the values.

Here, 
x=observations,
states= states,
a_0=start_probability,
a= transition_probability,
e=emission_probability,
end_st= end_state

Am I missing any minor aspect?
Code is running fine. 

If any one of the esteemed members may kindly guide me.

Regards,
Subhabrata Banerjee. 



 
   

[toc] | [next] | [standalone]


#71274

FromMRAB <python@mrabarnett.plus.com>
Date2014-05-10 21:44 +0100
Message-ID<mailman.9866.1399754708.18130.python-list@python.org>
In reply to#71266
On 2014-05-10 20:27, subhabangalore@gmail.com wrote:
> Dear Room,
>
> I was trying to go through a code given in http://en.wikipedia.org/wiki/Forward%E2%80%93backward_algorithm[ Forward Backward is an algorithm of Machine Learning-I am not talking on that
> I am just trying to figure out a query on its Python coding.]
>
> I came across the following codes.
>
>>>> states = ('Healthy', 'Fever')
>>>> end_state = 'E'
>>>> observations = ('normal', 'cold', 'dizzy')
>>>> start_probability = {'Healthy': 0.6, 'Fever': 0.4}
>>>> transition_probability = {
>     'Healthy' : {'Healthy': 0.69, 'Fever': 0.3, 'E': 0.01},
>     'Fever' : {'Healthy': 0.4, 'Fever': 0.59, 'E': 0.01},
>     }
>>>> emission_probability = {
>     'Healthy' : {'normal': 0.5, 'cold': 0.4, 'dizzy': 0.1},
>     'Fever' : {'normal': 0.1, 'cold': 0.3, 'dizzy': 0.6},
>     }
>
> def fwd_bkw(x, states, a_0, a, e, end_st):
>      L = len(x)
>      fwd = []
>      f_prev = {} #THE PROBLEM
>      # forward part of the algorithm
>      for i, x_i in enumerate(x):
>          f_curr = {}
>          for st in states:
>              if i == 0:
>                  # base case for the forward part
>                  prev_f_sum = a_0[st]
>              else:
>                  prev_f_sum = sum(f_prev[k]*a[k][st] for k in states) ##
>
>              f_curr[st] = e[st][x_i] * prev_f_sum
>
>          fwd.append(f_curr)
>          f_prev = f_curr
>
>      p_fwd = sum(f_curr[k]*a[k][end_st] for k in states)
>
> As this value was being called in prev_f_sum = sum(f_prev[k]*a[k][st] for k in states marked ##
> I wanted to know what values it is generating.
> So, I had made the following experiment, after
> for i, x_i in enumerate(x):
> I had put print f_prev
> but I am not getting how f_prev is getting the values.
>
> Here,
> x=observations,
> states= states,
> a_0=start_probability,
> a= transition_probability,
> e=emission_probability,
> end_st= end_state
>
> Am I missing any minor aspect?
> Code is running fine.
>
> If any one of the esteemed members may kindly guide me.
>
The values calculated in the inner loop are being put into the dict 
'f_curr'
and then, when that loop has completed, 'f_prev' is being bound to that
dict.

'f_curr' is bound to a new dict just before the inner loop, ready for
the new values.

[toc] | [prev] | [next] | [standalone]


#71296

Fromsubhabangalore@gmail.com
Date2014-05-10 23:20 -0700
Message-ID<e50d8baa-5fd0-4ca7-af59-2f9f66568596@googlegroups.com>
In reply to#71266
On Sunday, May 11, 2014 12:57:34 AM UTC+5:30, subhaba...@gmail.com wrote:
> Dear Room,
> 
> 
> 
> I was trying to go through a code given in http://en.wikipedia.org/wiki/Forward%E2%80%93backward_algorithm[ Forward Backward is an algorithm of Machine Learning-I am not talking on that
> 
> I am just trying to figure out a query on its Python coding.]
> 
> 
> 
> I came across the following codes.
> 
> 
> 
> >>> states = ('Healthy', 'Fever')
> 
> >>> end_state = 'E'
> 
> >>> observations = ('normal', 'cold', 'dizzy')
> 
> >>> start_probability = {'Healthy': 0.6, 'Fever': 0.4}
> 
> >>> transition_probability = {
> 
>    'Healthy' : {'Healthy': 0.69, 'Fever': 0.3, 'E': 0.01},
> 
>    'Fever' : {'Healthy': 0.4, 'Fever': 0.59, 'E': 0.01},
> 
>    }
> 
> >>> emission_probability = {
> 
>    'Healthy' : {'normal': 0.5, 'cold': 0.4, 'dizzy': 0.1},
> 
>    'Fever' : {'normal': 0.1, 'cold': 0.3, 'dizzy': 0.6},
> 
>    }
> 
> 
> 
> def fwd_bkw(x, states, a_0, a, e, end_st):
> 
>     L = len(x)
> 
>     fwd = []
> 
>     f_prev = {} #THE PROBLEM 
> 
>     # forward part of the algorithm
> 
>     for i, x_i in enumerate(x):
> 
>         f_curr = {}
> 
>         for st in states:
> 
>             if i == 0:
> 
>                 # base case for the forward part
> 
>                 prev_f_sum = a_0[st]
> 
>             else:
> 
>                 prev_f_sum = sum(f_prev[k]*a[k][st] for k in states) ##
> 
>  
> 
>             f_curr[st] = e[st][x_i] * prev_f_sum
> 
>  
> 
>         fwd.append(f_curr)
> 
>         f_prev = f_curr
> 
>  
> 
>     p_fwd = sum(f_curr[k]*a[k][end_st] for k in states)
> 
> 
> 
> As this value was being called in prev_f_sum = sum(f_prev[k]*a[k][st] for k in states marked ## 
> 
> I wanted to know what values it is generating.
> 
> So, I had made the following experiment, after 
> 
> for i, x_i in enumerate(x): 
> 
> I had put print f_prev 
> 
> but I am not getting how f_prev is getting the values.
> 
> 
> 
> Here, 
> 
> x=observations,
> 
> states= states,
> 
> a_0=start_probability,
> 
> a= transition_probability,
> 
> e=emission_probability,
> 
> end_st= end_state
> 
> 
> 
> Am I missing any minor aspect?
> 
> Code is running fine. 
> 
> 
> 
> If any one of the esteemed members may kindly guide me.
> 
> 
> 
> Regards,
> 
> Subhabrata Banerjee.

Dear Sir,
Thank you for your kind reply. I will check. 
Regards,
Subhabrata Banerjee. 

[toc] | [prev] | [next] | [standalone]


#71307

Fromsubhabangalore@gmail.com
Date2014-05-11 00:45 -0700
Message-ID<d4312400-92df-4611-b7ea-2fcc5890d6fa@googlegroups.com>
In reply to#71296
On Sunday, May 11, 2014 11:50:32 AM UTC+5:30, subhaba...@gmail.com wrote:
> On Sunday, May 11, 2014 12:57:34 AM UTC+5:30, subhaba...@gmail.com wrote:
> 
> > Dear Room,
> 
> > 
> 
> > 
> 
> > 
> 
> > I was trying to go through a code given in http://en.wikipedia.org/wiki/Forward%E2%80%93backward_algorithm[ Forward Backward is an algorithm of Machine Learning-I am not talking on that
> 
> > 
> 
> > I am just trying to figure out a query on its Python coding.]
> 
> > 
> 
> > 
> 
> > 
> 
> > I came across the following codes.
> 
> > 
> 
> > 
> 
> > 
> 
> > >>> states = ('Healthy', 'Fever')
> 
> > 
> 
> > >>> end_state = 'E'
> 
> > 
> 
> > >>> observations = ('normal', 'cold', 'dizzy')
> 
> > 
> 
> > >>> start_probability = {'Healthy': 0.6, 'Fever': 0.4}
> 
> > 
> 
> > >>> transition_probability = {
> 
> > 
> 
> >    'Healthy' : {'Healthy': 0.69, 'Fever': 0.3, 'E': 0.01},
> 
> > 
> 
> >    'Fever' : {'Healthy': 0.4, 'Fever': 0.59, 'E': 0.01},
> 
> > 
> 
> >    }
> 
> > 
> 
> > >>> emission_probability = {
> 
> > 
> 
> >    'Healthy' : {'normal': 0.5, 'cold': 0.4, 'dizzy': 0.1},
> 
> > 
> 
> >    'Fever' : {'normal': 0.1, 'cold': 0.3, 'dizzy': 0.6},
> 
> > 
> 
> >    }
> 
> > 
> 
> > 
> 
> > 
> 
> > def fwd_bkw(x, states, a_0, a, e, end_st):
> 
> > 
> 
> >     L = len(x)
> 
> > 
> 
> >     fwd = []
> 
> > 
> 
> >     f_prev = {} #THE PROBLEM 
> 
> > 
> 
> >     # forward part of the algorithm
> 
> > 
> 
> >     for i, x_i in enumerate(x):
> 
> > 
> 
> >         f_curr = {}
> 
> > 
> 
> >         for st in states:
> 
> > 
> 
> >             if i == 0:
> 
> > 
> 
> >                 # base case for the forward part
> 
> > 
> 
> >                 prev_f_sum = a_0[st]
> 
> > 
> 
> >             else:
> 
> > 
> 
> >                 prev_f_sum = sum(f_prev[k]*a[k][st] for k in states) ##
> 
> > 
> 
> >  
> 
> > 
> 
> >             f_curr[st] = e[st][x_i] * prev_f_sum
> 
> > 
> 
> >  
> 
> > 
> 
> >         fwd.append(f_curr)
> 
> > 
> 
> >         f_prev = f_curr
> 
> > 
> 
> >  
> 
> > 
> 
> >     p_fwd = sum(f_curr[k]*a[k][end_st] for k in states)
> 
> > 
> 
> > 
> 
> > 
> 
> > As this value was being called in prev_f_sum = sum(f_prev[k]*a[k][st] for k in states marked ## 
> 
> > 
> 
> > I wanted to know what values it is generating.
> 
> > 
> 
> > So, I had made the following experiment, after 
> 
> > 
> 
> > for i, x_i in enumerate(x): 
> 
> > 
> 
> > I had put print f_prev 
> 
> > 
> 
> > but I am not getting how f_prev is getting the values.
> 
> > 
> 
> > 
> 
> > 
> 
> > Here, 
> 
> > 
> 
> > x=observations,
> 
> > 
> 
> > states= states,
> 
> > 
> 
> > a_0=start_probability,
> 
> > 
> 
> > a= transition_probability,
> 
> > 
> 
> > e=emission_probability,
> 
> > 
> 
> > end_st= end_state
> 
> > 
> 
> > 
> 
> > 
> 
> > Am I missing any minor aspect?
> 
> > 
> 
> > Code is running fine. 
> 
> > 
> 
> > 
> 
> > 
> 
> > If any one of the esteemed members may kindly guide me.
> 
> > 
> 
> > 
> 
> > 
> 
> > Regards,
> 
> > 
> 
> > Subhabrata Banerjee.
> 
> 
> 
> Dear Sir,
> 
> Thank you for your kind reply. I will check. 
> 
> Regards,
> 
> Subhabrata Banerjee.

Dear Sir,
Thank you. It worked. I made another similar statement over another set of values on your reply it went nice.
Regards,
Subhabrata Banerjee.

[toc] | [prev] | [next] | [standalone]


#71312

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2014-05-11 13:03 +0100
Message-ID<mailman.9882.1399809799.18130.python-list@python.org>
In reply to#71307
On 11/05/2014 08:45, subhabangalore@gmail.com wrote:

[268 lines snipped]

Would you please use the mailing list 
https://mail.python.org/mailman/listinfo/python-list or read and action 
this https://wiki.python.org/moin/GoogleGroupsPython to prevent us 
seeing double line spacing and single line paragraphs, thanks.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web