首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将多个参数从Tensorflow probability传递到DistributionLambda层?

如何将多个参数从Tensorflow probability传递到DistributionLambda层?
EN

Stack Overflow用户
提问于 2019-05-11 22:55:16
回答 1查看 1.3K关注 0票数 3

我正在使用Keras和Tensorflow概率构建一个模型,该模型应该输出伽马函数的参数(α和β),而不是下面示例中所示的单个参数(t被传递给Normal分布函数)。

代码语言:javascript
复制
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)

相反,我想从两个Dense层输出alphabeta,然后将此参数传递给Gamma分布函数。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-23 05:29:44

像这样的东西?

代码语言:javascript
复制
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)>
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56091458

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档