tensorflow - 如何将多个参数从 Tensorflow 概率传递到 DistributionLambda 层?
问题描述
我正在使用 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)>
推荐阅读
- java - Java 和 Selenium:获取列表元素的 id
- ejs - 是否可以通过 storybook.js 的 preview-head.html 文件访问 webpack 配置属性?
- android - React Native - Google Fit API 无法获取步骤
- git - 如何仅签出特定提交的内容?
- python - 如何在推送到容器的注册表之前对构建的映像运行测试?
- c# - 如何解决电报中设置webhook的错误
- android-studio - 如何在android中向上设置微调器的位置?
- ios - Swift-以相同的顺序解码字典
- python - Pandas 将值分配给过滤后的数据帧/系列。为什么不一致?
- javascript - TypeError:无法读取 DirectionPage.CalculateDistanceWithGoogleApi 处未定义的属性“地图”