Skip to content

Change tune method to not act inplace plus more sampler stats #3732

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 4 commits into from
Dec 16, 2019

Conversation

michaelosthege
Copy link
Member

@michaelosthege michaelosthege commented Dec 15, 2019

By acting inplace, the tune method in metropolis.py modified that scaling of all parallel DEMetropolis chains at the same time.

When DEMetropolis is used with cores = 1, the step method instance is shallow-copied for each chain. The shallow-copying is memory-friendly (important for b/c of the model graph), but the shallow copies also share the reference to the original scaling attribute.

The following example shows why the in-place tune implementation is problematic:

import copy
import numpy

class Chain:
    def __init__(self):
        self.scaling = numpy.array([1,2,3], dtype=float)

chain1 = Chain()
chain2 = copy.copy(chain1)

print(chain1 is chain2)
#> False
print(chain1.scaling is chain2.scaling)
#> True

# In-place operation as previously done by tune method:
chain1.scaling *= 1.1

print(chain2.scaling)
#> [1.1, 2.2, 3.3]

Changes

  • added a regression test that checks the tune method to not act inplace
  • changed tune method to return a new array instead of returning the modified original
  • added accepted and scaling sampler stats for Metropolis and DEMetropolis because they were useful for testing

closes #3731

@michaelosthege michaelosthege changed the title Change Metropolis tune method to not act inplace Change tune method to not act inplace plus more sampler stats Dec 15, 2019
@ColCarroll
Copy link
Member

LGTM too. I restarted the build after clearing the caches on travis.

@codecov
Copy link

codecov bot commented Dec 16, 2019

Codecov Report

Merging #3732 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3732      +/-   ##
==========================================
+ Coverage   89.93%   89.93%   +<.01%     
==========================================
  Files         134      134              
  Lines       20422    20429       +7     
==========================================
+ Hits        18366    18373       +7     
  Misses       2056     2056
Impacted Files Coverage Δ
pymc3/tests/test_tuning.py 100% <100%> (ø) ⬆️
pymc3/step_methods/metropolis.py 86.79% <100%> (ø) ⬆️

@junpenglao junpenglao merged commit 9f5f676 into pymc-devs:master Dec 16, 2019
sthagen added a commit to sthagen/pymc-devs-pymc that referenced this pull request Dec 16, 2019
Change tune method to not act inplace plus more sampler stats (pymc-devs#3732)
@michaelosthege michaelosthege deleted the no-inplace-tuning branch August 7, 2021 11:22
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.

DEMetropolis chains share tuned hyperparameter (with cores=1)
3 participants