Hello,
I am confused about why this happens when I try to call sample
with a step
argument -
a ValueError: expected an ndarray
is thrown.
Here is my model followed by the traceback.
from pymc3 import Model, DiscreteUniform, Uniform, Discrete, Deterministic, sample, loo, Metropolis
import theano.tensor as tt
import theano
from theano.compile.ops import as_op
from rushhour import min_manhattan_distance, opt_solution_instances
from astar import LRTA, eval_theano
import numpy as np
from test import instance_set
from csv import DictReader
from random import random
from sys import argv
theano.config.exception_verbosity='high'
theano.config.optimizer='fast_compile'
# Two parameters
try:
subject=argv[1]
path_file=argv[2]
except:
print 'Args: <subject> <path_file>'
class IVS(Discrete):
def __init__(self,h_epsilon,learning_iter,exp_select, *args, **kwargs):
super(IVS, self).__init__(*args, **kwargs)#What does discrete expects?
self.h_epsilon = h_epsilon
self.learning_iter = learning_iter
self.exp = exp_select
def logp(self, value):
return logp(self.h_epsilon,self.learning_iter,self.exp,value)
def get_instance_names_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 np.array([fr for (x,fr) in sorted(data, key=lambda x: instance_names.index(x[0]))])
def make_model_path_length(i):
def model_path_length(h_epsilon,learning_iter,exp):
path,_= LRTA(i,heur=lambda x: (1+h_epsilon)*min_manhattan_distance(x),update_h=True,iters=learning_iter,exp=exp)
return len(path)
return model_path_length
@as_op(itypes=[tt.dscalar,tt.lscalar,tt.dscalar,tt.lvector], otypes=[tt.dscalar])
def logp(h_epsilon,learning_iter,exp,sub_path_length):
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,exp)
if r == sr or trials > max_trials:
break
trials+=1
print '{0},{1},{2},{3},{4},{5}'.format(subject,i.name,h_epsilon,learning_iter,exp,trials)
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
lrta_model=Model()
with lrta_model as lrta_model:
max_trials=20
instance_names=get_instance_names_by_subject(path_file,subject)
instances=np.array([i for i in instance_set if i.name in instance_names]) #is this sorted properly?
d=get_paths_by_subject(path_file,instance_names,subject,lambda x:int(x['human_length']))
sample_data={}
learning_iter=DiscreteUniform('learning_iter',lower=1, upper=5)
exp_select = Uniform('exp', lower=4., upper=20.)
h_epsilon = Uniform('h_epsilon',lower=0.,upper=1.)
path_length=IVS('path_length',h_epsilon,learning_iter,exp_select, observed=d)
step1=Metropolis(vars=[h_epsilon])
step2=Metropolis(vars=[learning_iter])
step3=Metropolis(vars=[exp_select])
print '*************calling sample************'
trace = sample(2,step=[step1,step2,step3])
print loo(trace,pointwise=True)
And this is the traceback-
Traceback (most recent call last):
File "lrta_pymc3.py", line 85, in <module>
trace = sample(2,step=[step1,step2,step3])
File "/usr/local/lib/python2.7/site-packages/pymc3/sampling.py", line 278, in sample
return sample_func(**sample_args)[discard:]
File "/usr/local/lib/python2.7/site-packages/pymc3/sampling.py", line 293, in _sample
for it, strace in enumerate(sampling):
File "/usr/local/lib/python2.7/site-packages/tqdm/_tqdm.py", line 862, in __iter__
for obj in iterable:
File "/usr/local/lib/python2.7/site-packages/pymc3/sampling.py", line 391, in _iter_sample
point, states = step.step(point)
File "/usr/local/lib/python2.7/site-packages/pymc3/step_methods/compound.py", line 24, in step
point, state = method.step(point)
File "/usr/local/lib/python2.7/site-packages/pymc3/step_methods/arraystep.py", line 152, in step
apoint, stats = self.astep(bij.map(point))
File "/usr/local/lib/python2.7/site-packages/pymc3/step_methods/metropolis.py", line 152, in astep
accept = self.delta_logp(q, q0)
File "/usr/local/lib/python2.7/site-packages/theano/compile/function_module.py", line 898, in __call__
storage_map=getattr(self.fn, 'storage_map', None))
File "/usr/local/lib/python2.7/site-packages/theano/gof/link.py", line 325, in raise_with_op
reraise(exc_type, exc_value, exc_trace)
File "/usr/local/lib/python2.7/site-packages/theano/compile/function_module.py", line 884, in __call__
self.fn() if output_subset is None else\
ValueError: expected an ndarray
Apply node that caused the error: Elemwise{add,no_inplace}(Elemwise{switch,no_inplace}.0, Elemwise{add,no_inplace}.0, Elemwise{add,no_inplace}.0, FromFunctionOp{logp}.0)
Toposort index: 65
Inputs types: [TensorType(float64, scalar), TensorType(float64, scalar), TensorType(float64, scalar), TensorType(float64, scalar)]
Inputs shapes: [(), (), (), 'No shapes']
Inputs strides: [(), (), (), 'No strides']
Inputs values: [array(-1.6094379124341003), array(-1.3862943611198906), array(-1.8960286450343948), -4.019877344877345]
Inputs type_num: [12, 12, 12, '']
Outputs clients: [[Elemwise{sub,no_inplace}(Elemwise{add,no_inplace}.0, Elemwise{add,no_inplace}.0)]]
Backtrace when the node is created(use Theano flag traceback.limit=N to make it longer):
File "lrta_pymc3.py", line 81, in <module>
step1=Metropolis(vars=[h_epsilon])
File "/usr/local/lib/python2.7/site-packages/pymc3/step_methods/metropolis.py", line 126, in __init__
self.delta_logp = delta_logp(model.logpt, vars, shared)
File "/usr/local/lib/python2.7/site-packages/pymc3/memoize.py", line 16, in memoizer
cache[key] = obj(*args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/pymc3/model.py", line 447, in logpt
return tt.add(*map(tt.sum, factors))
Debugprint of the apply node:
Elemwise{add,no_inplace} [id A] <TensorType(float64, scalar)> ''
|Elemwise{switch,no_inplace} [id B] <TensorType(float64, scalar)> ''
| |Elemwise{mul,no_inplace} [id C] <TensorType(int8, scalar)> ''
| | |Elemwise{mul,no_inplace} [id D] <TensorType(int8, scalar)> ''
| | | |TensorConstant{1} [id E] <TensorType(int8, scalar)>
| | | |Elemwise{mul,no_inplace} [id F] <TensorType(int8, scalar)> ''
| | | |TensorConstant{1} [id E] <TensorType(int8, scalar)>
| | | |Elemwise{ge,no_inplace} [id G] <TensorType(bool, scalar)> ''
| | | |learning_iter_shared [id H] <TensorType(int64, scalar)>
| | | |TensorConstant{1} [id I] <TensorType(int32, scalar)>
| | |Elemwise{mul,no_inplace} [id J] <TensorType(int8, scalar)> ''
| | |TensorConstant{1} [id E] <TensorType(int8, scalar)>
| | |Elemwise{le,no_inplace} [id K] <TensorType(bool, scalar)> ''
| | |learning_iter_shared [id H] <TensorType(int64, scalar)>
| | |TensorConstant{5} [id L] <TensorType(int32, scalar)>
| |TensorConstant{-1.60943791243} [id M] <TensorType(float64, scalar)>
| |TensorConstant{-inf} [id N] <TensorType(float32, scalar)>
|Elemwise{add,no_inplace} [id O] <TensorType(float64, scalar)> ''
| |Elemwise{switch,no_inplace} [id P] <TensorType(float32, scalar)> ''
| | |Elemwise{mul,no_inplace} [id Q] <TensorType(int8, scalar)> ''
| | | |Elemwise{mul,no_inplace} [id R] <TensorType(int8, scalar)> ''
| | | | |TensorConstant{1} [id E] <TensorType(int8, scalar)>
| | | | |Elemwise{mul,no_inplace} [id S] <TensorType(int8, scalar)> ''
| | | | |TensorConstant{1} [id E] <TensorType(int8, scalar)>
| | | | |Elemwise{ge,no_inplace} [id T] <TensorType(bool, scalar)> ''
| | | | |Elemwise{add,no_inplace} [id U] <TensorType(float64, scalar)> ''
| | | | | |Elemwise{mul,no_inplace} [id V] <TensorType(float64, scalar)> ''
| | | | | | |TensorConstant{16.0} [id W] <TensorType(float32, scalar)>
| | | | | | |sigmoid [id X] <TensorType(float64, scalar)> ''
| | | | | | |exp_interval___shared [id Y] <TensorType(float64, scalar)>
| | | | | |TensorConstant{4.0} [id Z] <TensorType(float32, scalar)>
| | | | |TensorConstant{4.0} [id Z] <TensorType(float32, scalar)>
| | | |Elemwise{mul,no_inplace} [id BA] <TensorType(int8, scalar)> ''
| | | |TensorConstant{1} [id E] <TensorType(int8, scalar)>
| | | |Elemwise{le,no_inplace} [id BB] <TensorType(bool, scalar)> ''
| | | |Elemwise{add,no_inplace} [id U] <TensorType(float64, scalar)> ''
| | | |TensorConstant{20.0} [id BC] <TensorType(float32, scalar)>
| | |TensorConstant{-2.77258872986} [id BD] <TensorType(float32, scalar)>
| | |TensorConstant{-inf} [id N] <TensorType(float32, scalar)>
| |Elemwise{sub,no_inplace} [id BE] <TensorType(float64, scalar)> ''
| |Elemwise{sub,no_inplace} [id BF] <TensorType(float64, scalar)> ''
| | |TensorConstant{2.77258872986} [id BG] <TensorType(float32, scalar)>
| | |Elemwise{mul,no_inplace} [id BH] <TensorType(float64, scalar)> ''
| | |TensorConstant{2} [id BI] <TensorType(int8, scalar)>
| | |softplus [id BJ] <TensorType(float64, scalar)> ''
| | |Elemwise{neg,no_inplace} [id BK] <TensorType(float64, scalar)> ''
| | |exp_interval___shared [id Y] <TensorType(float64, scalar)>
| |exp_interval___shared [id Y] <TensorType(float64, scalar)>
|Elemwise{add,no_inplace} [id BL] <TensorType(float64, scalar)> ''
| |Elemwise{switch,no_inplace} [id BM] <TensorType(float32, scalar)> ''
| | |Elemwise{mul,no_inplace} [id BN] <TensorType(int8, scalar)> ''
| | | |Elemwise{mul,no_inplace} [id BO] <TensorType(int8, scalar)> ''
| | | | |TensorConstant{1} [id E] <TensorType(int8, scalar)>
| | | | |Elemwise{mul,no_inplace} [id BP] <TensorType(int8, scalar)> ''
| | | | |TensorConstant{1} [id E] <TensorType(int8, scalar)>
| | | | |Elemwise{ge,no_inplace} [id BQ] <TensorType(bool, scalar)> ''
| | | | |Elemwise{add,no_inplace} [id BR] <TensorType(float64, scalar)> ''
| | | | | |Elemwise{mul,no_inplace} [id BS] <TensorType(float64, scalar)> ''
| | | | | | |TensorConstant{1.0} [id BT] <TensorType(float32, scalar)>
| | | | | | |sigmoid [id BU] <TensorType(float64, scalar)> ''
| | | | | | |Subtensor{int64} [id BV] <TensorType(float64, scalar)> ''
| | | | | | |Subtensor{int64:int64:} [id BW] <TensorType(float64, (True,))> ''
| | | | | | | |inarray1 [id BX] <TensorType(float64, (True,))>
| | | | | | | |Constant{0} [id BY] <int64>
| | | | | | | |Constant{1} [id BZ] <int64>
| | | | | | |Constant{0} [id BY] <int64>
| | | | | |TensorConstant{0.0} [id CA] <TensorType(float32, scalar)>
| | | | |TensorConstant{0.0} [id CA] <TensorType(float32, scalar)>
| | | |Elemwise{mul,no_inplace} [id CB] <TensorType(int8, scalar)> ''
| | | |TensorConstant{1} [id E] <TensorType(int8, scalar)>
| | | |Elemwise{le,no_inplace} [id CC] <TensorType(bool, scalar)> ''
| | | |Elemwise{add,no_inplace} [id BR] <TensorType(float64, scalar)> ''
| | | |TensorConstant{1.0} [id BT] <TensorType(float32, scalar)>
| | |TensorConstant{0.0} [id CA] <TensorType(float32, scalar)>
| | |TensorConstant{-inf} [id N] <TensorType(float32, scalar)>
| |Elemwise{sub,no_inplace} [id CD] <TensorType(float64, scalar)> ''
| |Elemwise{sub,no_inplace} [id CE] <TensorType(float64, scalar)> ''
| | |TensorConstant{0.0} [id CA] <TensorType(float32, scalar)>
| | |Elemwise{mul,no_inplace} [id CF] <TensorType(float64, scalar)> ''
| | |TensorConstant{2} [id BI] <TensorType(int8, scalar)>
| | |softplus [id CG] <TensorType(float64, scalar)> ''
| | |Elemwise{neg,no_inplace} [id CH] <TensorType(float64, scalar)> ''
| | |Subtensor{int64} [id BV] <TensorType(float64, scalar)> ''
| |Subtensor{int64} [id BV] <TensorType(float64, scalar)> ''
|FromFunctionOp{logp} [id CI] <TensorType(float64, scalar)> ''
|ViewOp [id CJ] <TensorType(float64, scalar)> 'h_epsilon'
|learning_iter_shared [id H] <TensorType(int64, scalar)>
|ViewOp [id CK] <TensorType(float64, scalar)> 'exp'
|TensorConstant{[13 11]} [id CL] <TensorType(int64, vector)>
Storage map footprint:
- TensorConstant{[13 11]}, Shape: (2,), ElemSize: 8 Byte(s), TotalSize: 16 Byte(s)
- Elemwise{switch,no_inplace}.0, Shape: (), ElemSize: 8 Byte(s), TotalSize: 8.0 Byte(s)
- Elemwise{add,no_inplace}.0, Shape: (), ElemSize: 8 Byte(s), TotalSize: 8.0 Byte(s)
- Constant{0}, Shape: (), ElemSize: 8 Byte(s), TotalSize: 8.0 Byte(s)
- Constant{1}, Shape: (), ElemSize: 8 Byte(s), TotalSize: 8.0 Byte(s)
- inarray1, Input, Shape: (1,), ElemSize: 8 Byte(s), TotalSize: 8 Byte(s)
- inarray, Input, Shape: (1,), ElemSize: 8 Byte(s), TotalSize: 8 Byte(s)
- learning_iter_shared, Shared Input, Shape: (), ElemSize: 8 Byte(s), TotalSize: 8.0 Byte(s)
- exp_interval___shared, Shared Input, Shape: (), ElemSize: 8 Byte(s), TotalSize: 8.0 Byte(s)
- TensorConstant{-1.60943791243}, Shape: (), ElemSize: 8 Byte(s), TotalSize: 8.0 Byte(s)
- Elemwise{add,no_inplace}.0, Shape: (), ElemSize: 8 Byte(s), TotalSize: 8.0 Byte(s)
- exp, Shape: (), ElemSize: 8 Byte(s), TotalSize: 8.0 Byte(s)
- TensorConstant{5}, Shape: (), ElemSize: 4 Byte(s), TotalSize: 4.0 Byte(s)
- TensorConstant{-inf}, Shape: (), ElemSize: 4 Byte(s), TotalSize: 4.0 Byte(s)
- TensorConstant{1}, Shape: (), ElemSize: 4 Byte(s), TotalSize: 4.0 Byte(s)
- TensorConstant{1.0}, Shape: (), ElemSize: 4 Byte(s), TotalSize: 4.0 Byte(s)
- TensorConstant{0.0}, Shape: (), ElemSize: 4 Byte(s), TotalSize: 4.0 Byte(s)
- TensorConstant{20.0}, Shape: (), ElemSize: 4 Byte(s), TotalSize: 4.0 Byte(s)
- TensorConstant{4.0}, Shape: (), ElemSize: 4 Byte(s), TotalSize: 4.0 Byte(s)
- TensorConstant{2.77258872986}, Shape: (), ElemSize: 4 Byte(s), TotalSize: 4.0 Byte(s)
- TensorConstant{-2.77258872986}, Shape: (), ElemSize: 4 Byte(s), TotalSize: 4.0 Byte(s)
- TensorConstant{16.0}, Shape: (), ElemSize: 4 Byte(s), TotalSize: 4.0 Byte(s)
- TensorConstant{2}, Shape: (), ElemSize: 1 Byte(s), TotalSize: 1.0 Byte(s)
- TensorConstant{1}, Shape: (), ElemSize: 1 Byte(s), TotalSize: 1.0 Byte(s)
TotalSize: 146.0 Byte(s) 0.000 GB
TotalSize inputs: 114.0 Byte(s) 0.000 GB