tensorflow - 如何从 Tensorflow 概率将多个参数传递到 DistributionLambda 层?

标签 tensorflow probability gamma-distribution

我正在使用 Keras 和 Tensorflow 概率构建一个模型,该模型应该输出 Gamma 函数的参数(alpha 和 beta),而不是单个参数,如下例所示( t 被传递到 Normal 分布函数)。

import tensorflow as tf
import tensorflow_probability as tfp
tfd = tfp.distributions

# Build model.
model = tf.keras.Sequential([
  tf.keras.layers.Dense(1),
  tfp.layers.DistributionLambda(lambda t: tfd.Normal(loc=t, scale=1)),
])

# Do inference.
model.compile(optimizer=tf.optimizers.Adam(learning_rate=0.05), loss=negloglik)
model.fit(x, y, epochs=500, verbose=False)

# Make predictions.
yhat = model(x_tst)

我想输出 alpha 而不是这个和beta从两个Dense层,然后将此参数传递给 Gamma分配功能。

最佳答案

类似这样的吗?

import tensorflow as tf
tf.enable_eager_execution()

print(tf.__version__) # 1.14.1-dev20190503

import tensorflow_probability as tfp
tfd = tfp.distributions

X = np.random.rand(4, 1).astype(np.float32)

d0 = tf.keras.layers.Dense(2)(X)
s0, s1 = tf.split(d0, 2)
dist = tfp.layers.DistributionLambda(lambda t: tfd.Gamma(t[0], t[1]))(s0, s1)

dist.sample() 
# <tf.Tensor: id=10580, shape=(2,), dtype=float32, numpy=array([1.1754944e-38, 1.3052921e-01], dtype=float32)>

关于tensorflow - 如何从 Tensorflow 概率将多个参数传递到 DistributionLambda 层?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56091458/

相关文章:

python - 使用 BatchNorm 层和 tensorflow 训练 Keras 模型

python - 在 Python 中计算分布中随机变量的概率

python - 分布密度

r - 仍然获得 'gamma' 系列的非正值

r - R 中的 Gamma 分布下限

python - 使用 Tensorflow CNN 进行预测

python - sigmoid激活函数可以用来解决Keras中的回归问题吗?

tensorflow - 强化学习,如何从 Action 维度空间大于一的高斯分布中采样 Action ?

r - 作业 : Simulating coin tosses until consecutive heads using R

R - Gamma 累积分布函数