Thanks, @junpenglao, got my stuff to work (so good so far) with Potential and using a sigmoid instead of the switch. The sampling rate has dropped approximately 8-fold, which is OK.
I don’t quite understand how to use Odered transform though (thought to check if it was faster).
I tried
#model params:
N = 2
xl = 0
xu = 1
class Ordered(pm.distributions.transforms.ElemwiseTransform):
name = "ordered"
def forward(self, x):
out = tt.zeros(x.shape)
out = tt.inc_subtensor(out[0], x[0])
out = tt.inc_subtensor(out[1:], tt.log(x[1:] - x[:-1]))
return out
def forward_val(self, x, point=None):
x, = pm.distributions.distribution.draw_values([x], point=point)
return self.forward(x)
def backward(self, y):
out = tt.zeros(y.shape)
out = tt.inc_subtensor(out[0], y[0])
out = tt.inc_subtensor(out[1:], tt.exp(y[1:]))
return tt.cumsum(out)
def jacobian_det(self, y):
return tt.sum(y[1:])
triangle = pm.Model()
with triangle:
"""
This is to test the assertion that rescaling approach to create generative models with linear constraints produces
improper densities
Knowledge:
1) x[0],x[1] \in [xl,xu]
2) x[0] < x[2]
"""
x_naive = pm.Uniform('x',
lower = xl,
upper = xu,
shape = N,
transform=Ordered())
with triangle:
trace = pm.sample(draws = 5000,njobs = 1)
with various combinations of shape parameter of Uniform transform: N, (N), (N,1), (1,N)
, and tried increasing N
, all giving this error:
Traceback (most recent call last):
File "/mnt/emptyplaceholder/tmp/pycharm-2017.3.2/helpers/pydev/pydev_run_in_console.py", line 53, in run_file
pydev_imports.execfile(file, globals, locals) # execute the script
File "/mnt/emptyplaceholder/tmp/pycharm-2017.3.2/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/mnt/emptyplaceholder/mcmc/Experiments/20180119_1_triangle/sample_ordered.py", line 55, in <module>
transform=Ordered())
File "/mnt/emptyplaceholder/miniconda/lib/python3.6/site-packages/pymc3/distributions/distribution.py", line 37, in __new__
return model.Var(name, dist, data, total_size)
File "/mnt/emptyplaceholder/miniconda/lib/python3.6/site-packages/pymc3/model.py", line 759, in Var
model=self)
File "/mnt/emptyplaceholder/miniconda/lib/python3.6/site-packages/pymc3/model.py", line 1393, in __init__
transformed_name, transform.apply(distribution), total_size=total_size)
File "/mnt/emptyplaceholder/miniconda/lib/python3.6/site-packages/pymc3/distributions/transforms.py", line 38, in apply
return TransformedDistribution.dist(dist, self)
File "/mnt/emptyplaceholder/miniconda/lib/python3.6/site-packages/pymc3/distributions/distribution.py", line 47, in dist
dist.__init__(*args, **kwargs)
File "/mnt/emptyplaceholder/miniconda/lib/python3.6/site-packages/pymc3/distributions/transforms.py", line 63, in __init__
testval = forward(dist.default())
File "/mnt/emptyplaceholder/mcmc/Experiments/20180119_1_triangle/sample_ordered.py", line 24, in forward
out = tt.inc_subtensor(out[0], x[0])
File "/mnt/emptyplaceholder/miniconda/lib/python3.6/site-packages/theano/tensor/var.py", line 579, in __getitem__
lambda entry: isinstance(entry, Variable)))
File "/mnt/emptyplaceholder/miniconda/lib/python3.6/site-packages/theano/gof/op.py", line 615, in __call__
node = self.make_node(*inputs, **kwargs)
File "/mnt/emptyplaceholder/miniconda/lib/python3.6/site-packages/theano/tensor/subtensor.py", line 481, in make_node
raise exception
ValueError: The index list is longer (size 1) than the number of dimensions of the tensor(namely 0). You are asking for a dimension of the tensor that does not exist! You might need to use dimshuffle to add extra dimension to your tensor.
When I stopped in the debugger on line out = tt.inc_subtensor(out[0], x[0])
which was raising the error and evaluated x, it returned an output of array(0.5) followed by an error, like so:
In[2]: x
Out[2]: array(0.5)
Error evaluating: thread_id: pid_10211_id_140233461333352
frame_id: 140232644536784
scope: FRAME
attrs: x
Traceback (most recent call last):
File "/mnt/emptyplaceholder/tmp/pycharm-2017.3.2/helpers/pydev/_pydevd_bundle/pydevd_vars.py", line 239, in resolve_compound_variable
return _typeName, resolver.get_dictionary(var)
File "/mnt/emptyplaceholder/tmp/pycharm-2017.3.2/helpers/pydev/pydevd_plugins/extensions/types/pydevd_plugin_numpy_types.py", line 77, in get_dictionary
ret['[0:%s] ' % (len(obj))] = list(obj[0:MAX_ITEMS_TO_HANDLE])
IndexError: too many indices for array
When I tried x.shape:
x.shape
Out[3]: ()
Error evaluating: thread_id: pid_10211_id_140233461333352
frame_id: 140232644536784
scope: FRAME
attrs: x
Traceback (most recent call last):
File "/mnt/emptyplaceholder/tmp/pycharm-2017.3.2/helpers/pydev/_pydevd_bundle/pydevd_vars.py", line 239, in resolve_compound_variable
return _typeName, resolver.get_dictionary(var)
File "/mnt/emptyplaceholder/tmp/pycharm-2017.3.2/helpers/pydev/pydevd_plugins/extensions/types/pydevd_plugin_numpy_types.py", line 77, in get_dictionary
ret['[0:%s] ' % (len(obj))] = list(obj[0:MAX_ITEMS_TO_HANDLE])
IndexError: too many indices for array