From a5746449a7efdd5fe46c59d1a3d75ee88af7cd2c Mon Sep 17 00:00:00 2001 From: hsm207 Date: Sat, 11 May 2019 05:06:25 +0800 Subject: [PATCH 1/6] Fix syntax error Fixes SyntaxError: positional argument follows keyword argument --- Chapter1_Introduction/Ch1_Introduction_TFP.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Chapter1_Introduction/Ch1_Introduction_TFP.ipynb b/Chapter1_Introduction/Ch1_Introduction_TFP.ipynb index 07aede9a..d4e20afc 100644 --- a/Chapter1_Introduction/Ch1_Introduction_TFP.ipynb +++ b/Chapter1_Introduction/Ch1_Introduction_TFP.ipynb @@ -1208,7 +1208,7 @@ "# Set the chain's start state.\n", "initial_chain_state = [\n", " tf.cast(tf.reduce_mean(count_data), tf.float32) * tf.ones([], dtype=tf.float32, name=\"init_lambda1\"),\n", - " tf.cast(tf.reduce_mean(count_data), tf.float32) * tf.ones([], dtype=tf.float32, name=\"init_lambda2\", tf.float32),\n", + " tf.cast(tf.reduce_mean(count_data), tf.float32) * tf.ones([], dtype=tf.float32, name=\"init_lambda2\"),\n", " 0.5 * tf.ones([], dtype=tf.float32, name=\"init_tau\"),\n", "]\n", "\n", From d8043bf8d500e1b79338c065efe4d9943936fe73 Mon Sep 17 00:00:00 2001 From: hsm207 Date: Sat, 11 May 2019 09:57:29 +0800 Subject: [PATCH 2/6] Fix typo dtype is an argument to tf.cast --- Chapter1_Introduction/Ch1_Introduction_TFP.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Chapter1_Introduction/Ch1_Introduction_TFP.ipynb b/Chapter1_Introduction/Ch1_Introduction_TFP.ipynb index d4e20afc..84ccbb59 100644 --- a/Chapter1_Introduction/Ch1_Introduction_TFP.ipynb +++ b/Chapter1_Introduction/Ch1_Introduction_TFP.ipynb @@ -1277,7 +1277,7 @@ " state_gradients_are_stopped=True),\n", " bijector=unconstraining_bijectors))\n", "\n", - "tau_samples = tf.floor(posterior_tau * tf.cast(tf.size(count_data)), tf.float32)\n", + "tau_samples = tf.floor(posterior_tau * tf.cast(tf.size(count_data), dtype=tf.float32))\n", "\n", "# tau_samples, lambda_1_samples, lambda_2_samples contain\n", "# N samples from the corresponding posterior distribution\n", From d39fa938267cb4b0fc09ec6474e2a94df78fbe09 Mon Sep 17 00:00:00 2001 From: hsm207 Date: Sat, 11 May 2019 10:07:17 +0800 Subject: [PATCH 3/6] Update make_simple_step_size_update_policy tfp.mcmc.make_simple_step_size_update_policy() requires num_adaptation_steps as an argument. I am not sure if the correct value is None but it gives the same results as the PyMC3 version of this notebook. --- Chapter1_Introduction/Ch1_Introduction_TFP.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Chapter1_Introduction/Ch1_Introduction_TFP.ipynb b/Chapter1_Introduction/Ch1_Introduction_TFP.ipynb index 84ccbb59..4e7d9a49 100644 --- a/Chapter1_Introduction/Ch1_Introduction_TFP.ipynb +++ b/Chapter1_Introduction/Ch1_Introduction_TFP.ipynb @@ -1273,7 +1273,7 @@ " target_log_prob_fn=unnormalized_log_posterior,\n", " num_leapfrog_steps=2,\n", " step_size=step_size,\n", - " step_size_update_fn=tfp.mcmc.make_simple_step_size_update_policy(),\n", + " step_size_update_fn=tfp.mcmc.make_simple_step_size_update_policy(num_adaptation_steps=None),\n", " state_gradients_are_stopped=True),\n", " bijector=unconstraining_bijectors))\n", "\n", From f93a081bcdd54809330d0d41fdc6635c4e91796c Mon Sep 17 00:00:00 2001 From: hsm207 Date: Sat, 11 May 2019 10:20:26 +0800 Subject: [PATCH 4/6] Fix deprecation warning Fixes: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version. Instructions for updating: Use tf.cast instead. --- Chapter1_Introduction/Ch1_Introduction_TFP.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Chapter1_Introduction/Ch1_Introduction_TFP.ipynb b/Chapter1_Introduction/Ch1_Introduction_TFP.ipynb index 4e7d9a49..81810cf1 100644 --- a/Chapter1_Introduction/Ch1_Introduction_TFP.ipynb +++ b/Chapter1_Introduction/Ch1_Introduction_TFP.ipynb @@ -1234,7 +1234,7 @@ "\n", " lambda_ = tf.gather(\n", " [lambda_1, lambda_2],\n", - " indices=tf.to_int32(tau * tf.cast(tf.size(count_data), tf.float32) <= tf.cast(tf.range(tf.size(count_data)), tf.float32)))\n", + " indices=tf.to_int32(tau * tf.cast(tf.size(count_data), tf.float32) <= tf.cast(tf.range(tf.size(count_data)), tf.float32), tf.int32))\n", " rv_observation = tfd.Poisson(rate=lambda_)\n", " \n", " return (\n", From 7803bc567e0365615dcebc1ca79ef68db108390b Mon Sep 17 00:00:00 2001 From: hsm207 Date: Sat, 11 May 2019 10:28:32 +0800 Subject: [PATCH 5/6] Fix typo Forgot to change tf.to_int32 to tf.cast --- Chapter1_Introduction/Ch1_Introduction_TFP.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Chapter1_Introduction/Ch1_Introduction_TFP.ipynb b/Chapter1_Introduction/Ch1_Introduction_TFP.ipynb index 81810cf1..9e67b92c 100644 --- a/Chapter1_Introduction/Ch1_Introduction_TFP.ipynb +++ b/Chapter1_Introduction/Ch1_Introduction_TFP.ipynb @@ -1234,7 +1234,7 @@ "\n", " lambda_ = tf.gather(\n", " [lambda_1, lambda_2],\n", - " indices=tf.to_int32(tau * tf.cast(tf.size(count_data), tf.float32) <= tf.cast(tf.range(tf.size(count_data)), tf.float32), tf.int32))\n", + " indices=tf.cast(tau * tf.cast(tf.size(count_data), tf.float32) <= tf.cast(tf.range(tf.size(count_data)), tf.float32), tf.int32))\n", " rv_observation = tfd.Poisson(rate=lambda_)\n", " \n", " return (\n", From 063220471efbbf09f9d050d1faaaeb1fa186cf4f Mon Sep 17 00:00:00 2001 From: hsm207 Date: Wed, 15 May 2019 20:27:08 +0800 Subject: [PATCH 6/6] Fix likelihood calculations to be compatible with N>1 The original implementation will throw a shape error when N>1 --- Chapter3_MCMC/Ch3_IntroMCMC_TFP.ipynb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Chapter3_MCMC/Ch3_IntroMCMC_TFP.ipynb b/Chapter3_MCMC/Ch3_IntroMCMC_TFP.ipynb index c1257def..5ccdf3df 100644 --- a/Chapter3_MCMC/Ch3_IntroMCMC_TFP.ipynb +++ b/Chapter3_MCMC/Ch3_IntroMCMC_TFP.ipynb @@ -461,8 +461,12 @@ "\n", "# plotting details.\n", "x_ = y_ = np.linspace(.01, 5, 100)\n", - "likelihood_x_ = evaluate(tfd.Poisson(rate=x_).prob(data_[:, 0]))\n", - "likelihood_y_ = evaluate(tfd.Poisson(rate=y_).prob(data_[:, 1]))\n", + "likelihood_x = tfd.Poisson(rate=tf.expand_dims(x_, 1)).prob(data_[:, 0])\n", + "likelihood_x = tf.reduce_prod(likelihood_x, axis=1)\n", + "likelihood_x_ = evaluate(likelihood_x)\n", + "likelihood_y = tfd.Poisson(rate=tf.expand_dims(y_, 1)).prob(data_[:, 1])\n", + "likelihood_y = tf.reduce_prod(likelihood_y, axis=1)\n", + "likelihood_y_ = evaluate(likelihood_y)\n", "L_ = evaluate(tf.matmul(tf.expand_dims(likelihood_x_, 1), \n", " tf.expand_dims(likelihood_y_, 0)))" ]