You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We can now check that the gradient Op works as expected. First, just create and call the `LogLikeGrad` class, which should return the gradient directly (note that we have to create a [PyTensor function](http://deeplearning.net/software/pytensor/library/compile/function.html) to convert the output of the Op to an array). Secondly, we call the gradient from `LogLikeWithGrad` by using the [PyTensor tensor gradient](http://deeplearning.net/software/pytensor/library/gradient.html#pytensor.gradient.grad) function. Finally, we will check the gradient returned by the PyMC model for a Normal distribution, which should be the same as the log-likelihood function we defined. In all cases we evaluate the gradients at the true values of the model function (the straight line) that was created.
413
413
414
414
```{code-cell} ipython3
415
-
# test the gradient Op by direct call
416
-
pytensor.config.compute_test_value = "ignore"
417
-
pytensor.config.exception_verbosity = "high"
415
+
ip = pymodel.initial_point()
416
+
print(f"Evaluating dlogp of model at point\n {ip}")
grad_vals_pymc = gradfunc(np.array([mtrue, ctrue]))[1] # get dlogp values
445
-
446
-
print(f'Gradient returned by PyMC "Normal" distribution: {grad_vals_pymc}')
421
+
print(f'\nGradient of model using a custom "LogLikeWithGrad":\n {grad_vals_custom}')
422
+
print(f'Gradient of model using a PyMC "Normal" distribution:\n {grad_vals_pymc}')
447
423
```
448
424
449
425
We could also do some profiling to compare performance between implementations. The {ref}`profiling` notebook shows how to do it.
@@ -454,6 +430,7 @@ We could also do some profiling to compare performance between implementations.
454
430
455
431
* Adapted from [Jørgen Midtbø](https://github.com/jorgenem/)'s [example](https://discourse.pymc.io/t/connecting-pymc-to-external-code-help-with-understanding-pytensor-custom-ops/670) by Matt Pitkin both as a [blogpost](http://mattpitkin.github.io/samplers-demo/pages/pymc-blackbox-likelihood/) and as an example notebook to this gallery in August, 2018 ([pymc#3169](https://github.com/pymc-devs/pymc/pull/3169) and [pymc#3177](https://github.com/pymc-devs/pymc/pull/3177))
456
432
* Updated by [Oriol Abril](https://github.com/OriolAbril) on December 2021 to drop the Cython dependency from the original notebook and use numpy instead ([pymc-examples#28](https://github.com/pymc-devs/pymc-examples/pull/28))
433
+
* Re-executed by Oriol Abril with pymc 5.0.0 ([pymc-examples#496](https://github.com/pymc-devs/pymc-examples/pull/496))
0 commit comments