Skip to content

FR: Separate compile/run methods for pathfinder #488

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

Open
mike-lawrence opened this issue May 28, 2025 · 0 comments
Open

FR: Separate compile/run methods for pathfinder #488

mike-lawrence opened this issue May 28, 2025 · 0 comments

Comments

@mike-lawrence
Copy link

I'm using Pathfinder to generate inits for a subsequent run of NUTS, and for each of my N NUTS chains, I run a separate multi-path pathfinder:

n_chains = int(os.cpu_count()/2)
init_vals = []
seed = 0
while len(init_vals) < n_chains:
	try:
		seed += 1
		with inference_model:
			pathfinder_trace = pmx.fit(
				method = "pathfinder"
				, random_seed = seed
				, num_paths = 5 # more than 5 causes a crash!
				, num_draws_per_path = 1000
				, importance_sampling = 'psis'
				, num_draws = 1
			)
		pathfinder_trace.posterior = pathfinder_trace.posterior.squeeze("chain", drop=True)
		point = {
			var: pathfinder_trace.posterior[var].isel(draw=0).values
			for var in pathfinder_trace.posterior.data_vars
		}
		init_vals.append(point)
	except Exception as e:
		print(f"Error occurred: {e}")
		print("Retrying with a different seed...")

# now we can use the pathfinder trace to initialize the chains
with inference_model:
	posterior_trace = pm.sample(
		chains = n_chains
		, cores = n_chains
		, initvals = init_vals
	)

However, with the current API, there is an unnecessary re-compilation of the model for every run of pathfinder, so it would be useful to have separate compile() and run() methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant