Skip to content

DOC: Docstrings for Elemwise Ops are not very helpful #292

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
jessegrabowski opened this issue May 10, 2023 · 0 comments · Fixed by #1255
Closed

DOC: Docstrings for Elemwise Ops are not very helpful #292

jessegrabowski opened this issue May 10, 2023 · 0 comments · Fixed by #1255
Labels

Comments

@jessegrabowski
Copy link
Member

Issue with current documentation:

When a user checks a docstring for most basic Elemwise Ops, he is met with a wall of generic boilerplate:

import pytensor.tensor as pt

help(pt.exp)

Returns:

Help on Elemwise in module pytensor.tensor.math:

<pytensor.tensor.elemwise.Elemwise object>
    e^`a`
    
    Generalizes a scalar `Op` to tensors.
    
    All the inputs must have the same number of dimensions. When the
    `Op` is performed, for each dimension, each input's size for that
    dimension must be the same. As a special case, it can also be one
    but only if the input's `broadcastable` flag is ``True`` for that
    dimension. In that case, the tensor is (virtually) replicated
    along that dimension to match the size of the others.
    
    The dtypes of the outputs mirror those of the scalar `Op` that is
    being generalized to tensors. In particular, if the calculations
    for an output are done in-place on an input, the output type must
    be the same as the corresponding input type (see the doc of
    `ScalarOp` to get help about controlling the output type)
    
    Notes
    -----
    -``Elemwise(add)``: represents ``+`` on tensors ``x + y``
    -``Elemwise(add, {0 : 0})``: represents the ``+=`` operation ``x += y``
    -``Elemwise(add, {0 : 1})``: represents ``+=`` on the second argument ``y += x``
    -``Elemwise(mul)(np.random.random((10, 5)), np.random.random((1, 5)))``:
    the second input is completed along the first dimension to match the first input
    -``Elemwise(true_div)(np.random.random(10, 5), np.random.random(10, 1))``: same but along the
    second dimension
    -``Elemwise(int_div)(np.random.random((1, 5)), np.random.random((10, 1)))``:
    the output has size ``(10, 5)``.
    -``Elemwise(log)(np.random.random((3, 4, 5)))``

I'm not sure how much needs to be said on the exponential function as such, but there are some important Elemwise Ops that are not so obvious. For example, the fact that pt.eq needs to be used instead of == isn't immediately obvious, and could be explained with examples (and counter-examples) in its docstring. A small examples section would also a opportunity to present new users with some basics about building computational graphs, compiling functions, using .eval(), etc.

At the very least, the boilerplate is clutter that could be eliminated. The clutter is worse when you open the docstring in juypter, because it displays the Class docstring (shown above) and the Call docstring, which explains what an Apply node is.

I remember being very confused by all this when I was getting started, so it might be something nice to refine.

Idea or request for content:

No response

twiecki added a commit that referenced this issue Mar 2, 2025
This PR improves docstrings for Elemwise functions by:
1. Modifying scalar_elemwise to no longer append the generic Elemwise docstring
2. Enhancing docstrings for specific mathematical functions (exp, log, eq) with:
   - Better formatted NumPy-style documentation
   - Clear descriptions of functionality
   - Practical usage examples
   - Additional notes on potential pitfalls

This fixes issue #292 which noted that current Elemwise docstrings are
not very helpful due to excessive generic boilerplate.

🤖 Generated with Claude Code
Co-Authored-By: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
1 participant