我正在尝试使用TensorFlow概率构建结构时间序列模型,这是我的模型的结构: tf .__ version__ =='2.2.0' tfp .__ version__ =='0.10.0'
import tensorflow as tf
from tensorflow_probability import distributions as tfd
import tensorflow_probability as tfp
import numpy as np
ts = np.random.normal(size=365).astype('float32')
var = np.random.exponential(3,size=400).astype('float32')
# Local linear trend.
local_linear_trend = tfp.sts.LocalLinearTrend(
observed_time_series=ts.astype('float32'),
name='LLT'
)
day_of_week = tfp.sts.Seasonal(
num_seasons=7,
num_steps_per_season=1,
observed_time_series=ts.astype('float32'),
name='day_of_week',
)
month_of_year = tfp.sts.Seasonal(
num_seasons=12,
num_steps_per_season=30,
observed_time_series=ts.astype('float32'),
name='month_of_year',
)
yearweek = tfp.sts.Seasonal(
num_seasons=52,
num_steps_per_season=7,
observed_time_series=ts.astype('float32'),
name='yearweek',
)
external_var = tf.cast(tf.reshape(var,(-1,1)),dtype=tf.float32)
x_var = tfp.sts.LinearRegression(
design_matrix=external_var,
weights_prior=tfd.Exponential(rate=2, validate_args=True),
name='external_factor')
model_components_base = [
local_linear_trend,
day_of_week,
month_of_year,
x_var
]
model_base = tfp.sts.Sum(
components=model_components_base,
observed_time_series=ts
)
optimizer = tf.optimizers.Adam(learning_rate=.1)
variational_posteriors = tfp.sts.build_factored_surrogate_posterior(
model = model_base,
seed=1234
)
target_log_prob_fn = model_base.joint_log_prob(
observed_time_series=ts.astype('float32')
)
elbo_loss_curve = tfp.vi.fit_surrogate_posterior(
target_log_prob_fn=target_log_prob_fn,
surrogate_posterior=variational_posteriors,
optimizer=optimizer,
num_steps=150,
seed=1234
)
请注意,对于外部因素,我已经将权重先验参数设置为指数分布,以确保获得正的权重。 运行脚本后,出现此错误
tensorflow.python.framework.errors_impl.InvalidArgumentError: assertion failed: [Sample must be non-negative.] [Condition x >= 0 did not hold element-wise:] [x (monte_carlo_variational_loss/build_factored_surrogate_posterior_JointDistributionNamed/sample/build_factored_surrogate_posterior_external_factor__weights_posterior/sample/Reshape:0) = ] [[-0.00462905131]]
[[{{node monte_carlo_variational_loss/expectation/external_factor_external_factor_identityExponential/log_prob/Exponential_2/log_prob/assert_non_negative/assert_less_equal/Assert/AssertGuard/else/_1/Assert}}]] [Op:__inference_train_loop_body_17946]
Function call stack:
train_loop_body
ERROR:tensorflow:==================================
Object was never used (type <class 'tensorflow.python.ops.tensor_array_ops.TensorArray'>):
<tensorflow.python.ops.tensor_array_ops.TensorArray object at 0x14365ca90>
If you want to mark it as used call its "mark_used()" method.
It was originally created here:
File "...venv/lib/python3.7/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2770, in while_loop
return result File ".../venv/lib/python3.7/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2718, in <lambda>
body = lambda i, lv: (i + 1, orig_body(*lv)) File "..../venv/lib/python3.7/site-packages/tensorflow/python/ops/functional_ops.py", line 648, in compute
return (next_i, flat_a_out, tas) File "...umm/venv/lib/python3.7/site-packages/tensorflow/python/ops/functional_ops.py", line 643, in <listcomp>
tas = [ta.write(i, value) for (ta, value) in zip(tas, flat_a_out)] File "....venv/lib/python3.7/site-packages/tensorflow/python/util/tf_should_use.py", line 237, in wrapped
error_in_function=error_in_function)
==================================