【发布时间】:2020-06-10 10:15:16
【问题描述】:
我在使用 tfp.layers.DistributionLambda 时遇到问题,我是一个 TF 新手,正在努力使张量流动。 有人可以提供一些关于如何设置输出分布参数的见解吗?
上下文:
TFP 团队在Regression with Probabilistic Layers in TensorFlow Probability 上写了一个教程,它建立了以下模型:
# Build model.
model = tfk.Sequential([
tf.keras.layers.Dense(1 + 1),
tfp.layers.DistributionLambda(
lambda t: tfd.Normal(loc=t[..., :1],
scale=1e-3 + tf.math.softplus(0.05 * t[..., 1:]))),
])
我的问题:
它使用 tfp.layers.DistributionLambda 输出正态分布,但我不清楚 tfd.Normal 的参数(平均值/位置和标准差/比例)是如何设置的,所以我无法将 Normal 更改为伽马分布。我尝试了以下方法,但没有奏效(预测分布参数为 nan)。
def dist_output_layer (t, softplus_scale=0.05):
"""Create distribution with variable mean and variance
"""
mean = t[..., :1]
std_dev = 1e-3 + tf.math.softplus(softplus_scale * mean)
alpha = (mean/std_dev)**2
beta = alpha/mean
return tfd.Gamma(concentration = alpha,
rate = beta
)
# Build model.
model = tf.keras.Sequential([
tf.keras.layers.Dense(20,activation="relu"), # "By using a deeper neural network and introducing nonlinear activation functions, however, we can learn more complicated functional dependencies!
tf.keras.layers.Dense(1 + 1), #two neurons here b/c the output layer's distribution's mean and std. deviation
tfp.layers.DistributionLambda(dist_output_layer)
])
非常感谢。
【问题讨论】:
标签: python tensorflow tensorflow-probability