My logp function depends on comparison of model output to observed data. ( r==rs in the following function)
def logp(instances,h_epsilon,learning_iter,sub_path_length):
max_trials=20
lp=0
trials=1;
for i,sr in zip(instances,sub_path_length):
m_path_f = make_model_path_length(i)
while True:
r=m_path_f(h_epsilon,learning_iter)
print 'Model:{} Subject:{}'.format(r,sr)
if r == sr or trials > max_trials:
break
trials+=1
sample_data[(i.name,str(h_epsilon),str(learning_iter))]=trials
lp+=sum([(1./t) for t in range(1,trials+1)])
trials=1
return -lp
The values of s and sr are Theano values and Iâm not sure that this is how I am supposed to compare them.
What is the right way to do it?
Here is the rest of my model:
class IVS(Discrete):
def __init__(self,instances,h_epsilon,learning_iter, *args, **kwargs):
super(IVS, self).__init__(*args, **kwargs)#What does discrete expects?
self.instances = instances
self.h_epsilon = h_epsilon
self.learning_iter = learning_iter
def logp(self, value):
return logp(self.instances,self.h_epsilon,self.learning_iter,value)
def get_instances_by_subject(path_file,subject):
with open(path_file,'rb') as f:
reader=DictReader(f)
data=[d['instance'] for d in reader if d['subject']==subject and d['complete']=='True']
return sorted(data)
def get_paths_by_subject(path_file,instance_names,subject,fun):
with open(path_file,'rb') as f:
reader=DictReader(f)
data=[(d['instance'],fun(d)) for d in reader if d['subject']==subject and d['instance'] in instance_names]
return [fr for (x,fr) in sorted(data, key=lambda x: instance_names.index(x[0]))]
d=get_paths_by_subject(path_file,get_instances_by_subject(path_file,subject),subject,lambda x:float(x['human_length']))
sample_data={}
def make_model_path_length(i):
@as_op(itypes=[tt.dscalar,tt.lscalar], otypes=[tt.lscalar])
def model_path_length(h_epsilon,learning_iter):
return len(LRTA(i,heur=lambda x: (1+h_epsilon)*min_manhattan_distance(x),update_h=True,iters=learning_iter)[0])
return model_path_length
lrta_model=Model()
with lrta_model:
learning_iter_=DiscreteUniform('learning_iter_',lower=1, upper=5)
learning_iter=Deterministic('learning_iter',learning_iter_)
h_epsilon_ = Uniform('h_epsilon_',lower=0.,upper=1.)
h_epsilon = Deterministic('h_epsilon',h_epsilon_)
instances=np.array([i for i in instance_set if i.name in get_instances_by_subject(path_file,subject)]) #is this sorted properly?
path_length=IVS('path_length',instances,h_epsilon,learning_iter, observed=d)