Skip to content

Make Basic tensorflow.distributions model#4

Open
sharanry wants to merge 1 commit into
pymc-devs:masterfrom
sharanry:tf_simpletest
Open

Make Basic tensorflow.distributions model#4
sharanry wants to merge 1 commit into
pymc-devs:masterfrom
sharanry:tf_simpletest

Conversation

@sharanry

Copy link
Copy Markdown

#2

@junpenglao

Copy link
Copy Markdown
Member

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

@sharanry

Copy link
Copy Markdown
Author

@junpenglao okay, on it!

@sharanry

Copy link
Copy Markdown
Author

@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?

@junpenglao

Copy link
Copy Markdown
Member

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.

@ferrine

ferrine commented Jan 29, 2018

Copy link
Copy Markdown
Member

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

@sharanry

sharanry commented Jan 29, 2018

Copy link
Copy Markdown
Author

@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

@twiecki

twiecki commented Jan 29, 2018

Copy link
Copy Markdown
Member

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).

@sharanry

Copy link
Copy Markdown
Author

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

@sharanry

sharanry commented Feb 5, 2018

Copy link
Copy Markdown
Author

@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?

@twiecki

twiecki commented Feb 5, 2018 via email

Copy link
Copy Markdown
Member

@sharanry

sharanry commented Feb 5, 2018

Copy link
Copy Markdown
Author

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

@twiecki

twiecki commented Feb 5, 2018

Copy link
Copy Markdown
Member

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.

@sharanry

Copy link
Copy Markdown
Author
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

@twiecki

twiecki commented Feb 13, 2018

Copy link
Copy Markdown
Member

@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): pymc-devs/pymc@98a2e03

@ferrine

ferrine commented Feb 13, 2018

Copy link
Copy Markdown
Member

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

ferrine commented Feb 13, 2018

Copy link
Copy Markdown
Member

personally I just wait for edward 2.0 to release

@sharanry

Copy link
Copy Markdown
Author

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

@fonnesbeck

Copy link
Copy Markdown
Member

@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.

@junpenglao

Copy link
Copy Markdown
Member

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

@twiecki

twiecki commented Mar 25, 2018

Copy link
Copy Markdown
Member

@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.

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

Successfully merging this pull request may close these issues.

5 participants