如何在Tensorflow概率中分解和可视化坡度分量 [英] How to Decompose and Visualise Slope Component in Tensorflow Probability
问题描述
我正在运行tensorflow 2.1和tensorflow_probability 0.9.我已经将结构时间序列模型与季节性要素拟合.我正在使用Tensorflow概率结构时间序列概率示例中的代码: Tensorflow Github上一>.
I'm running tensorflow 2.1 and tensorflow_probability 0.9. I have fit a Structural Time Series Model with a seasonal component. I am using code from the Tensorflow Probability Structural Time Series Probability example: Tensorflow Github.
在该示例中,有一个大图可以直观地看到分解:
In the example there is a great plot where the decomposition is visualised:
# Get the distributions over component outputs from the posterior marginals on
# training data, and from the forecast model.
component_dists = sts.decompose_by_component(
demand_model,
observed_time_series=demand_training_data,
parameter_samples=q_samples_demand_)
forecast_component_dists = sts.decompose_forecast_by_component(
demand_model,
forecast_dist=demand_forecast_dist,
parameter_samples=q_samples_demand_)
demand_component_means_, demand_component_stddevs_ = (
{k.name: c.mean() for k, c in component_dists.items()},
{k.name: c.stddev() for k, c in component_dists.items()})
(
demand_forecast_component_means_,
demand_forecast_component_stddevs_
) = (
{k.name: c.mean() for k, c in forecast_component_dists.items()},
{k.name: c.stddev() for k, c in forecast_component_dists.items()}
)
在使用趋势分量时,是否可以分解和可视化这两者:
When using a trend component, is it possible to decompose and visualise both:
趋势/_level_scale&趋势/_slope_scale
trend/_level_scale & trend/_slope_scale
我尝试了许多排列来提取趋势组件的嵌套元素,但是没有运气.
I have tried many permutations to extract the nested element of the trend component with no luck.
感谢您的时间.
推荐答案
我们没有为此编写单独的STS接口,但是您可以通过以下方式访问潜在状态(在此情况下,水平和倾斜)的后验直接查询基础状态空间模型的边际均值和协方差:
We didn't write a separate STS interface for this, but you can access the posterior on latent states (in this case, both the level and slope) by directly querying the underlying state-space model for its marginal means and covariances:
ssm = model.make_state_space_model(
num_timesteps=num_timesteps,
param_vals=parameter_samples)
posterior_means, posterior_covs = (
ssm.posterior_marginals(observed_time_series))
您还应该能够通过运行ssm.posterior_sample(observed_time_series, num_samples)
从关节后部提取样本.
You should also be able to draw samples from the joint posterior by running ssm.posterior_sample(observed_time_series, num_samples)
.
从没有批处理形状(Could not find valid device for node. Node:{{node Reshape}}
)的模型中提取后验样本时,当前似乎有一个小故障:在我们修复此问题的同时,应该添加一个人工批处理维度作为解决方法:
ssm.posterior_sample(observed_time_series[tf.newaxis, ...], num_samples)
.
It looks like there's currently a glitch when drawing posterior samples from a model with no batch shape (Could not find valid device for node. Node:{{node Reshape}}
): while we fix that, it should work to add an artificial batch dimension as a workaround:
ssm.posterior_sample(observed_time_series[tf.newaxis, ...], num_samples)
.
这篇关于如何在Tensorflow概率中分解和可视化坡度分量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!