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

PPC with Minibatch ADVI #2190

Closed
allentran opened this Issue May 16, 2017 · 8 comments

Comments

Projects
None yet
5 participants
@allentran
Copy link

allentran commented May 16, 2017

I have a question about doing the PPC with minibatch ADVI.

The example here (https://pymc-devs.github.io/pymc3/notebooks/bayesian_neural_network_opvi-advi.html) sets up a shared Theano variable ann_input, compiles and fits the model and then does the sampling on some new input by using the set_value() method on ann_input.

Using minibatch, you pass the model a pm.generator(.). How do I set the value on this object so I can do the PPC?

@junpenglao

This comment has been minimized.

Copy link
Member

junpenglao commented May 30, 2017

Hi @allentran , sorry about the late reply. @ferrine is working on the new interface and docs of minibatch ADVI and will be released soon.
meanwhile, you can sample from the fitted model, which returns a trace just like the ones from MCMC samplers. You can then do PPC using this (fake) trace.

@allentran

This comment has been minimized.

Copy link
Author

allentran commented May 31, 2017

OK, just to confirm, no way to access the (much faster method) Theano shared variable/compile a separate function.

@Pflip

This comment has been minimized.

Copy link

Pflip commented Jun 28, 2017

Are there, by chance, any updates on this issue?

@junpenglao

This comment has been minimized.

Copy link
Member

junpenglao commented Jun 28, 2017

@Pflip Yes, you can find some information here: http://pymc-devs.github.io/pymc3/notebooks/variational_api_quickstart.html

I will close this for now. If you have specific question of the usage, please post on our discourse: https://discourse.pymc.io/

@junpenglao junpenglao closed this Jun 28, 2017

@ferrine

This comment has been minimized.

Copy link
Member

ferrine commented Jun 28, 2017

Hi! Sorry for no response. The only chance to sample ppc after minibatch inference is the following:

  1. Initialize model with shared data, not minibatch.
  2. Create minibatches for inference using pm.Minibatch
  3. Use more_replacements argument in fit like approx = pm.fit(more_replacements={full_x: minibatch_x, full_y:minibatch_y}, ...)

After that you can get trace from approximation and pass it to sample_ppc. Note that it will perform plc for examples you defined on step 1, not minibatches. There your are free to set any data your like.

@allentran

This comment has been minimized.

Copy link
Author

allentran commented Jul 3, 2017

Cool, this looks like what I was after, will try.

@hugothchu

This comment has been minimized.

Copy link

hugothchu commented Jul 31, 2017

Hi, I converted the Dirichlet Process Mixture Model for Density Estimation Example for Minibatch ADVI, and attempted PPC on it.

DPM_output = theano.shared(y_train)
minibatch_y = pm.Minibatch(y_train, batch_size=500)

with pm.Model() as DPM:
	K = 50
	alpha = pm.Gamma('alpha', 1.0, 1.0)
	beta = pm.Beta('beta', 1.0, alpha, shape=K)
	w = pm.Deterministic('w', stick_breaking(beta))
	tau = pm.Gamma('tau', 1.0, 1.0, shape=K)
	lambda_ = pm.Uniform('lambda', 0, 5, shape=K)
	mu = pm.Normal('mu', 0, tau=lambda_ * tau, shape=K)
	obs = pm.NormalMixture('obs', w, mu, tau=lambda_ * tau, observed=DPM_output)

	if os.path.isfile(approx_store):
		with open(approx_store, 'rb') as handle:
			advifit = pickle.load(handle)
	else:
		advifit = pm.fit(100000, method='advi', more_replacements={DPM_output: minibatch_y}, callbacks=[pm.callbacks.CheckParametersConvergence()])

		with open(approx_store, "wb") as handle:
			pickle.dump(advifit, handle, protocol=pickle.HIGHEST_PROTOCOL)

	approx = advifit.sample(10000)
	ppc = pm.sample_ppc(approx, size=100, progressbar=True)

However, I got the following error:

Traceback (most recent call last):
  File "PyMC3_DPM.py", line 63, in <module>
    ppc = pm.sample_ppc(approx, size=100, progressbar=True)
  File "/usr/local/lib/python3.5/dist-packages/pymc3/sampling.py", line 539, in sample_ppc
    vals = var.distribution.random(point=param, size=size)
  File "/usr/local/lib/python3.5/dist-packages/pymc3/distributions/mixture.py", line 128, in random
    w = draw_values([self.w], point=point)[0]
  File "/usr/local/lib/python3.5/dist-packages/pymc3/distributions/distribution.py", line 201, in draw_values
    givens[name] = (node, _draw_value(node, point=point))
  File "/usr/local/lib/python3.5/dist-packages/pymc3/distributions/distribution.py", line 268, in _draw_value
    func = _compile_theano_function(param, variables)
  File "/usr/local/lib/python3.5/dist-packages/pymc3/memoize.py", line 16, in memoizer
    cache[key] = obj(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/pymc3/distributions/distribution.py", line 230, in _compile_theano_function
    allow_input_downcast=True)
  File "/usr/local/lib/python3.5/dist-packages/theano/compile/function.py", line 326, in function
    output_keys=output_keys)
  File "/usr/local/lib/python3.5/dist-packages/theano/compile/pfunc.py", line 486, in pfunc
    output_keys=output_keys)
  File "/usr/local/lib/python3.5/dist-packages/theano/compile/function_module.py", line 1794, in orig_function
    output_keys=output_keys).create(
  File "/usr/local/lib/python3.5/dist-packages/theano/compile/function_module.py", line 1446, in __init__
    accept_inplace)
  File "/usr/local/lib/python3.5/dist-packages/theano/compile/function_module.py", line 177, in std_fgraph
    update_mapping=update_mapping)
  File "/usr/local/lib/python3.5/dist-packages/theano/gof/fg.py", line 180, in __init__
    self.__import_r__(output, reason="init")
  File "/usr/local/lib/python3.5/dist-packages/theano/gof/fg.py", line 361, in __import_r__
    raise MissingInputError("Undeclared input", variable=variable)
theano.gof.fg.MissingInputError: Undeclared input

Any ideas what this means? Sorry if it has already been asked before.

Thanks,
Hugo

@ferrine

This comment has been minimized.

Copy link
Member

ferrine commented Aug 1, 2017

I suggest you pickling not advifit but approx.shaped_params

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment