用两组数据求tensorflow概率中log_prob的似然

Use two set of data for likelihood of log_prob in tensorflow probability

我是 tensorflow 的新手,正在尝试将 STAN 模型转换为 TFP。这是我使用 JointDistributionCoroutineAutoBatched.

的 TFP 模型
def make_joint_distribution_coroutine(Depth,N_RNA):
    def model():
        ## c1 prior 
        c1 = yield tfd.Gamma(concentration = 1.1, rate = 0.005)
        ## c2 prior
        c2 = yield tfd.Gamma(concentration = 1.1, rate = 0.005)
        ## s prior 
        s = yield GammaModeSD(1,1)
        ## theta prior 
        theta = yield tfd.LogNormal(0,s)
        ## p prior
        p = yield BetaModeConc(0.1,c1)
        ## tfp bug, need to cast tensor to float32
        #theta = tf.cast(theta, tf.float32)
        #p = tf.cast(p, tf.float32)
        ## q formula
        q = (theta*p)/(1-p+theta*p)
        ## qi prior 
        qi = yield BetaModeConc(tf.repeat(q,N_RNA), c2)
        ## qi likelihood 
        k = yield tfd.Binomial(tf.cast(Depth,tf.float32),qi)
        # p likelihood
        a = yield tfd.Binomial(tf.cast(Depth,tf.float32),p)
    return tfd.JointDistributionCoroutineAutoBatched(model)

我的模型生成了两组不同的数据,它们是 ak。如果它只有 ak,那么我可以通过

指定我的 log_prob 函数
def joint_log_prob(*args):
      return joint.log_prob(*args, likelihood = data)

joint_log_prob = lambda *x: model.log_prob(x + (data,))

但我的问题是如何将两组不同的数据合并到一个 log_prob 函数中?谢谢!

最简单的解决方案就是同时指定两者。假设 datatuple:

def joint_log_prob(*args):
  return joint.log_prob(*args, a=data[0], k=data[1])

joint_log_prob = lambda *x: model.log_prob(x + data)

您可能还想写:

joint_log_prob = joint.experimental_pin(a=.., k=..).unnormalized_log_prob

(见JointDistributionPinned