Hi there, I'm encountering an issue in Chapter 06 at Code Block 6.14, when it comes to the section of the code where we're using generate_gam_ar_latent()
:
mcmc_samples, sampler_stats = run_mcmc(
1000, gam_with_latent_ar, n_chains=4, num_adaptation_steps=1000,
seed=tf.constant([36245, 734565], dtype=tf.int32),
observed=co2_by_month_training_data.T
)
When it comes to running MCMC function on this model I get the following error:
------------------------------------------------------------------
ValueError Traceback (most recent call last)
<timed exec> in <module>
~\anaconda3\envs\pm3env\lib\site-packages\tensorflow\python\util\traceback_utils.py in error_handler(*args, **kwargs)
151 except Exception as e:
152 filtered_tb = _process_traceback_frames(e.__traceback__)
--> 153 raise e.with_traceback(filtered_tb) from None
154 finally:
155 del filtered_tb
~\anaconda3\envs\pm3env\lib\site-packages\tensorflow_probability\python\experimental\mcmc\windowed_sampling.py in windowed_adaptive_nuts(n_draws, joint_dist, n_chains, num_adaptation_steps, current_state, init_step_size, dual_averaging_kwargs, max_tree_depth, max_energy_diff, unrolled_leapfrog_steps, parallel_iterations, trace_fn, return_final_kernel_results, discard_tuning, chain_axis_names, seed, **pins)
709 'unrolled_leapfrog_steps': unrolled_leapfrog_steps,
710 'parallel_iterations': parallel_iterations}
--> 711 return _windowed_adaptive_impl(
712 n_draws=n_draws,
713 joint_dist=joint_dist,
~\anaconda3\envs\pm3env\lib\site-packages\tensorflow_probability\python\experimental\mcmc\windowed_sampling.py in _windowed_adaptive_impl(n_draws, joint_dist, kind, n_chains, proposal_kernel_kwargs, num_adaptation_steps, current_state, dual_averaging_kwargs, trace_fn, return_final_kernel_results, discard_tuning, seed, chain_axis_names, **pins)
883 samplers.sanitize_seed(seed), n=2)
884 (target_log_prob_fn, initial_transformed_position, bijector,
--> 885 step_broadcast, batch_shape, shard_axis_names) = _setup_mcmc(
886 joint_dist,
887 n_chains=n_chains,
~\anaconda3\envs\pm3env\lib\site-packages\tensorflow_probability\python\experimental\mcmc\windowed_sampling.py in _setup_mcmc(model, n_chains, init_position, seed, **pins)
241 tf.nest.flatten(batch_shape))
242
--> 243 lp_static_shape = tensorshape_util.concatenate(n_chains, batch_shape)
244
245 if not tensorshape_util.is_fully_defined(batch_shape):
~\anaconda3\envs\pm3env\lib\site-packages\tensorflow_probability\python\internal\tensorshape_util.py in concatenate(x, other)
131 dimensions in `x` and `other`.
132 """
--> 133 return _cast_tensorshape(tf.TensorShape(x).concatenate(other), type(x))
134
135
~\anaconda3\envs\pm3env\lib\site-packages\tensorflow_probability\python\internal\tensorshape_util.py in _cast_tensorshape(x, x_type)
72 # as the shape, which we don't want.
73 return np.array(as_list(x), dtype=np.int32)
---> 74 return x_type(as_list(x))
75
76
~\anaconda3\envs\pm3env\lib\site-packages\tensorflow_probability\python\internal\tensorshape_util.py in as_list(x)
62 ValueError: If `x` has unknown rank.
63 """
---> 64 return tf.TensorShape(x).as_list()
65
66
ValueError: as_list() is not defined on an unknown TensorShape.
Is there something that I need to fix in the generate_gam_ar_latent()
function itself?
def generate_gam_ar_latent(training=True):
@tfd.JointDistributionCoroutine
def gam_with_latent_ar():
seasonality, trend, noise_sigma = yield from gam_trend_seasonality()
# Latent AR(1)
ar_sigma = yield root(tfd.HalfNormal(.1, name='ar_sigma'))
rho = yield root(tfd.Uniform(-1., 1., name='rho'))
def ar_func(y):
loc = tf.concat([tf.zeros_like(y[..., :1]), y[..., :-1]],
axis=-1) * rho[..., None]
return tfd.Independent(
tfd.Normal(loc=loc, scale=ar_sigma[..., None]),
reinterpreted_batch_ndims=1
)
temporal_error = yield tfd.Autoregressive(
distribution_fn=ar_func,
sample0=tf.zeros_like(trend),
num_steps=trend.shape[-1],
name='temporal_error'
)
# Linear prediction
y_hat = seasonality + trend + temporal_error
if training:
y_hat = y_hat[..., :co2_by_month_training_data.shape[0]]
# Likelihood
observed = yield tfd.Independent(
tfd.Normal(y_hat, noise_sigma[..., None]),
reinterpreted_batch_ndims=1,
name='observed'
)
return gam_with_latent_ar
I upgraded to TensorFlow Probability 0.15.0 and now my Kernel just dies; is there any possible known solution to this? Many thanks for this incredible book!