1

我在GaussianProcessRegressionModel上遵循本教程第三个示例的逻辑。但是,我的设置中的一个区别是我的幅值长度尺度是向量。但是,我很难为矢量化参数设置双射器。

我尝试了官方示例教程中的一种方法(单击此处并搜索关键字“Batching Bijectors”)。

他们用

softplus = tfp.bijectors.Softplus(
  hinge_softness=[1., .5, .1])
print("Hinge softness shape:", softplus.hinge_softness.shape)

更改 Softplus for scalar 参数的形状。但是控制台一直显示相同的错误消息。

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

我不知道这些输入形状到底是什么意思。感谢您的时间和解释。

--------我是人工分隔线------

在和布赖恩讨论之后,我知道我错在哪里了。错误消息可能意味着结果compute_joint_log_prob_3不是标量而是其他形状。

正如布赖恩昨天所说,Softplus()能够根据它所依赖的张量自动广播。如果我想改变它的柔软度,那么我可以修改hinge_softness=....

并且在阅读了关于张量流分布形状的教程后,我也有了更深入的了解。

再次感谢您的澄清......在我知道我错在哪里之后,这是多么美好的一天......

4

1 回答 1

0

如果您只想要铰链柔软度为 1 的相同 softplus,则双射器将广播,您只需编写:

vectorized_sp = tfb.Softplus(hinge_softness=np.float32(1)) 另请注意,默认值为 1,因此更简单: vectorized_sp = tfb.Softplus()

另外,我建议查看SimpleStepSizeAdaptation内核(可能仅在pip install tfp-nightly当前)。

我认为您看到的实际异常可能是由于双射器参数形状与您的潜在状态形状发生某种冲突造成的。转换后的转换内核需要在双射器指定的事件暗淡上减少 log_prob。对于event_ndims每个潜在的,使用您返回的 log_prob 的等级target_log_prob_fn作为目标批次等级得出,即双射器将减少尾随事件维度。

你能多说一点你想做的事吗?看起来您正试图在一堆 GP 内核 hparams 上运行单个 MCMC 链。很难提供很多帮助,而不是看到compute_joint_log_prob_3.

于 2019-04-25T19:51:31.167 回答