Skip to content

Implement Model.debug() helper #6634

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 2 commits into from
Mar 31, 2023
Merged

Conversation

ricardoV94
Copy link
Member

@ricardoV94 ricardoV94 commented Mar 30, 2023

Example:

import pymc as pm

with pm.Model() as m:
    x = pm.Normal("x", [1, -1, 1])
    y = pm.Normal("y", mu=x, sigma=x * 2)
m.debug()

Outputs:

point={'x': array([ 1., -1.,  1.]), 'y': array([ 1., -1.,  1.])}

The variable y has the following parameters:
0: x [id A] <TensorType(float64, (3,))>
1: Elemwise{mul,no_inplace} [id B] <TensorType(float64, (3,))>
 |TensorConstant{(1,) of 2.0} [id C] <TensorType(float64, (1,))>
 |x [id A] <TensorType(float64, (3,))>
The parameters evaluate to:
0: [ 1. -1.  1.]
1: [ 2. -2.  2.]
This does not respect one of the following constraints: sigma > 0

Another example:

import pymc as pm

with pm.Model() as m:
    theta = pm.Uniform('theta', lower=0, upper=1)
    y = pm.Uniform('y', lower=0, upper=theta, observed=[0.49, 0.27, 0.53, 0.19])
m.debug()    
point={'theta_interval__': array(0.)}

The variable y has the following parameters:
0: TensorConstant{0.0} [id A] <TensorType(float64, ())>
1: Elemwise{sigmoid,no_inplace} [id B] <TensorType(float64, ())> 'theta'
 |theta_interval__ [id C] <TensorType(float64, ())>
The parameters evaluate to:
0: 0.0
1: 0.5
Some of the observed values of variable y are associated with a non-finite logp:
 value = 0.53 -> logp = -inf

Closes #4205

@ricardoV94 ricardoV94 marked this pull request as draft March 30, 2023 14:21
@ricardoV94 ricardoV94 force-pushed the model_debug branch 2 times, most recently from 265019c to e7e96bf Compare March 30, 2023 14:31
@ricardoV94 ricardoV94 marked this pull request as ready for review March 30, 2023 14:32
@codecov
Copy link

codecov bot commented Mar 30, 2023

Codecov Report

Merging #6634 (d54e826) into main (3f2a1da) will increase coverage by 0.01%.
The diff coverage is 95.12%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #6634      +/-   ##
==========================================
+ Coverage   91.94%   91.96%   +0.01%     
==========================================
  Files          94       94              
  Lines       15845    15923      +78     
==========================================
+ Hits        14569    14643      +74     
- Misses       1276     1280       +4     
Impacted Files Coverage Δ
pymc/model.py 90.03% <94.87%> (+0.51%) ⬆️
pymc/distributions/continuous.py 97.70% <100.00%> (ø)

The default use of `check_parameters` indicates that an expression can be replaced by -inf, if the constraints aren't met. Instead, if `can_be_replaced_by_ninf=False`, sampling would fail for negative tau/sigma.

To avoid this, the conversion now returns the right value for positive tau or sigma, but negative images if the inputs were negative. The methods that then validate the paramters (such as logp, logcdf, random), can later catch this.
Copy link
Member

@michaelosthege michaelosthege left a comment

Choose a reason for hiding this comment

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

Very cool!!

@ricardoV94 ricardoV94 changed the title Add model debug helper Implement Model.debug() helper Mar 30, 2023
@ricardoV94 ricardoV94 marked this pull request as draft March 31, 2023 07:23
@ricardoV94 ricardoV94 marked this pull request as ready for review March 31, 2023 07:38
@ricardoV94
Copy link
Member Author

ricardoV94 commented Mar 31, 2023

I refined it to only show the value-evaluation pairs that lead to non-finite results.

@ricardoV94 ricardoV94 merged commit 6404805 into pymc-devs:main Mar 31, 2023
@ricardoV94 ricardoV94 deleted the model_debug branch April 5, 2023 07:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve model debugging
2 participants