Question on Debugging a code line

subhabangalore at gmail.com subhabangalore at gmail.com
Sun May 11 02:20:32 EDT 2014


On Sunday, May 11, 2014 12:57:34 AM UTC+5:30, subhaba... at 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. 



More information about the Python-list mailing list