我遵循GaussianProcessRegressionModel上本教程第3个示例的逻辑。但是,设置中的差异之一是我的振幅和 length_scale 是矢量。但是,我很难为向量化参数设置双射器。
我尝试了官方示例教程(click here中的一种方法,并搜索了关键字“ Batching Bijectors”)。
他们使用了
softplus = tfp.bijectors.Softplus(
hinge_softness=[1., .5, .1])
print("Hinge softness shape:", softplus.hinge_softness.shape)
更改Softplus for标量参数的形状。但是控制台始终显示相同的错误消息。
我的compute_joint_log_prob_3
仅在给定所有数据和参数的情况下输出标量对数后验概率。而且我已经测试过该功能运作良好。唯一的问题是在存在矢量化内核超参数的情况下unconstrained_bijectors
的设置。
# Create a list to save all variables to be iterated.
initial_chain_states = [
tf.ones([1, num_GPs], dtype=tf.float32, name="init_amp_1"),
tf.ones([1, num_GPs], dtype=tf.float32, name="init_scale_1"),
tf.ones([1, num_GPs], dtype=tf.float32, name="init_amp_0"),
tf.ones([1, num_GPs], dtype=tf.float32, name="init_scale_0"),
tf.ones([], dtype=tf.float32, name="init_sigma_sq_1"),
tf.ones([], dtype=tf.float32, name="init_sigma_sq_0")
]
vectorized_sp = tfb.Softplus(hinge_softness=np.ones([1, num_GPs], dtype=np.float32))
unconstrained_bijectors = [
vectorized_sp,
vectorized_sp,
vectorized_sp,
vectorized_sp,
tfp.bijectors.Softplus(),
tfp.bijectors.Softplus()
]
def un_normalized_log_posterior(amplitude_1, length_scale_1,
amplitude_0, length_scale_0,
noise_var_1, noise_var_0):
return compute_joint_log_prob_3(
para_index, delayed_signal, y_type,
amplitude_1, length_scale_1, amplitude_0, length_scale_0,
noise_var_1, noise_var_0
)
num_results = 200
[
amps_1,
scales_1,
amps_0,
scales_0,
sigma_sqs_1,
sigma_sqs_0
], kernel_results = tfp.mcmc.sample_chain(
num_results=num_results,
num_burnin_steps=250,
num_steps_between_results=3,
current_state=initial_chain_states,
kernel=tfp.mcmc.TransformedTransitionKernel(
inner_kernel=tfp.mcmc.HamiltonianMonteCarlo(
target_log_prob_fn=un_normalized_log_posterior,
step_size=np.float32(0.1),
num_leapfrog_steps=3,
step_size_update_fn=tfp.mcmc.make_simple_step_size_update_policy(
num_adaptation_steps=100)),
bijector=unconstrained_bijectors))
它应该工作,并且模型将绘制此参数的样本。相反,我收到了一堆错误消息,说
Traceback (most recent call last):
File "/MMAR_q/venv/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 1659, in _create_c_op
c_op = c_api.TF_FinishOperation(op_desc)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Requires start <= limit when delta > 0: 1/0 for 'mcmc_sample_chain/transformed_kernel_bootstrap_results/mh_bootstrap_results/hmc_kernel_bootstrap_results/maybe_call_fn_and_grads/value_and_gradients/softplus_10/forward_log_det_jacobian/range' (op: 'Range') with input shapes: [], [], [] and with computed input tensors: input[0] = <1>, input[1] = <0>, input[2] = <1>.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/runpy.py", line 183, in _run_module_as_main
mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/runpy.py", line 109, in _get_module_details
__import__(pkg_name)
File "/MMAR_q/MMAR_q.py", line 237, in <module>
bijector=unconstrained_bijectors))
File "/MMAR_q/venv/lib/python3.7/site-packages/tensorflow_probability/python/mcmc/sample.py", line 235, in sample_chain
previous_kernel_results = kernel.bootstrap_results(current_state)
File "/MMAR_q/venv/lib/python3.7/site-packages/tensorflow_probability/python/mcmc/transformed_kernel.py", line 344, in bootstrap_results
transformed_init_state))
File "/MMAR_q/venv/lib/python3.7/site-packages/tensorflow_probability/python/mcmc/hmc.py", line 518, in bootstrap_results
kernel_results = self._impl.bootstrap_results(init_state)
File "/MMAR_q/venv/lib/python3.7/site-packages/tensorflow_probability/python/mcmc/metropolis_hastings.py", line 264, in bootstrap_results
pkr = self.inner_kernel.bootstrap_results(init_state)
File "/MAR_q/venv/lib/python3.7/site-packages/tensorflow_probability/python/mcmc/hmc.py", line 687, in bootstrap_results
] = mcmc_util.maybe_call_fn_and_grads(self.target_log_prob_fn, init_state)
File "/MMAR_q/venv/lib/python3.7/site-packages/tensorflow_probability/python/mcmc/util.py", line 237, in maybe_call_fn_and_grads
result, grads = _value_and_gradients(fn, fn_arg_list, result, grads)
File "/MMAR_q/venv/lib/python3.7/site-packages/tensorflow_probability/python/mcmc/util.py", line 185, in _value_and_gradients
result = fn(*fn_arg_list)
File "/MMAR_q/venv/lib/python3.7/site-packages/tensorflow_probability/python/mcmc/transformed_kernel.py", line 204, in new_target_log_prob
event_ndims=event_ndims)
File "/MMAR_q/venv/lib/python3.7/site-packages/tensorflow_probability/python/mcmc/transformed_kernel.py", line 51, in fn
for b, e, sp in zip(bijector, event_ndims, transformed_state_parts)
File "/MMAR_q/venv/lib/python3.7/site-packages/tensorflow_probability/python/mcmc/transformed_kernel.py", line 51, in <listcomp>
for b, e, sp in zip(bijector, event_ndims, transformed_state_parts)
File "/MMAR_q/venv/lib/python3.7/site-packages/tensorflow_probability/python/bijectors/bijector.py", line 1205, in forward_log_det_jacobian
return self._call_forward_log_det_jacobian(x, event_ndims, name)
File "/MMAR_q/venv/lib/python3.7/site-packages/tensorflow_probability/python/bijectors/bijector.py", line 1177, in _call_forward_log_det_jacobian
kwargs=kwargs)
File "/MMAR_q/venv/lib/python3.7/site-packages/tensorflow_probability/python/bijectors/bijector.py", line 982, in _compute_inverse_log_det_jacobian_with_caching
event_ndims)
File "/MMAR_q/venv/lib/python3.7/site-packages/tensorflow_probability/python/bijectors/bijector.py", line 1272, in _reduce_jacobian_det_over_event
axis=self._get_event_reduce_dims(min_event_ndims, event_ndims))
File "/MMAR_q/venv/lib/python3.7/site-packages/tensorflow_probability/python/bijectors/bijector.py", line 1284, in _get_event_reduce_dims
return tf.range(-reduce_ndims, 0)
File "/MMAR_q/venv/lib/python3.7/site-packages/tensorflow/python/ops/math_ops.py", line 1199, in range
return gen_math_ops._range(start, limit, delta, name=name)
File "/MMAR_q/venv/lib/python3.7/site-packages/tensorflow/python/ops/gen_math_ops.py", line 6746, in _range
"Range", start=start, limit=limit, delta=delta, name=name)
File "/MMAR_q/venv/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py", line 788, in _apply_op_helper
op_def=op_def)
File "/MMAR_q/venv/lib/python3.7/site-packages/tensorflow/python/util/deprecation.py", line 507, in new_func
return func(*args, **kwargs)
File "/MMAR_q/venv/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 3300, in create_op
op_def=op_def)
File "/MMAR_q/venv/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 1823, in __init__
control_input_ops)
File "/MMAR_q/venv/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 1662, in _create_c_op
raise ValueError(str(e))
ValueError: Requires start <= limit when delta > 0: 1/0 for 'mcmc_sample_chain/transformed_kernel_bootstrap_results/mh_bootstrap_results/hmc_kernel_bootstrap_results/maybe_call_fn_and_grads/value_and_gradients/softplus_10/forward_log_det_jacobian/range' (op: 'Range') with input shapes: [], [], [] and with computed input tensors: input[0] = <1>, input[1] = <0>, input[2] = <1>.
我不知道这些输入形状到底意味着什么。谢谢您的时间和解释。
-------我是人工分离线------
与Brian讨论后,我知道我错了。错误消息可能意味着compute_joint_log_prob_3
的结果不是标量,而是其他形状。
正如Brian昨天说的那样,Softplus()
能够根据所得到的张量自动广播。如果要更改其柔软度,则可以修改hinge_softness=...
。
阅读tutorial on tensorflow distribution shape后,我也获得了更深入的了解。
再次感谢您的澄清...在我知道自己错在哪里之后,今天真是美好的一天...
答案 0 :(得分:0)
如果您只希望使用相同的softplus,其铰链柔度为1,则双射器将广播,您可以编写:
vectorized_sp = tfb.Softplus(hinge_softness=np.float32(1))
另请注意,默认值为1,因此更简单:
vectorized_sp = tfb.Softplus()
另外,我建议您查看SimpleStepSizeAdaptation
内核(当前可能仅位于pip install tfp-nightly
中)。
我认为您看到的实际异常可能是由于bijector参数形状与您的潜伏状态形状发生了某种冲突。转换后的过渡内核需要在双射器指定的事件暗中减少log_prob。每个潜伏数的event_ndims
是使用您从target_log_prob_fn
返回的log_prob的等级作为目标批次等级得出的,即跟踪事件的维数将被Bijector减小。
您能再说一下您要做什么吗?看来您正在尝试在一堆GP内核hparams上运行单个MCMC链。很难提供很多帮助,而看不到compute_joint_log_prob_3
的内部。