Comments (4)
Thanks! I expect that what's happening here is that all the calls to numpy.random
are producing float64
arrays which can be upcast to float128
, but won't be downcast to float32
. This means that every time a proposal is made, the coordinates will end up being cast to the higher precision. It would take some work to be explicit about the random dtype throughout the whole code base, so I'm not sure there's much I'd want to change about emcee's operation here. For your use case, would it be sufficient to just downcast the dtype at the top of lhood
before evaluating your model on the GPU?
from emcee.
Okay, having fiddled with this a little more, I found a very simple fix, at least in the case of the Goodman & Weare WalkMove
class. I had misunderstood why the shape was different after the first call to the likelihood function -- I wrongly assumed that the data had been reinterpreted to an array half the size with np.float64
elements, when the size change is actually just because of how RedBlueMove
fundamentally works. Realizing that, I was able to modify my MWE to the following, which runs without error. (I realize downcasting was an option, too, but, for a performance-critical application, you'd prefer to eliminate the typecast and work with shallow copies when possible.)
import emcee
import numpy as np
sigma = 1.
def lhood(p):
assert p.dtype == np.float32
return -0.5 * np.sum(p**2, axis=1) / sigma**2
nwalkers, ndim = 16, 4
class MyWalkMove(emcee.moves.WalkMove):
def get_proposal(self, s, c, random):
c = np.concatenate(c, axis=0)
Ns, Nc = len(s), len(c)
ndim = s.shape[1]
q = np.empty_like(s) # <-- changed here
s0 = Nc if self.s is None else self.s
for i in range(Ns):
inds = random.choice(Nc, s0, replace=False)
cov = np.atleast_2d(np.cov(c[inds], rowvar=0))
q[i] = random.multivariate_normal(s[i], cov)
return q, np.zeros(Ns)
sampler = emcee.EnsembleSampler(nwalkers, ndim, lhood,
vectorize=True, moves=MyWalkMove())
p0 = np.random.randn(nwalkers, ndim).astype(np.float32)
sampler.run_mcmc(p0, 100)
I don't think this breaks anything else in the code, though I haven't run any other tests. Happy to make it a pull request if you prefer, though it's literally a one-line fix.
from emcee.
Oh nice! Please do open a PR with that change. One liners are the best! :D Thanks.
from emcee.
Addressed (for two move types, at least) by PR #484.
from emcee.
Related Issues (20)
- HDF backend file size
- Multiprocessing Pool scaling not as expected HOT 4
- [3.1.3] test failure with scipy 1.10 HOT 3
- Progress bar HOT 1
- Investigate the use of optree for structured parameter sets
- Issue on page /index.html HOT 1
- Add set_description to `pbar._NoOpPBar` HOT 1
- In emcee.EnsembleSampler threads command is not working
- JAX implementation of emcee HOT 3
- issue with emcee installation HOT 3
- (solved) issue with sampler stalling with multiprocessing
- Saving non-arrayifiable data at each likelihood evaluation (either to disk or in memory) HOT 4
- Incompatible with numpy 2.0.0rc1 HOT 3
- New release to prepare for numpy 2 HOT 5
- Trying to use Blobs with the vectorize=True keyword returns a ValueError("incompatible input dimensions")
- emcee 3.1.5 incompatible with numpy < 1.25.0
- Using multithreading from tutorial doesnt seem to work
- Modified Walk Moves
- Issues with Parameter Extraction from Emcee
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from emcee.