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

Efficient Tensor conversion from list of numpy arrays #1071

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion pomegranate/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ def _cast_as_tensor(value, dtype=None):
return value
else:
return value.type(dtype)


if isinstance(value, list) and all(isinstance(v, numpy.ndarray) for v in value):
value = numpy.array(value)
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't seem to have a return statement in all cases?

Copy link
Owner

Choose a reason for hiding this comment

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

It's saying that if value is a list of numpy arrays, cast value as a single numpy array. L63 will then take that new numpy array and operate on it, but more efficiently than if it were a list of arrays.

Copy link
Contributor

Choose a reason for hiding this comment

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

Doh!


if isinstance(value, (float, int, list, tuple, numpy.ndarray)):
if dtype is None:
return torch.tensor(value)
Expand Down