Skip to content

Refactor convert_observed_data #7299

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

Merged
Merged
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
7 changes: 4 additions & 3 deletions pymc/pytensorf.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,12 @@
]


def convert_observed_data(data):
def convert_observed_data(data) -> np.ndarray | Variable:
"""Convert user provided dataset to accepted formats."""

if isgenerator(data):
return floatX(generator(data))
Comment on lines +84 to +85
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 was on purpose, for something fancy in VI. @ferrine can you confirm whether it's true and whether we still need it?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, the refactor only moves the code, but if we can delete it that'd be even better!

Copy link
Member

Choose a reason for hiding this comment

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

Doesn't wrapping the generator in floatX consume it immediately?

Copy link
Member

Choose a reason for hiding this comment

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

nvm it was also done before

Copy link
Member

Choose a reason for hiding this comment

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

User-provided observed data comes through this, therefore I'm pretty certain that this generator branch is still relevant for VI.

One next step after this PR could be to split the function into two overloads - one of which applies to generator type data, and the other to all the rest.

The more important next step should be the introduction of a dtype kwarg so we can get #7277 fixed.

Copy link
Member

Choose a reason for hiding this comment

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

Just a guess, but that might be for stochastic optimization? If so, that generator could even be infinite.
Why not (floatX(value) for value in generator)?

Copy link
Member

Choose a reason for hiding this comment

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

If the output can be a generator, the type signature is wrong

Copy link
Member

Choose a reason for hiding this comment

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

No, even with a generator, the returned value is a TensorVariable:

grafik

Copy link
Member

Choose a reason for hiding this comment

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

I see some black magic.


if hasattr(data, "to_numpy") and hasattr(data, "isnull"):
# typically, but not limited to pandas objects
vals = data.to_numpy()
Expand Down Expand Up @@ -116,8 +119,6 @@ def convert_observed_data(data):
ret = data
elif sps.issparse(data):
ret = data
elif isgenerator(data):
ret = generator(data)
else:
ret = np.asarray(data)

Expand Down