Skip to content

Add PyMC code examples to math function docstrings #824

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion pytensor/tensor/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,34 @@ def cast(x, dtype: str | np.dtype) -> TensorVariable:

@scalar_elemwise
def switch(cond, ift, iff):
"""if cond then ift else iff"""
"""
Conditionally selects elements from two tensors based on a condition tensor.

This op is similar to NumPy's `np.where` and `np.choose` functions.

Parameters
----------
cond : TensorVariable
A boolean-type tensor determining which output value to choose.
Should be broadcastable to the shapes of `ift` and `iff`.
ift : TensorVariable
Values selected at `True` elements of `cond`.
iff : TensorVariable
Values selected at `False` elements of `cond`.

Examples
--------
This example demonstrates how `switch` can be used in PyMC to model a
categorical variable.

.. code:: python

import pymc as pm

with pm.Model():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import pymc, use pm.math.switch? For a fully reproducible snippet and emphasize math module

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use pm.math or pt, since this is in the pytensor repo?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I didn't even realize this was in the pytensor repo. These examples should be in PyMC and in the PyMC math module docs. It doesn't make sense to mention PyMC in PyTensor

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The API docs currently use the docstrings from the PyTensor functions. Do we need to create explicit wrappers in PyMC?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can add more stuff in the docs page than the default docstrings I think (or completely override the defaults).

x = pm.Categorical('x', np.array([0.1, 0.9]))
y = pm.Bernoulli('y', p=pm.math.switch(x, 0.9, 0.1), shape=10)
"""


where = switch
Expand Down
23 changes: 22 additions & 1 deletion pytensor/tensor/extra_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,9 +410,18 @@ def cumsum(x, axis=None):
axis
The axis along which the cumulative sum is computed.
The default (None) is to compute the cumsum over the flattened array.
# noqa W293
Example
-------
Usage in PyMC:

.. code-block:: python

.. versionadded:: 0.7
with pm.Model() as model:
x0 = pm.Normal('x0')
x = pm.Normal('x', mu=0, sd=1, shape=10)
# Gaussian random walk
grw = pm.Deterministic('grw', x0 + pm.math.cumsum(x))

"""
return CumOp(axis=axis, mode="add")(x)
Expand All @@ -430,7 +439,19 @@ def cumprod(x, axis=None):
axis
The axis along which the cumulative product is computed.
The default (None) is to compute the `cumprod` over the flattened array.
# noqa W293
Example
-------
Usage in PyMC:

.. code-block:: python

import pymc as pm

with pm.Model() as model:
x = pm.Normal('x', shape=(10, 3))
# Product of x
prod_x = pm.Deterministic('prod_x', pm.math.cumprod(x, axis=0))

.. versionadded:: 0.7

Expand Down
21 changes: 21 additions & 0 deletions pytensor/tensor/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -1945,6 +1945,17 @@ def dot(l, r):
"""Return a symbolic dot product.

This is designed to work with both sparse and dense tensors types.

Example usage with PyMC:

.. code:: python

import pymc as pm

with pm.Model() as model:
x = pm.Normal('x', mu=0, sd=1, shape=2)
y = pm.Normal('y', mu=0, sd=1, shape=2)
z = pt.math.dot(x, y)
"""

if not isinstance(l, Variable):
Expand Down Expand Up @@ -2664,6 +2675,16 @@ def prod(
If this is set to True, the axes which are reduced are left in
the result as dimensions with size one. With this option, the result
will broadcast correctly against the original tensor.
# noqa W293
Example
-------
.. code-block:: python

import pymc as pm

with pm.Model() as model:
n = pm.Poisson('n', 1, shape=(2, 3))
prod_n = pm.Deterministic('prod_n', pm.math.prod(n, axis=0))

"""

Expand Down
29 changes: 29 additions & 0 deletions pytensor/tensor/special.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,35 @@ def c_code_cache_version():


def softmax(c, axis=None):
"""
Compute the softmax of a vector along a specified axis.

Parameters
----------
c : TensorVariable
The input tensor.
axis : int
The axis along which to compute the softmax.

Returns
-------
TensorVariable
The softmax of the input tensor along the specified axis.

Examples
--------
In PyMC, you can use this function to compute a softmax over a vector of
probabilities representing the likelihood of each class in a multiclass
classification problem. Here is an example::

import pymc as pm

with pm.Model() as model:
weights = pm.Gamma('weights', 1, 1, shape=3)
softmax_prob = pm.math.softmax(weights)
outcome = pm.Categorical('outcome', p=softmax_prob)

"""
c = as_tensor_variable(c)
return Softmax(axis=axis)(c)

Expand Down
Loading