pymc4_prototypes icon indicating copy to clipboard operation
pymc4_prototypes copied to clipboard

Make Basic tensorflow.distributions model

Open sharanry opened this issue 7 years ago • 20 comments

#2

sharanry avatar Jan 29 '18 14:01 sharanry

what you are doing is sampling from the prior. Could you try to improve it to include some example using the HMC from tensorflow?

junpenglao avatar Jan 29 '18 14:01 junpenglao

@junpenglao okay, on it!

sharanry avatar Jan 29 '18 15:01 sharanry

@junpenglao I am unable to find a HMC distribution in tensorflow. But I did find couple of implementations of it using tensorflow on github. Its currently a feature request in tensorflow's issues.

What do you suggest I do?

sharanry avatar Jan 29 '18 16:01 sharanry

HMC is not a distribution, it is the Hamiltonian Monte Carlo sampler. You can find it in https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/bayesflow/python/ops/hmc_impl.py

You can check out their test case to have some inspiration: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/bayesflow/python/kernel_tests/hmc_test.py https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/bayesflow/python/kernel_tests/metropolis_hastings_test.py https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/bayesflow/python/kernel_tests/monte_carlo_test.py

I think it might also help if you open a post on discourse and write down how you aim to implement it and your thought process so we can discuss.

junpenglao avatar Jan 29 '18 16:01 junpenglao

I wonder if we can use tensorflow dataset and propagate gradient through it. To my knowledge there is an option to change input source, that's what we need for bayesian stuff

ferrine avatar Jan 29 '18 16:01 ferrine

@junpenglao I now got a basic understanding of what HMC is intuitively here.

Could I use your implementation in pyro here as a guideline?

I will be reading through the tensorflow tests tests. I will open a post on discourse once I have basic idea in mind.

TIA

sharanry avatar Jan 29 '18 18:01 sharanry

I think this would be much more powerful if you used a sampler from pymc3 (like HMC or NUTS, these specifically only require logp and dlogp).

twiecki avatar Jan 29 '18 19:01 twiecki

I have opened a post here. https://discourse.pymc.io/t/hmc-sampling-in-tensorflow/797

sharanry avatar Jan 30 '18 08:01 sharanry

@twiecki Are you suggesting to use the models from tensorflow and the sampling from pymc? I am unable to find posterior sampling functions in tensorflow? Do you suggest I implement HMC or NUTS manually as a test?

sharanry avatar Feb 05 '18 10:02 sharanry

there first one. make tensorflow distributions work with the HMC implementation of pymc3. note that that implementation is python only and should just require logp and dlogp.

On Feb 5, 2018 11:22 AM, "Sharan Yalburgi" [email protected] wrote:

@twiecki https://github.com/twiecki Are you suggesting to use the models from tensorflow and the sampling from pymc? I am unable to find posterior sampling functions in tensorflow? Do you suggest I implement HMC or NUTS manually as a test?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/pymc-devs/pymc4_prototypes/pull/4#issuecomment-363041951, or mute the thread https://github.com/notifications/unsubscribe-auth/AApJmN2lSBD-DCpKrvmGwTBggdu5wl0Pks5tRtZogaJpZM4RwrbA .

twiecki avatar Feb 05 '18 10:02 twiecki

@twiecki The pymc samplers still require theano variables as input? Is there any way around this?

sharanry avatar Feb 05 '18 12:02 sharanry

It probably does require some adaptation. You could copy&paste the code over (HMC is pretty self-contained I think). In general, a good first step is probably to just get the logp and dlogp of a simple model. Then see how that could be passed into the HMC class in pymc3. That's really the work, figuring out how to combine the two.

twiecki avatar Feb 05 '18 16:02 twiecki

class Norm():
  def __init__(self, mean=0., sd=1.):
    self.mean = tf.constant(mean)
    self.sd = tf.constant(sd)
    self.n = tf.distributions.Normal(loc=self.mean, scale=self.sd)
    
  def sample(self, sample_shape=(), ):    
    out = self.n.sample(sample_shape)
    init = tf.global_variables_initializer()
    with tf.Session().as_default() as sess:
      init.run()
      return out.eval()
    
  def logp(self, sample_shape=(), value=None):
    if value == None:
      value = self.sample(sample_shape)
    logp = self.n.log_prob(value)
    init = tf.global_variables_initializer()
    with tf.Session().as_default() as sess:
      init.run()
      return logp.eval()
  
  def dlogp(self, sample_shape=(), value=None):
    if value == None:
      value = self.sample(sample_shape)
    
    dlogp = tf.gradients(self.n.log_prob(value), [self.mean, self.sd])
    
    init = tf.global_variables_initializer()
    with tf.Session().as_default() as sess:
      init.run()
      return sess.run(dlogp)

Am I on the right track here? @twiecki

sharanry avatar Feb 11 '18 07:02 sharanry

@sharanry Yes, that looks like a great start. You then want to be able to evaluate the logp and dlogp for changing values of mu and sd (which are suggested by the sampler). You can see here for some code where we built a wrapper for edward (which probably doesn't work anymore with newer versions): https://github.com/pymc-devs/pymc3/commit/98a2e038d2e83d94556a69d6e169e58f25024528

twiecki avatar Feb 13 '18 14:02 twiecki

I don't like the proposed architecture still having no alternatives. The main drawback of this approach is return sess.run(dlogp|sample|logp) that is not symbolic.

ferrine avatar Feb 13 '18 14:02 ferrine

personally I just wait for edward 2.0 to release

ferrine avatar Feb 13 '18 14:02 ferrine

@twiecki Any specific reason why edward support was removed on PyMC3?

sharanry avatar Feb 22 '18 17:02 sharanry

@sharanry the team decided that extensions for third-party packages ought not to be part of the main repository, mainly due to maintenance overhead. Individuals are welcome to extend PyMC3 any way they wish, but it should be a separate project.

fonnesbeck avatar Feb 22 '18 18:02 fonnesbeck

It was removed also because they change the API and nobody is able to make it work at the time.

junpenglao avatar Feb 22 '18 18:02 junpenglao

@sharanry GSoC applications are now open if you planned to submit something. There are some quite interesting developments in regards to PyMC4 and TF probability / Edward 2. Would be great to have you involved.

twiecki avatar Mar 25 '18 21:03 twiecki