Question on Debugging a code line

S

subhabangalore

Dear Room,

I was trying to go through a code given in http://en.wikipedia.org/wiki/Forward–backward_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.
'Healthy' : {'Healthy': 0.69, 'Fever': 0.3, 'E': 0.01},
'Fever' : {'Healthy': 0.4, 'Fever': 0.59, 'E': 0.01},
}'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.
 
M

MRAB

Dear Room,

I was trying to go through a code given in http://en.wikipedia.org/wiki/Forward–backward_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.
'Healthy' : {'Healthy': 0.69, 'Fever': 0.3, 'E': 0.01},
'Fever' : {'Healthy': 0.4, 'Fever': 0.59, 'E': 0.01},
}'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.
 
S

subhabangalore

Dear Room,



I was trying to go through a code given in http://en.wikipedia.org/wiki/Forward–backward_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.



'Healthy' : {'Healthy': 0.69, 'Fever': 0.3, 'E': 0.01},

'Fever' : {'Healthy': 0.4, 'Fever': 0.59, 'E': 0.01},

}

'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.
 
S

subhabangalore

Dear Room,
I was trying to go through a code given in http://en.wikipedia.org/wiki/Forward–backward_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]

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.
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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top