如何在 TensorFlow 中为矢量化参数设置双射器?
How to set bijectors for vectorized parameters in TensorFlow?
我正在遵循 GaussianProcessRegressionModel 本教程的第 3 个示例的逻辑。但是,我的设置中的差异之一是我的 amplitude 和 length_scale 是向量。但是,我很难为矢量化参数设置双射器。
我尝试了官方示例教程中的一种方法(click here 并搜索关键字 'Batching Bijectors')。
他们使用了
softplus = tfp.bijectors.Softplus(
hinge_softness=[1., .5, .1])
print("Hinge softness shape:", softplus.hinge_softness.shape)
更改标量参数的 Softplus 形状。但是控制台一直显示相同的错误信息。
My 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之后我也有了更深的理解。
再次感谢您的澄清...当我知道我错在哪里后,这是多么美好的一天...
如果你只想要铰链柔软度为 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
是使用来自 target_log_prob_fn
的 log_prob 你 return 的排名作为目标批排名得出的,即尾随事件维度将被双射器减少.
你能多说说你想做什么吗?看起来您正在尝试 运行 通过一堆 GP 内核 hparams 的单个 MCMC 链。很难提供很多帮助,没有看到 compute_joint_log_prob_3
.
的内部结构
我正在遵循 GaussianProcessRegressionModel 本教程的第 3 个示例的逻辑。但是,我的设置中的差异之一是我的 amplitude 和 length_scale 是向量。但是,我很难为矢量化参数设置双射器。
我尝试了官方示例教程中的一种方法(click here 并搜索关键字 'Batching Bijectors')。
他们使用了
softplus = tfp.bijectors.Softplus(
hinge_softness=[1., .5, .1])
print("Hinge softness shape:", softplus.hinge_softness.shape)
更改标量参数的 Softplus 形状。但是控制台一直显示相同的错误信息。
My 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之后我也有了更深的理解。
再次感谢您的澄清...当我知道我错在哪里后,这是多么美好的一天...
如果你只想要铰链柔软度为 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
是使用来自 target_log_prob_fn
的 log_prob 你 return 的排名作为目标批排名得出的,即尾随事件维度将被双射器减少.
你能多说说你想做什么吗?看起来您正在尝试 运行 通过一堆 GP 内核 hparams 的单个 MCMC 链。很难提供很多帮助,没有看到 compute_joint_log_prob_3
.