[英]Tensorflow Probability Error: OperatorNotAllowedInGraphError: iterating over `tf.Tensor` is not allowed
I am trying to estimate a model in tensorflow using NUTS by providing it a likelihood function.我试图通过提供似然函数来使用 NUTS 估计张量流中的模型。 I have checked the likelihood function is returning reasonable values.我已经检查了似然函数是否返回了合理的值。 I am following the setup here for setting up NUTS:https://rlhick.people.wm.edu/posts/custom-likes-tensorflow.html我正在按照此处的设置来设置 NUTS:https ://rlhick.people.wm.edu/posts/custom-likes-tensorflow.html
and some of the examples here for setting up priors, etc.: https://github.com/tensorflow/probability/blob/master/tensorflow_probability/examples/jupyter_notebooks/Multilevel_Modeling_Primer.ipynb以及这里设置先验等的一些示例: https : //github.com/tensorflow/probability/blob/master/tensorflow_probability/examples/jupyter_notebooks/Multilevel_Modeling_Primer.ipynb
My code is in a colab notebook here: https://drive.google.com/file/d/1L9JQPLO57g3OhxaRCB29do2m808ZUeex/view?usp=sharing我的代码在一个 colab 笔记本中: https ://drive.google.com/file/d/1L9JQPLO57g3OhxaRCB29do2m808ZUeex/view?usp = sharing
I get the error: OperatorNotAllowedInGraphError: iterating over
tf.Tensor is not allowed: AutoGraph did not convert this function. Try decorating it directly with @tf.function.
我收到错误消息: OperatorNotAllowedInGraphError: iterating over
is not allowed: AutoGraph did not convert this function. Try decorating it directly with @tf.function.
OperatorNotAllowedInGraphError: iterating over
tf.Tensor is not allowed: AutoGraph did not convert this function. Try decorating it directly with @tf.function.
is not allowed: AutoGraph did not convert this function. Try decorating it directly with @tf.function.
This is my first time using tensorflow and I am quite lost interpreting this error.这是我第一次使用 tensorflow,我完全无法解释这个错误。 It would also be ideal if I could pass the starting parameter values as a single input (example I am working off doesn't do it, but I assume it is possible).如果我可以将起始参数值作为单个输入传递,那也将是理想的(我正在处理的示例没有这样做,但我认为这是可能的)。
Update It looks like I had to change the position of the @tf.function decorator.更新看起来我不得不改变@tf.function 装饰器的位置。 The sampler now runs, but it gives me the same value for all samples for each of the parameters.采样器现在运行,但它为每个参数的所有样本提供相同的值。 Is it a requirement that I pass a joint distribution through the log_prob() function?我是否需要通过 log_prob() 函数传递联合分布? I am clearly missing something.我显然错过了一些东西。 I can run the likelihood through bfgs optimization and get reasonable results (I've estimated the model via maximum likelihood with fixed parameters in other software).我可以通过 bfgs 优化运行似然并得到合理的结果(我在其他软件中通过具有固定参数的最大似然估计了模型)。 It looks like I need to define the function to return a joint distribution and call log_prob().看起来我需要定义函数来返回联合分布并调用 log_prob()。 I can do this if I set it up as a logistic regression (logit choice model is logistically distributed in differences).如果我将其设置为逻辑回归(logit 选择模型在逻辑上分布在差异中),我可以做到这一点。 However, I lose the standard closed form.但是,我失去了标准的封闭形式。
My function is as follows:我的功能如下:
@tf.function
def mmnl_log_prob(init_mu_b_time,init_sigma_b_time,init_a_car,init_a_train,init_b_cost,init_scale):
# Create priors for hyperparameters
mu_b_time = tfd.Sample(tfd.Normal(loc=init_mu_b_time, scale=init_scale),sample_shape=1).sample()
# HalfCauchy distributions are too wide for logit discrete choice
sigma_b_time = tfd.Sample(tfd.Normal(loc=init_sigma_b_time, scale=init_scale),sample_shape=1).sample()
# Create priors for parameters
a_car = tfd.Sample(tfd.Normal(loc=init_a_car, scale=init_scale),sample_shape=1).sample()
a_train = tfd.Sample(tfd.Normal(loc=init_a_train, scale=init_scale),sample_shape=1).sample()
# a_sm = tfd.Sample(tfd.Normal(loc=init_a_sm, scale=init_scale),sample_shape=1).sample()
b_cost = tfd.Sample(tfd.Normal(loc=init_b_cost, scale=init_scale),sample_shape=1).sample()
# Define a heterogeneous random parameter model with MultivariateNormalDiag()
# Use MultivariateNormalDiagPlusLowRank() to define nests, etc.
b_time = tfd.Sample(tfd.MultivariateNormalDiag( # b_time
loc=mu_b_time,
scale_diag=sigma_b_time),sample_shape=num_idx).sample()
# Definition of the utility functions
V1 = a_train + tfm.multiply(b_time,TRAIN_TT_SCALED) + b_cost * TRAIN_COST_SCALED
V2 = tfm.multiply(b_time,SM_TT_SCALED) + b_cost * SM_COST_SCALED
V3 = a_car + tfm.multiply(b_time,CAR_TT_SCALED) + b_cost * CAR_CO_SCALED
print("Vs",V1,V2,V3)
# Definition of loglikelihood
eV1 = tfm.multiply(tfm.exp(V1),TRAIN_AV_SP)
eV2 = tfm.multiply(tfm.exp(V2),SM_AV_SP)
eV3 = tfm.multiply(tfm.exp(V3),CAR_AV_SP)
eVD = eV1 + eV2 +
eV3
print("eVs",eV1,eV2,eV3,eVD)
l1 = tfm.multiply(tfm.truediv(eV1,eVD),tf.cast(tfm.equal(CHOICE,1),tf.float32))
l2 = tfm.multiply(tfm.truediv(eV2,eVD),tf.cast(tfm.equal(CHOICE,2),tf.float32))
l3 = tfm.multiply(tfm.truediv(eV3,eVD),tf.cast(tfm.equal(CHOICE,3),tf.float32))
ll = tfm.reduce_sum(tfm.log(l1+l2+l3))
print("ll",ll)
return ll
The function is called as follows:该函数的调用方式如下:
nuts_samples = 1000
nuts_burnin = 500
chains = 4
## Initial step size
init_step_size=.3
init = [0.,0.,0.,0.,0.,.5]
##
## NUTS (using inner step size averaging step)
##
@tf.function
def nuts_sampler(init):
nuts_kernel = tfp.mcmc.NoUTurnSampler(
target_log_prob_fn=mmnl_log_prob,
step_size=init_step_size,
)
adapt_nuts_kernel = tfp.mcmc.DualAveragingStepSizeAdaptation(
inner_kernel=nuts_kernel,
num_adaptation_steps=nuts_burnin,
step_size_getter_fn=lambda pkr: pkr.step_size,
log_accept_prob_getter_fn=lambda pkr: pkr.log_accept_ratio,
step_size_setter_fn=lambda pkr, new_step_size: pkr._replace(step_size=new_step_size)
)
samples_nuts_, stats_nuts_ = tfp.mcmc.sample_chain(
num_results=nuts_samples,
current_state=init,
kernel=adapt_nuts_kernel,
num_burnin_steps=100,
parallel_iterations=5)
return samples_nuts_, stats_nuts_
samples_nuts, stats_nuts = nuts_sampler(init)
I have an answer to my question!我有我的问题的答案! It is simply a matter of different nomenclature.这只是不同命名法的问题。 I need to define my model as a softmax function, which I knew was what I would call a "logit model", but it just wasn't clicking for me.我需要将我的模型定义为 softmax 函数,我知道这就是我所说的“logit 模型”,但它并没有为我点击。 The following blog post gave me the epiphany: http://khakieconomics.github.io/2019/03/17/Putting-it-all-together.html以下博文让我顿悟: http : //khakieconomics.github.io/2019/03/17/Putting-it-all-together.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.