Connecting PyMC3 to external code - help with understanding Theano custom Ops

I know this is an old topic, but I’m trying to implement the grad method of a Theano Op whose perform method returns a vector, not a scalar. More specifically, the Op operates on a dataset that can have one or more rows and it returns a single value for each row of the dataset (i.e., the output is an Mx1 matrix given an MxN input dataset). The Jacobian in this case will have M rows and P columns, where P is the number of variables in the model.

In my problem, I have a single variable and the dataset has 53 rows and 1 column. I’ve tried a bunch of different things, but I can’t get it to work. It keeps complaining the my input size is 1 (i_shape) and the gradient has a size of 53 (t_shape) in the theano/gradient.py file.

Here’s some code:

def grad(self, inputs, grad_outputs):
    (theta,) = inputs
    J = self.model_grad(theta)
    return [tt.dot(J[0], grad_outputs)]

where self.model_grad calls the gradients function that @mattpitkin explained. Any suggestions? Note that I’m not implementing a log-likelihood Op that returns a single value given a dataset. I want my Op to return one output per row of a dataset.

Additional information:

  • The main Op class has
itypes = [tt.dvector]
otypes = [tt.dvector]
  • The gradient Op class has
itypes = [tt.dvector]
otypes = [tt.dmatrix]