Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bilby
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
lscsoft
bilby
Commits
1be08c59
Commit
1be08c59
authored
6 years ago
by
Matthew Pitkin
Committed by
Rhys Green
6 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Fixes to PyMC3 autoinitialisation of NUTS step method
parent
a4467c22
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!299
Fixing some argument bugs in pymc3.py.
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
bilby/core/sampler/pymc3.py
+35
-16
35 additions, 16 deletions
bilby/core/sampler/pymc3.py
with
35 additions
and
16 deletions
bilby/core/sampler/pymc3.py
+
35
−
16
View file @
1be08c59
...
...
@@ -424,9 +424,21 @@ class Pymc3(MCMCSampler):
# set the prior
self
.
set_prior
()
# if a custom log_likelihood function requires a `sampler` argument
# then use that log_likelihood function, with the assumption that it
# takes in a Pymc3 Sampler, with a pymc3_model attribute, and defines
# the likelihood within that context manager
likeargs
=
infer_args_from_method
(
self
.
likelihood
.
log_likelihood
)
if
'
sampler
'
in
likeargs
:
self
.
likelihood
.
log_likelihood
(
sampler
=
self
)
else
:
# set the likelihood function from predefined functions
self
.
set_likelihood
()
# get the step method keyword arguments
step_kwargs
=
self
.
kwargs
.
pop
(
'
step_kwargs
'
)
nuts_kwargs
=
self
.
kwargs
.
pop
(
'
nuts_kwargs
'
)
methodslist
=
[]
# set the step method
if
isinstance
(
self
.
step_method
,
(
dict
,
OrderedDict
)):
...
...
@@ -438,12 +450,15 @@ class Pymc3(MCMCSampler):
if
isinstance
(
self
.
step_method
[
key
],
list
):
for
sms
in
self
.
step_method
[
key
]:
curmethod
=
sms
.
lower
()
methodslist
.
append
(
curmethod
)
args
=
{}
if
curmethod
==
'
nuts
'
:
if
nuts_kwargs
is
not
None
:
args
=
nuts_kwargs
elif
step_kwargs
is
not
None
:
args
=
step_kwargs
.
get
(
'
nuts
'
,
{})
args
=
step_kwargs
.
pop
(
'
nuts
'
,
{})
# add values into nuts_kwargs
nuts_kwargs
=
args
else
:
args
=
{}
else
:
...
...
@@ -454,12 +469,15 @@ class Pymc3(MCMCSampler):
self
.
kwargs
[
'
step
'
].
append
(
pymc3
.
__dict__
[
step_methods
[
curmethod
]](
vars
=
[
self
.
pymc3_priors
[
key
]],
**
args
))
else
:
curmethod
=
self
.
step_method
[
key
].
lower
()
methodslist
.
append
(
curmethod
)
args
=
{}
if
curmethod
==
'
nuts
'
:
if
nuts_kwargs
is
not
None
:
args
=
nuts_kwargs
elif
step_kwargs
is
not
None
:
args
=
step_kwargs
.
get
(
'
nuts
'
,
{})
args
=
step_kwargs
.
pop
(
'
nuts
'
,
{})
# add values into nuts_kwargs
nuts_kwargs
=
args
else
:
args
=
{}
else
:
...
...
@@ -475,47 +493,48 @@ class Pymc3(MCMCSampler):
compound
=
[]
for
sms
in
self
.
step_method
:
curmethod
=
sms
.
lower
()
methodslist
.
append
(
curmethod
)
args
=
{}
if
curmethod
==
'
nuts
'
:
if
nuts_kwargs
is
not
None
:
args
=
nuts_kwargs
elif
step_kwargs
is
not
None
:
args
=
step_kwargs
.
get
(
'
nuts
'
,
{})
args
=
step_kwargs
.
pop
(
'
nuts
'
,
{})
# add values into nuts_kwargs
nuts_kwargs
=
args
else
:
args
=
{}
else
:
args
=
step_kwargs
.
get
(
curmethod
,
{})
compound
.
append
(
pymc3
.
__dict__
[
step_methods
[
curmethod
]](
**
args
))
self
.
kwargs
[
'
step
'
]
=
compound
else
:
self
.
kwargs
[
'
step
'
]
=
None
if
self
.
step_method
is
not
None
:
curmethod
=
self
.
step_method
.
lower
()
methodslist
.
append
(
curmethod
)
args
=
{}
if
curmethod
==
'
nuts
'
:
if
nuts_kwargs
is
not
None
:
args
=
nuts_kwargs
elif
step_kwargs
is
not
None
:
args
=
step_kwargs
.
get
(
'
nuts
'
,
{})
args
=
step_kwargs
.
pop
(
'
nuts
'
,
{})
# add values into nuts_kwargs
nuts_kwargs
=
args
else
:
args
=
{}
else
:
args
=
step_kwargs
.
get
(
curmethod
,
{})
self
.
kwargs
[
'
step
'
]
=
pymc3
.
__dict__
[
step_methods
[
curmethod
]](
**
args
)
else
:
# re-add step_kwargs and nuts_kwargs if no step methods are set
self
.
kwargs
[
'
nuts_kwargs
'
]
=
nuts_kwargs
# re-add step_kwargs if no step methods are set
self
.
kwargs
[
'
step_kwargs
'
]
=
step_kwargs
# if a custom log_likelihood function requires a `sampler` argument
# then use that log_likelihood function, with the assumption that it
# takes in a Pymc3 Sampler, with a pymc3_model attribute, and defines
# the likelihood within that context manager
likeargs
=
infer_args_from_method
(
self
.
likelihood
.
log_likelihood
)
if
'
sampler
'
in
likeargs
:
self
.
likelihood
.
log_likelihood
(
sampler
=
self
)
else
:
# set the likelihood function from predefined functions
self
.
set_likelihood
()
# check whether only NUTS step method has been assigned
if
np
.
all
([
sm
.
lower
()
==
'
nuts
'
for
sm
in
methodslist
]):
# in this case we can let PyMC3 autoinitialise NUTS, so remove the step methods and re-add nuts_kwargs
self
.
kwargs
[
'
step
'
]
=
None
self
.
kwargs
[
'
nuts_kwargs
'
]
=
nuts_kwargs
with
self
.
pymc3_model
:
# perform the sampling
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment