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 1 commit
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
26 changes: 25 additions & 1 deletion pytensor/tensor/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -757,8 +757,32 @@ 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

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=switch(x, 0.9, 0.1), shape=10)
"""

where = switch

Expand Down
12 changes: 12 additions & 0 deletions pytensor/tensor/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -1945,6 +1945,18 @@ 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 pymc3 as pm
import theano.tensor as tt

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 = tt.dot(x, y)
"""

if not isinstance(l, Variable):
Expand Down
30 changes: 30 additions & 0 deletions pytensor/tensor/special.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,36 @@ 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
import pytensor.tensor as pt

with pm.Model() as model:
weights = pm.Gamma('weights', 1, 1, shape=3)
softmax_prob = pt.softmax(weights)
Copy link
Member

Choose a reason for hiding this comment

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

Same

outcome = pm.Categorical('outcome', p=softmax_prob)

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

Expand Down
Loading