def delta_logp(logp, vars, shared): [logp0], inarray0 = join_nonshared_inputs([logp], vars, shared) tensor_type = inarray0.type inarray1 = tensor_type('inarray1') logp1 = CallableTensor(logp0)(inarray1) f = theano.function([inarray1, inarray0], logp1 - logp0) f.trust_input = True return f
def logp_forw(out_vars, vars, shared): """Compile Theano function of the model and the input and output variables. Parameters ---------- out_vars: List containing :class:`pymc3.Distribution` for the output variables vars: List containing :class:`pymc3.Distribution` for the input variables shared: List containing :class:`theano.tensor.Tensor` for depended shared data """ out_list, inarray0 = join_nonshared_inputs(out_vars, vars, shared) f = theano_function([inarray0], out_list[0]) f.trust_input = True return f
def _theano_hamiltonian(model_vars, shared, logpt, potential): """Creates a Hamiltonian with shared inputs. Parameters ---------- model_vars : array of variables to be sampled shared : theano tensors that are already shared logpt : model log probability potential : hamiltonian potential Returns ------- Hamiltonian : namedtuple with log pdf, gradient of log pdf, and potential functions q : Starting position variable. """ dlogp = gradient(logpt, model_vars) (logp, dlogp), q = join_nonshared_inputs([logpt, dlogp], model_vars, shared) logp = CallableTensor(logp) dlogp = CallableTensor(dlogp) return Hamiltonian(logp, dlogp, potential), q
def _theano_hamiltonian(model_vars, shared, logpt, potential): """Creates a Hamiltonian with shared inputs. Parameters ---------- model_vars : array of variables to be sampled shared : theano tensors that are already shared logpt : model log probability potential : hamiltonian potential Returns ------- Hamiltonian : namedtuple with log pdf, gradient of log pdf, and potential functions q : Starting position variable. """ dlogp = gradient(logpt, model_vars) (logp, dlogp), q = join_nonshared_inputs([logpt, dlogp], model_vars, shared) dlogp_func = theano.function(inputs=[q], outputs=dlogp) dlogp_func.trust_input = True logp = CallableTensor(logp) dlogp = CallableTensor(dlogp) return Hamiltonian(logp, dlogp, potential), q, dlogp_func
def _theano_hamiltonian(model_vars, shared, logpt, potential): """Create a Hamiltonian with shared inputs. Parameters ---------- model_vars : array of variables to be sampled shared : theano tensors that are already shared logpt : model log probability potential : hamiltonian potential Returns ------- Hamiltonian : namedtuple with log pdf, gradient of log pdf, and potential functions q : Initial position for Hamiltonian Monte Carlo dlogp_func: theano function that computes the gradient of a log pdf at a point """ dlogp = gradient(logpt, model_vars) (logp, dlogp), q = join_nonshared_inputs([logpt, dlogp], model_vars, shared) dlogp_func = theano.function(inputs=[q], outputs=dlogp) dlogp_func.trust_input = True logp = CallableTensor(logp) dlogp = CallableTensor(dlogp) return Hamiltonian(logp, dlogp, potential), q, dlogp_func
def logp_forw(logp, vars, shared): [logp0], inarray0 = join_nonshared_inputs([logp], vars, shared) f = theano.function([inarray0], logp0) f.trust_input = True return f