-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Implemented logprob for SpecifyShape and CheckandRaise #6538
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 199 commits
Commits
Show all changes
201 commits
Select commit
Hold shift + click to select a range
379b1e9
Added logprob for SpecifyShape and CheckandRaise
Dhruvanshu-Joshi 5329794
Added logprob for SpecifyShape and CheckandRaise and removed errors
Dhruvanshu-Joshi 949032a
Fix SpecifyShape
ricardoV94 53e0f98
Tests for logprob for Specify_shape and check_raise_assert added
Dhruvanshu-Joshi 6a49bed
Resolved errors
Dhruvanshu-Joshi 814efef
Added logprob for SpecifyShape and CheckandRaise
Dhruvanshu-Joshi 9caf789
Added logprob for SpecifyShape and CheckandRaise and removed errors
Dhruvanshu-Joshi 0e3c9c1
Fix SpecifyShape
ricardoV94 9d0041f
Tests for logprob for Specify_shape and check_raise_assert added
Dhruvanshu-Joshi e54c01d
Added logprob for SpecifyShape and CheckandRaise
Dhruvanshu-Joshi f17a408
Added logprob for SpecifyShape and CheckandRaise and removed errors
Dhruvanshu-Joshi 1db882c
Fix SpecifyShape
ricardoV94 9e1c429
Resolved errors in check_raise_assert
Dhruvanshu-Joshi e21b6cd
Resolved pre-commit error
Dhruvanshu-Joshi ff2b76a
Added logprob for SpecifyShape and CheckandRaise
Dhruvanshu-Joshi b56c345
Added logprob for SpecifyShape and CheckandRaise and removed errors
Dhruvanshu-Joshi 740da4c
Tests for logprob for Specify_shape and check_raise_assert added
Dhruvanshu-Joshi 55fb450
Fix SpecifyShape
ricardoV94 48c3710
Tests for logprob for Specify_shape and check_raise_assert added
Dhruvanshu-Joshi cc1ed51
Added logprob for SpecifyShape and CheckandRaise
Dhruvanshu-Joshi 03b609f
Added logprob for SpecifyShape and CheckandRaise and removed errors
Dhruvanshu-Joshi a8cdee8
Fix SpecifyShape
ricardoV94 dca066b
Resolved errors in check_raise_assert
Dhruvanshu-Joshi b0f34dd
Resolved pre-commit error
Dhruvanshu-Joshi 5824bb7
Combined the checks in logprob and the corresponding tests suites
Dhruvanshu-Joshi eede32f
Updated the checks in logprob and the corresponding tests suites
Dhruvanshu-Joshi f03a7e5
Updated and rectified the checks in logprob and the corresponding tes…
Dhruvanshu-Joshi d2d1c3e
Added logprob for SpecifyShape and CheckandRaise
Dhruvanshu-Joshi 93f9f77
Added logprob for SpecifyShape and CheckandRaise and removed errors
Dhruvanshu-Joshi b0efee0
Fix SpecifyShape
ricardoV94 b679248
Tests for logprob for Specify_shape and check_raise_assert added
Dhruvanshu-Joshi 6144396
Resolved errors
Dhruvanshu-Joshi 24d0426
Added logprob for SpecifyShape and CheckandRaise
Dhruvanshu-Joshi 20125f8
Added logprob for SpecifyShape and CheckandRaise and removed errors
Dhruvanshu-Joshi affbd51
Fix SpecifyShape
ricardoV94 66e7099
Tests for logprob for Specify_shape and check_raise_assert added
Dhruvanshu-Joshi fbe4f84
Added logprob for SpecifyShape and CheckandRaise
Dhruvanshu-Joshi 0e9507e
Added logprob for SpecifyShape and CheckandRaise and removed errors
Dhruvanshu-Joshi 05cd98e
Fix SpecifyShape
ricardoV94 c018416
Resolved errors in check_raise_assert
Dhruvanshu-Joshi 885cd62
Resolved pre-commit error
Dhruvanshu-Joshi c070723
Combined the checks in logprob and the corresponding tests suites
Dhruvanshu-Joshi 8b5d00f
Updated class name
Dhruvanshu-Joshi 38d14cb
Updated class name
Dhruvanshu-Joshi d74a067
Solving merge conflicts
Dhruvanshu-Joshi e88f823
Added logprob for SpecifyShape and CheckandRaise
Dhruvanshu-Joshi dd54d46
Added logprob for SpecifyShape and CheckandRaise and removed errors
Dhruvanshu-Joshi 5c39620
Fix SpecifyShape
ricardoV94 5a92248
Tests for logprob for Specify_shape and check_raise_assert added
Dhruvanshu-Joshi 06688bd
Resolved errors
Dhruvanshu-Joshi 6791067
Added logprob for SpecifyShape and CheckandRaise
Dhruvanshu-Joshi eb1c4cf
Added logprob for SpecifyShape and CheckandRaise and removed errors
Dhruvanshu-Joshi 1b51a75
Fix SpecifyShape
ricardoV94 8ebfa7e
Tests for logprob for Specify_shape and check_raise_assert added
Dhruvanshu-Joshi f349459
Added logprob for SpecifyShape and CheckandRaise
Dhruvanshu-Joshi ba245d2
Added logprob for SpecifyShape and CheckandRaise and removed errors
Dhruvanshu-Joshi 1264916
Fix SpecifyShape
ricardoV94 d4773cc
Resolved errors in check_raise_assert
Dhruvanshu-Joshi 1419418
Resolved pre-commit error
Dhruvanshu-Joshi aea4516
Added logprob for SpecifyShape and CheckandRaise
Dhruvanshu-Joshi 878205b
Added logprob for SpecifyShape and CheckandRaise and removed errors
Dhruvanshu-Joshi ce7d5e1
Tests for logprob for Specify_shape and check_raise_assert added
Dhruvanshu-Joshi 1e1efbf
Fix SpecifyShape
ricardoV94 556a06a
Tests for logprob for Specify_shape and check_raise_assert added
Dhruvanshu-Joshi ad314b1
Added logprob for SpecifyShape and CheckandRaise
Dhruvanshu-Joshi e70810f
Added logprob for SpecifyShape and CheckandRaise and removed errors
Dhruvanshu-Joshi b28a00f
Fix SpecifyShape
ricardoV94 c11a8f8
Resolved errors in check_raise_assert
Dhruvanshu-Joshi 229f61b
Resolved pre-commit error
Dhruvanshu-Joshi 3659a5e
Combined the checks in logprob and the corresponding tests suites
Dhruvanshu-Joshi 3de287f
Updated the checks in logprob and the corresponding tests suites
Dhruvanshu-Joshi 07c844d
Updated and rectified the checks in logprob and the corresponding tes…
Dhruvanshu-Joshi 20839be
Added logprob for SpecifyShape and CheckandRaise
Dhruvanshu-Joshi 6609ed4
Added logprob for SpecifyShape and CheckandRaise and removed errors
Dhruvanshu-Joshi 5f0ca26
Fix SpecifyShape
ricardoV94 57df1a9
Tests for logprob for Specify_shape and check_raise_assert added
Dhruvanshu-Joshi 15c1403
Resolved errors
Dhruvanshu-Joshi 1294ae8
Added logprob for SpecifyShape and CheckandRaise
Dhruvanshu-Joshi d9bde80
Added logprob for SpecifyShape and CheckandRaise and removed errors
Dhruvanshu-Joshi 4368145
Fix SpecifyShape
ricardoV94 0ffd52e
Tests for logprob for Specify_shape and check_raise_assert added
Dhruvanshu-Joshi 6a303a5
Added logprob for SpecifyShape and CheckandRaise
Dhruvanshu-Joshi 7e46130
Added logprob for SpecifyShape and CheckandRaise and removed errors
Dhruvanshu-Joshi c97d5ec
Fix SpecifyShape
ricardoV94 3410188
Resolved errors in check_raise_assert
Dhruvanshu-Joshi 36c657f
Resolved pre-commit error
Dhruvanshu-Joshi e348300
Combined the checks in logprob and the corresponding tests suites
Dhruvanshu-Joshi 5eddea8
Solving all merge conflicts
Dhruvanshu-Joshi 25478a5
Reducing code redundancy
Dhruvanshu-Joshi 546e1c0
Reducing code redundancy
Dhruvanshu-Joshi 087b00d
Added logprob for SpecifyShape and CheckandRaise
Dhruvanshu-Joshi 8551fd5
Added logprob for SpecifyShape and CheckandRaise and removed errors
Dhruvanshu-Joshi 8464cc1
Fix SpecifyShape
ricardoV94 1876001
Tests for logprob for Specify_shape and check_raise_assert added
Dhruvanshu-Joshi 7cab395
Resolved errors
Dhruvanshu-Joshi 23a74fb
Added logprob for SpecifyShape and CheckandRaise
Dhruvanshu-Joshi 82cc153
Added logprob for SpecifyShape and CheckandRaise and removed errors
Dhruvanshu-Joshi d50137d
Fix SpecifyShape
ricardoV94 e2e2cb8
Tests for logprob for Specify_shape and check_raise_assert added
Dhruvanshu-Joshi 89305dc
Added logprob for SpecifyShape and CheckandRaise
Dhruvanshu-Joshi a0b722c
Added logprob for SpecifyShape and CheckandRaise and removed errors
Dhruvanshu-Joshi f878cda
Fix SpecifyShape
ricardoV94 41f3175
Resolved errors in check_raise_assert
Dhruvanshu-Joshi 59940d7
Resolved pre-commit error
Dhruvanshu-Joshi bc4110b
Added logprob for SpecifyShape and CheckandRaise
Dhruvanshu-Joshi 4d12c16
Added logprob for SpecifyShape and CheckandRaise and removed errors
Dhruvanshu-Joshi f2f109e
Tests for logprob for Specify_shape and check_raise_assert added
Dhruvanshu-Joshi 21d1f9b
Fix SpecifyShape
ricardoV94 430f81c
Tests for logprob for Specify_shape and check_raise_assert added
Dhruvanshu-Joshi 67c32a7
Added logprob for SpecifyShape and CheckandRaise
Dhruvanshu-Joshi 94edd31
Added logprob for SpecifyShape and CheckandRaise and removed errors
Dhruvanshu-Joshi 5fe9c37
Fix SpecifyShape
ricardoV94 bc8b6df
Resolved errors in check_raise_assert
Dhruvanshu-Joshi 9652d99
Resolved pre-commit error
Dhruvanshu-Joshi 245b678
Combined the checks in logprob and the corresponding tests suites
Dhruvanshu-Joshi 8732720
Updated the checks in logprob and the corresponding tests suites
Dhruvanshu-Joshi bb15eec
Updated and rectified the checks in logprob and the corresponding tes…
Dhruvanshu-Joshi 1d5bf00
Added logprob for SpecifyShape and CheckandRaise
Dhruvanshu-Joshi 96ddacf
Added logprob for SpecifyShape and CheckandRaise and removed errors
Dhruvanshu-Joshi d676945
Fix SpecifyShape
ricardoV94 ff6ec56
Tests for logprob for Specify_shape and check_raise_assert added
Dhruvanshu-Joshi cea9840
Resolved errors
Dhruvanshu-Joshi 79c2856
Added logprob for SpecifyShape and CheckandRaise
Dhruvanshu-Joshi 5b1ebe0
Added logprob for SpecifyShape and CheckandRaise and removed errors
Dhruvanshu-Joshi 9008c09
Fix SpecifyShape
ricardoV94 72c63ab
Tests for logprob for Specify_shape and check_raise_assert added
Dhruvanshu-Joshi d7cc4f6
Added logprob for SpecifyShape and CheckandRaise
Dhruvanshu-Joshi b0ac4cf
Added logprob for SpecifyShape and CheckandRaise and removed errors
Dhruvanshu-Joshi 444c285
Fix SpecifyShape
ricardoV94 06e23cc
Resolved errors in check_raise_assert
Dhruvanshu-Joshi ee146f0
Resolved pre-commit error
Dhruvanshu-Joshi c08b819
Combined the checks in logprob and the corresponding tests suites
Dhruvanshu-Joshi 7dd588b
Solving numerous commits issue
Dhruvanshu-Joshi ad6ec33
Updated the checks in logprob and the corresponding tests suites
Dhruvanshu-Joshi 0f5d05b
Updated and rectified the checks in logprob and the corresponding tes…
Dhruvanshu-Joshi 5f0aa4c
Added logprob for SpecifyShape and CheckandRaise
Dhruvanshu-Joshi 63af1b4
Added logprob for SpecifyShape and CheckandRaise and removed errors
Dhruvanshu-Joshi 413a09a
Fix SpecifyShape
ricardoV94 f2c15f0
Tests for logprob for Specify_shape and check_raise_assert added
Dhruvanshu-Joshi 4642e46
Resolved errors
Dhruvanshu-Joshi 9034fde
Added logprob for SpecifyShape and CheckandRaise
Dhruvanshu-Joshi 291e25c
Added logprob for SpecifyShape and CheckandRaise and removed errors
Dhruvanshu-Joshi c1d0e97
Fix SpecifyShape
ricardoV94 ffed617
Tests for logprob for Specify_shape and check_raise_assert added
Dhruvanshu-Joshi 4fe6f6d
Added logprob for SpecifyShape and CheckandRaise
Dhruvanshu-Joshi c9d4d1a
Added logprob for SpecifyShape and CheckandRaise and removed errors
Dhruvanshu-Joshi fdf6322
Fix SpecifyShape
ricardoV94 53d22d4
Resolved errors in check_raise_assert
Dhruvanshu-Joshi a2a40cf
Resolved pre-commit error
Dhruvanshu-Joshi 1dd12a7
Combined the checks in logprob and the corresponding tests suites
Dhruvanshu-Joshi a38b519
Updated class name
Dhruvanshu-Joshi 216adba
Solving merge conflicts
Dhruvanshu-Joshi 3f4463f
Added logprob for SpecifyShape and CheckandRaise
Dhruvanshu-Joshi f67c36a
Added logprob for SpecifyShape and CheckandRaise and removed errors
Dhruvanshu-Joshi b391cd5
Fix SpecifyShape
ricardoV94 efb4777
Tests for logprob for Specify_shape and check_raise_assert added
Dhruvanshu-Joshi e3df69d
Resolved errors
Dhruvanshu-Joshi 247a715
Added logprob for SpecifyShape and CheckandRaise
Dhruvanshu-Joshi a638aae
Added logprob for SpecifyShape and CheckandRaise and removed errors
Dhruvanshu-Joshi 8cc3599
Fix SpecifyShape
ricardoV94 4473035
Tests for logprob for Specify_shape and check_raise_assert added
Dhruvanshu-Joshi 7a042c3
Added logprob for SpecifyShape and CheckandRaise
Dhruvanshu-Joshi 31e2137
Added logprob for SpecifyShape and CheckandRaise and removed errors
Dhruvanshu-Joshi 09f74f3
Fix SpecifyShape
ricardoV94 13f98db
Resolved errors in check_raise_assert
Dhruvanshu-Joshi 9d684bf
Resolved pre-commit error
Dhruvanshu-Joshi 76627e9
Added logprob for SpecifyShape and CheckandRaise
Dhruvanshu-Joshi b19db27
Added logprob for SpecifyShape and CheckandRaise and removed errors
Dhruvanshu-Joshi 67eb279
Tests for logprob for Specify_shape and check_raise_assert added
Dhruvanshu-Joshi fc78195
Fix SpecifyShape
ricardoV94 19bd626
Tests for logprob for Specify_shape and check_raise_assert added
Dhruvanshu-Joshi dc78ebe
Added logprob for SpecifyShape and CheckandRaise
Dhruvanshu-Joshi 4680983
Added logprob for SpecifyShape and CheckandRaise and removed errors
Dhruvanshu-Joshi 27d5961
Fix SpecifyShape
ricardoV94 c8fbd13
Resolved errors in check_raise_assert
Dhruvanshu-Joshi af54c39
Resolved pre-commit error
Dhruvanshu-Joshi da95c1f
Combined the checks in logprob and the corresponding tests suites
Dhruvanshu-Joshi 439b772
Updated the checks in logprob and the corresponding tests suites
Dhruvanshu-Joshi 1996bc1
Updated and rectified the checks in logprob and the corresponding tes…
Dhruvanshu-Joshi 24282a5
Added logprob for SpecifyShape and CheckandRaise
Dhruvanshu-Joshi 6b632ac
Added logprob for SpecifyShape and CheckandRaise and removed errors
Dhruvanshu-Joshi 0c7c3c1
Fix SpecifyShape
ricardoV94 d7bdbbf
Tests for logprob for Specify_shape and check_raise_assert added
Dhruvanshu-Joshi 3d2d250
Resolved errors
Dhruvanshu-Joshi f2b0e7d
Added logprob for SpecifyShape and CheckandRaise
Dhruvanshu-Joshi 56f7445
Added logprob for SpecifyShape and CheckandRaise and removed errors
Dhruvanshu-Joshi 98b1a36
Fix SpecifyShape
ricardoV94 27d7a97
Tests for logprob for Specify_shape and check_raise_assert added
Dhruvanshu-Joshi 6f27645
Added logprob for SpecifyShape and CheckandRaise
Dhruvanshu-Joshi 0018dc0
Added logprob for SpecifyShape and CheckandRaise and removed errors
Dhruvanshu-Joshi 7f3c82d
Fix SpecifyShape
ricardoV94 df5c846
Resolved errors in check_raise_assert
Dhruvanshu-Joshi e9b0928
Resolved pre-commit error
Dhruvanshu-Joshi dd8aee6
Combined the checks in logprob and the corresponding tests suites
Dhruvanshu-Joshi 7f6884b
Solving all merge conflicts
Dhruvanshu-Joshi 0e96cec
Reducing code redundancy
Dhruvanshu-Joshi a240b55
Reducing code redundancy
Dhruvanshu-Joshi 7647bd3
Updated the checks in logprob and the corresponding tests suites
Dhruvanshu-Joshi 86ca5cb
Updated and rectified the checks in logprob and the corresponding tes…
Dhruvanshu-Joshi e97b6de
Rectified test suites
Dhruvanshu-Joshi 54eb767
update code for checks to replace logprob with_logprob_helper
Dhruvanshu-Joshi 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 hidden or 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
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,158 @@ | ||
# Copyright 2023 The PyMC Developers | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
# MIT License | ||
# | ||
# Copyright (c) 2021-2022 aesara-devs | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
|
||
from typing import List, Optional | ||
|
||
import pytensor.tensor as pt | ||
|
||
from pytensor.graph.rewriting.basic import node_rewriter | ||
from pytensor.raise_op import CheckAndRaise, ExceptionType | ||
from pytensor.tensor.shape import SpecifyShape | ||
|
||
from pymc.logprob.abstract import MeasurableVariable, _logprob, logprob | ||
from pymc.logprob.rewriting import PreserveRVMappings, measurable_ir_rewrites_db | ||
from pymc.logprob.utils import ignore_logprob | ||
|
||
|
||
class MeasurableSpecifyShape(SpecifyShape): | ||
"""A placeholder used to specify a log-likelihood for a specify-shape sub-graph.""" | ||
|
||
|
||
MeasurableVariable.register(MeasurableSpecifyShape) | ||
|
||
|
||
@_logprob.register(MeasurableSpecifyShape) | ||
def logprob_specify_shape(op, values, inner_rv, *shapes, **kwargs): | ||
(value,) = values | ||
# transfer specify_shape from rv to value | ||
value = pt.specify_shape(value, shapes) | ||
return logprob(inner_rv, value) | ||
|
||
|
||
@node_rewriter([SpecifyShape]) | ||
def find_measurable_specify_shapes(fgraph, node) -> Optional[List[MeasurableSpecifyShape]]: | ||
r"""Finds `SpecifyShapeOp`\s for which a `logprob` can be computed.""" | ||
|
||
if isinstance(node.op, MeasurableSpecifyShape): | ||
return None # pragma: no cover | ||
|
||
rv_map_feature: Optional[PreserveRVMappings] = getattr(fgraph, "preserve_rv_mappings", None) | ||
|
||
if rv_map_feature is None: | ||
return None # pragma: no cover | ||
|
||
rv = node.outputs[0] | ||
|
||
base_rv, *shape = node.inputs | ||
|
||
if not ( | ||
base_rv.owner | ||
and isinstance(base_rv.owner.op, MeasurableVariable) | ||
and base_rv not in rv_map_feature.rv_values | ||
): | ||
return None # pragma: no cover | ||
|
||
new_op = MeasurableSpecifyShape() | ||
# Make base_var unmeasurable | ||
unmeasurable_base_rv = ignore_logprob(base_rv) | ||
new_rv = new_op.make_node(unmeasurable_base_rv, *shape).default_output() | ||
new_rv.name = rv.name | ||
|
||
return [new_rv] | ||
|
||
|
||
measurable_ir_rewrites_db.register( | ||
"find_measurable_specify_shapes", | ||
find_measurable_specify_shapes, | ||
"basic", | ||
"specify_shape", | ||
) | ||
|
||
|
||
class MeasurableCheckAndRaise(CheckAndRaise): | ||
"""A placeholder used to specify a log-likelihood for an assert sub-graph.""" | ||
|
||
|
||
MeasurableVariable.register(MeasurableCheckAndRaise) | ||
|
||
|
||
@_logprob.register(MeasurableCheckAndRaise) | ||
def logprob_assert(op, values, inner_rv, *assertion, **kwargs): | ||
(value,) = values | ||
# transfer assertion from rv to value | ||
value = op(assertion, value) | ||
return logprob(inner_rv, value) | ||
|
||
|
||
@node_rewriter([CheckAndRaise]) | ||
def find_measurable_asserts(fgraph, node) -> Optional[List[MeasurableCheckAndRaise]]: | ||
r"""Finds `AssertOp`\s for which a `logprob` can be computed.""" | ||
|
||
if isinstance(node.op, MeasurableCheckAndRaise): | ||
return None # pragma: no cover | ||
|
||
rv_map_feature: Optional[PreserveRVMappings] = getattr(fgraph, "preserve_rv_mappings", None) | ||
|
||
if rv_map_feature is None: | ||
return None # pragma: no cover | ||
|
||
rv = node.outputs[0] | ||
|
||
base_rv, *conds = node.inputs | ||
|
||
if not ( | ||
base_rv.owner | ||
and isinstance(base_rv.owner.op, MeasurableVariable) | ||
and base_rv not in rv_map_feature.rv_values | ||
): | ||
return None # pragma: no cover | ||
|
||
exception_type = ExceptionType() | ||
new_op = MeasurableCheckAndRaise(exc_type=exception_type) | ||
# Make base_var unmeasurable | ||
unmeasurable_base_rv = ignore_logprob(base_rv) | ||
new_rv = new_op.make_node(unmeasurable_base_rv, *conds).default_output() | ||
new_rv.name = rv.name | ||
|
||
return [new_rv] | ||
|
||
|
||
measurable_ir_rewrites_db.register( | ||
"find_measurable_asserts", | ||
find_measurable_asserts, | ||
"basic", | ||
"assert", | ||
) |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,107 @@ | ||
# Copyright 2023 The PyMC Developers | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
# MIT License | ||
# | ||
# Copyright (c) 2021-2022 aesara-devs | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
|
||
import inspect | ||
import os | ||
import re | ||
import sys | ||
|
||
import numpy as np | ||
import pytensor | ||
import pytensor.tensor as pt | ||
import pytest | ||
|
||
from pytensor.raise_op import Assert | ||
from scipy import stats | ||
|
||
from pymc.distributions import Dirichlet | ||
from pymc.logprob.joint_logprob import factorized_joint_logprob | ||
|
||
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) | ||
parentdir = os.path.dirname(currentdir) | ||
sys.path.insert(0, parentdir) | ||
from distributions.test_multivariate import dirichlet_logpdf | ||
|
||
|
||
def test_specify_shape_logprob(): | ||
# 1. Create graph using SpecifyShape | ||
# Use symbolic last dimension, so that SpecifyShape is not useless | ||
last_dim = pt.scalar(name="last_dim", dtype="int64") | ||
x_base = Dirichlet.dist(pt.ones((last_dim,)), shape=(5, last_dim)) | ||
x_base.name = "x" | ||
x_rv = pt.specify_shape(x_base, shape=(5, 3)) | ||
x_rv.name = "x" | ||
|
||
# 2. Request logp | ||
x_vv = x_rv.clone() | ||
[x_logp] = factorized_joint_logprob({x_rv: x_vv}).values() | ||
|
||
# 3. Test logp | ||
x_logp_fn = pytensor.function([last_dim, x_vv], x_logp) | ||
|
||
# 3.1 Test valid logp | ||
x_vv_test = stats.dirichlet(np.ones((3,))).rvs(size=(5,)) | ||
np.testing.assert_array_almost_equal( | ||
x_logp_fn(last_dim=3, x=x_vv_test), | ||
dirichlet_logpdf(x_vv_test, np.ones((3,))), | ||
) | ||
|
||
# 3.2 Test shape error | ||
x_vv_test_invalid = stats.dirichlet(np.ones((1,))).rvs(size=(5,)) | ||
with pytest.raises(TypeError, match=re.escape("not compatible with the data's ((5, 1))")): | ||
x_logp_fn(last_dim=1, x=x_vv_test_invalid) | ||
|
||
|
||
def test_assert_logprob(): | ||
rv = pt.random.normal() | ||
assert_op = Assert("Test assert") | ||
# Example: Add assert that rv must be positive | ||
assert_rv = assert_op(rv > 0, rv) | ||
assert_rv.name = "assert_rv" | ||
|
||
assert_vv = assert_rv.clone() | ||
assert_logp = factorized_joint_logprob({assert_rv: assert_vv})[assert_vv] | ||
|
||
# Check valid value is correct and doesn't raise | ||
# Since here the value to the rv satisfies the condition, no error is raised. | ||
valid_value = 3.0 | ||
with pytest.raises(AssertionError, match="Test assert"): | ||
assert_logp.eval({assert_vv: valid_value}) | ||
|
||
# Check invalid value | ||
# Since here the value to the rv is negative, an exception is raised as the condition is not met | ||
with pytest.raises(AssertionError, match="Test assert"): | ||
assert_logp.eval({assert_vv: -5.0}) |
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.
This should be removed. For running the test locally, just make sure you are running from the root folder I think