CamDavidsonPilon / Probabilistic-Programming-and-Bayesian-Methods-for-Hackers

aka "Bayesian Methods for Hackers": An introduction to Bayesian methods + probabilistic programming with a computation/understanding-first, mathematics-second point of view. All in pure Python ;)
http://camdavidsonpilon.github.io/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/
MIT License
26.55k stars 7.85k forks source link

Added Tensorflow Probability Example #399

Closed matthew-mcateer closed 5 years ago

matthew-mcateer commented 5 years ago

This new added Jupyter Notebook does away with mentions of PyMC2, PyMC3, and Theano, and uses Google's Tensorflow probability for solving the same problems with the same concepts. There have also been increases to the resolutions of the matplotlib plots to show more detail on retina screens.

CamDavidsonPilon commented 5 years ago

Some notes:

  1. I'll add something to the README as soon as this is merged.
  2. TIL and ❤️ InlineBackend.figure_format = 'retina'
  3. Locally, I commented out assert tf.test.is_gpu_available() - things seem to work, but do we need to require this for users? I'd prefer the easier-to-install-and-learn option here.

Chapter 1 runs great 👍

CamDavidsonPilon commented 5 years ago

Chapter 2:

Example: Cheating among students has some print errors - however even after fixing them, I still can't get the example to run due to an eval() error.

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-51-4e14523833b7> in <module>()
      2     observed_proportion_val = observed_proportion_calc(t_a=true_answers,
      3                                                        fc=first_coin_flips,
----> 4                                                        sc=second_coin_flips)
      5     print(observed_proportion_val)

<ipython-input-50-f15116f73129> in observed_proportion_calc(t_a, fc, sc)
     13     """
     14     observed = fc * t_a + (1 - fc) * sc
---> 15     observed_proportion = np.sum(observed.eval()) / float(N)
     16 
     17     return observed_proportion

AttributeError: 'numpy.ndarray' object has no attribute 'eval'

challenger_data = np.genfromtxt("challenger_data.csv", skip_header=1 -> challenger_data = np.genfromtxt("data/challenger_data.csv", skip_header=1,


Does this need google.colab as a dependency?


Locally, I am running 0.3.0 tensorflow. The API for Normal in my version is loc, scale, and it looks like it is on nightly tfp. Can you update to that version?


I'm getting a step_size error when setting up the chain:

ValueError: Variable step_size already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at:

  File "<ipython-input-36-5a0b0de50bb6>", line 31, in <module>
    trainable=False)
  File "/Users/camerondavidson-pilon/.virtualenvs/data3/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2910, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "/Users/camerondavidson-pilon/.virtualenvs/data3/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2850, in run_ast_nodes
    if self.run_code(code, result):

Edit:

Seeing your new comments now, okay 👌

matthew-mcateer commented 5 years ago

@CamDavidsonPilon Glad to see those preliminary requests were to your liking.

matthew-mcateer commented 5 years ago

With a lot of the notebooks, it turned out that I had accidentally sent pull requests for the wrong version (which is apparently something that can happen when you're working with both local jupyter and online Google Colab).

matthew-mcateer commented 5 years ago

With two of the notebooks, I added the pull requests prematurely, because within these notebooks I include links to Github at the top of the notebook, and using the github link as a placeholder (i.e., linking directly to the notebook in the PR), was the most effective way we had at making sure a user could conceivably view any version of the notebook they wanted, whether that was a local copy, a copy in Github, or a copy in Colab.

matthew-mcateer commented 5 years ago

So with that in mind, we've seen a lot of the errors in the current pull request. We're going to be uploading much more stable versions in the near future (i.e., by week's end). We'll alert you when we've done so, @CamDavidsonPilon . It turned out the "Complete" notebooks weren't as complete in PR 13ec2c5 as we initially thought. This should be fixed soon and we'll make those PRs with the notebooks that have been vetted and tested in multiple environments.

matthew-mcateer commented 5 years ago

Also, @CamDavidsonPilon, in these notebooks, in additon to the replacement o PyMC & SciPy with TFP, we're also working on replacing a majority of the Numpy code with Tensorflow and TFP as well. We just figured we'd update everyone on the scope change.

matthew-mcateer commented 5 years ago

Hey @CamDavidsonPilon , Here's a new version of the PR. We decided that it would probably be easier on you if just one chapter was committed at a time. With that in mind, here is a revised chapter 1 (which now includes some of the best practices for TFP semantics, AND also runs in both Graph and Eager mode for Tensorflow). A lot of the plotting has been updated as well.

CamDavidsonPilon commented 5 years ago

Hey @matthew-mcateer, looks great! Runs fine for me. Code looks good too.

Merging the first chapter - excited to see the others next!