-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Deprecate block_diag
from math module in favor of PyTensor
#7132
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fd5f642
Remove block_diag from pymc.math in favor of alias to pytensor.tensor…
AryanNanda17 a5913e5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] b55c877
FutureWarning Added
AryanNanda17 3320155
Update
AryanNanda17 a01db7f
Update
AryanNanda17 fe8a065
Update
AryanNanda17 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,8 +22,6 @@ | |
import pytensor.sparse | ||
import pytensor.tensor as pt | ||
import pytensor.tensor.slinalg | ||
import scipy as sp | ||
import scipy.sparse | ||
|
||
from pytensor.graph.basic import Apply | ||
from pytensor.graph.op import Op | ||
|
@@ -93,9 +91,8 @@ | |
from pytensor.tensor.linalg import solve_triangular | ||
from pytensor.tensor.nlinalg import matrix_inverse | ||
from pytensor.tensor.special import log_softmax, softmax | ||
from scipy.linalg import block_diag as scipy_block_diag | ||
|
||
from pymc.pytensorf import floatX, ix_, largest_common_dtype | ||
from pymc.pytensorf import floatX | ||
|
||
__all__ = [ | ||
"abs", | ||
|
@@ -513,55 +510,9 @@ | |
raise ValueError("Input should be 2 or 3 dimensional") | ||
|
||
|
||
class BlockDiagonalMatrix(Op): | ||
__props__ = ("sparse", "format") | ||
|
||
def __init__(self, sparse=False, format="csr"): | ||
if format not in ("csr", "csc"): | ||
raise ValueError(f"format must be one of: 'csr', 'csc', got {format}") | ||
self.sparse = sparse | ||
self.format = format | ||
|
||
def make_node(self, *matrices): | ||
if not matrices: | ||
raise ValueError("no matrices to allocate") | ||
matrices = list(map(pt.as_tensor, matrices)) | ||
if any(mat.type.ndim != 2 for mat in matrices): | ||
raise TypeError("all data arguments must be matrices") | ||
if self.sparse: | ||
out_type = pytensor.sparse.matrix(self.format, dtype=largest_common_dtype(matrices)) | ||
else: | ||
out_type = pytensor.tensor.matrix(dtype=largest_common_dtype(matrices)) | ||
return Apply(self, matrices, [out_type]) | ||
|
||
def perform(self, node, inputs, output_storage, params=None): | ||
dtype = largest_common_dtype(inputs) | ||
if self.sparse: | ||
output_storage[0][0] = sp.sparse.block_diag(inputs, self.format, dtype) | ||
else: | ||
output_storage[0][0] = scipy_block_diag(*inputs).astype(dtype) | ||
|
||
def grad(self, inputs, gout): | ||
shapes = pt.stack([i.shape for i in inputs]) | ||
index_end = shapes.cumsum(0) | ||
index_begin = index_end - shapes | ||
slices = [ | ||
ix_( | ||
pt.arange(index_begin[i, 0], index_end[i, 0]), | ||
pt.arange(index_begin[i, 1], index_end[i, 1]), | ||
) | ||
for i in range(len(inputs)) | ||
] | ||
return [gout[0][slc] for slc in slices] | ||
|
||
def infer_shape(self, fgraph, nodes, shapes): | ||
first, second = zip(*shapes) | ||
return [(pt.add(*first), pt.add(*second))] | ||
|
||
|
||
def block_diagonal(matrices, sparse=False, format="csr"): | ||
r"""See scipy.sparse.block_diag or | ||
scipy.linalg.block_diag for reference | ||
def block_diagonal(*matrices, sparse=False, format="csr"): | ||
r"""See pt.slinalg.block_diag or | ||
pytensor.sparse.basic.block_diag for reference | ||
|
||
Parameters | ||
---------- | ||
|
@@ -577,4 +528,8 @@ | |
""" | ||
if len(matrices) == 1: # graph optimization | ||
return matrices[0] | ||
return BlockDiagonalMatrix(sparse=sparse, format=format)(*matrices) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a FutureWarning mentioning this is deprecated in favor of the pytensor function, so we can remove this thin wrapper sometime in the future? |
||
if sparse: | ||
return pytensor.sparse.basic.block_diag(*matrices, format=format) | ||
else: | ||
return pt.slinalg.block_diag(*matrices) | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,4 +30,4 @@ sphinx>=1.5 | |
sphinxext-rediraffe | ||
types-cachetools | ||
typing-extensions>=3.7.4 | ||
watermark | ||
watermark |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should keep the old signature, missed this before:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated it!