Skip to content
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

Implement vectorize helper with similar semantics to that of NumPy #500

Merged
merged 2 commits into from
Nov 15, 2023

Conversation

ricardoV94
Copy link
Member

@ricardoV94 ricardoV94 commented Nov 15, 2023

Just a difference between providing a function or working on an existing graph.

Creating dummy inputs may not be intuitive to many users, so this helps with that. Also behaves more like NumPy.

import pytensor
import pytensor.tensor as pt

def func(x):
    return pt.exp(x) / pt.sum(pt.exp(x))

vec_func = pt.vectorize(func, signature="(a)->(a)")

x = pt.matrix("x")
y = vec_func(x)

fn = pytensor.function([x], y)
fn([[0, 1, 2], [2, 1, 0]])
# array([[0.09003057, 0.24472847, 0.66524096],
#        [0.66524096, 0.24472847, 0.09003057]])

@codecov-commenter
Copy link

codecov-commenter commented Nov 15, 2023

Codecov Report

Merging #500 (225f429) into main (902eeb6) will increase coverage by 0.01%.
The diff coverage is 95.45%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #500      +/-   ##
==========================================
+ Coverage   80.75%   80.77%   +0.01%     
==========================================
  Files         161      162       +1     
  Lines       46078    46105      +27     
  Branches    11258    11266       +8     
==========================================
+ Hits        37212    37240      +28     
- Misses       6637     6638       +1     
+ Partials     2229     2227       -2     
Files Coverage Δ
pytensor/tensor/blockwise.py 79.89% <100.00%> (-0.11%) ⬇️
pytensor/tensor/functional.py 100.00% <100.00%> (ø)
pytensor/graph/replace.py 84.84% <71.42%> (-1.47%) ⬇️

... and 1 file with indirect coverage changes

def vectorize(func: Callable, signature: Optional[str] = None) -> Callable:
"""Equivalent to numpy.vectorize.

If you have an existing graph you wish to vectorize, use `pytensor.graph.replace.vectorize_graph`
Copy link
Member

Choose a reason for hiding this comment

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

Should this be a note further down below?

Copy link
Member Author

Choose a reason for hiding this comment

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

Where would you put it?

Copy link
Member

Choose a reason for hiding this comment

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

After Returns a new heading Notes

Copy link
Member

Choose a reason for hiding this comment

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

I was going to comment on this too; it's just not so clear what it means (since a priori everything in pytensor is a graph).

Maybe:

Vectorize a python function with symbolic pytensor inputs and outputs according to a function signature, equivalent to numpy.vectorize.

Note that this function cannot operate on pytensor variables that represent computational graphs. To vectorize an existing pytensor graph, use `pytensor.graph.replace.vectorize`. 

func: Callable
Function that returns PyTensor varibales from TensorVariable inputs.
signature: str, optional
String that specifies the number and dimension of inputs and outputs in the core case.
Copy link
Member

Choose a reason for hiding this comment

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

I think this could be a bit better explained.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think people will be familiar with how it works in numpy, and if not they can check it. We just borrowed their API

@ricardoV94
Copy link
Member Author

Updated docstrings @twiecki @jessegrabowski

Copy link
Member

@jessegrabowski jessegrabowski left a comment

Choose a reason for hiding this comment

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

10/10 will use

@ricardoV94 ricardoV94 merged commit 230a808 into pymc-devs:main Nov 15, 2023
53 checks passed
@ricardoV94 ricardoV94 deleted the vectorize_user_facing branch November 16, 2023 06:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants