# Custom operation for likelihood and cache value of likelihood

**URL:** <https://discourse.pymc.io/t/custom-operation-for-likelihood-and-cache-value-of-likelihood/3197>\
**Category:** Questions\
**Created:** [May 2, 2019, 1:19pm UTC](https://discourse.pymc.io/t/custom-operation-for-likelihood-and-cache-value-of-likelihood/3197 "2019-05-02T13:19:40Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Hamster\_on\_wheels](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/hamster_on_wheels/32/1823_2.png) [@Hamster\_on\_wheels](https://discourse.pymc.io/u/Hamster_on_wheels)\
**Post date:** [May 7, 2019, 11:54pm UTC](https://discourse.pymc.io/t/custom-operation-for-likelihood-and-cache-value-of-likelihood/3197/2 "2019-05-07T23:54:25Z")

</div>

Probably I can do the following to avoid evaluating the same Y for twice.

1. Wrap the intermediate calculations Y into an operation without gradient.
2. Create an operation A without a gradient, which calculates the log-likelihood log(X) using the operation Y.
3. Create an operation B without a gradient, which calculates the gradient of the log-likelihood (1 / X) \* ∂X/∂θ using the operation Y again.
4. Create an operation C with gradient for the log-likelihood and its gradient. Operation C depends on operation A and B.

But, when I define a custom theano operation, I have the impression that the methods of my custom operation seem to take numpy array instead of theano tensors as arguments. While I imagine theano tensors are associated with a hash in the computation graph, the numpy arrays would not have this hash.

Can my strategy really avoid evaluating the computation in operation Y for twice?

How often does NUTS ask for the log-likelihood and for the gradient?

---

_[View the full topic](https://discourse.pymc.io/t/custom-operation-for-likelihood-and-cache-value-of-likelihood/3197)._
