【问题标题】:How to mix many distributions in one tensorflow probability layer?如何在一个张量流概率层中混合多个分布?
【发布时间】:2020-11-23 15:50:45
【问题描述】:

我有几个DistributionLambda 层作为一个模型的输出,我想在一个新层中进行类似连接的操作,以便只有一个输出是所有分布的混合,假设他们是独立的。然后,我可以对模型的输出应用对数似然损失。否则,我无法将损失应用于Concatenate 层,因为它丢失了log_prob 方法。我一直在尝试使用 Blockwise 分发,但到目前为止没有运气。

这里是一个示例代码:

from tensorflow.keras import layers
from tensorflow.keras import models
from tensorflow.keras import optimizers
from tensorflow_probability import distributions
from tensorflow_probability import layers as tfp_layers


def likelihood_loss(y_true, y_pred):
    """Adding negative log likelihood loss."""
    return -y_pred.log_prob(y_true)


def distribution_fn(params):
    """Distribution function."""
    return distributions.Normal(
        params[:, 0], math.log(1.0 + math.exp(params[:, 1])))


output_steps = 3
...
lstm_layer = layers.LSTM(10, return_state=True)
last_layer, l_h, l_c = lstm_layer(last_layer)
lstm_state = [l_h, l_c]
dense_layer = layers.Dense(2)
last_layer = dense_layer(last_layer)
last_layer = tfp_layers.DistributionLambda(
    make_distribution_fn=distribution_fn)(last_layer)
output_layers = [last_layer]
# Get output sequence, re-injecting the output of each step
for number in range(1, output_steps):
    last_layer = layers.Reshape((1, 1))(last_layer)
    last_layer, l_h, l_c = lstm_layer(last_layer, initial_state=lstm_states)
    # Storing state for next time step
    lstm_states = [l_h, l_c]
    last_layer = tfp_layers.DistributionLambda(
        make_distribution_fn=distribution_fn)(dense_layer(last_layer))
    output_layers.append(last_layer)

# This does not work
# last_layer = distributions.Blockwise(output_layers)

# This works for the model but cannot compute loss
# last_layer = layers.Concatenate(axis=1)(output_layers)
the_model = models.Model(inputs=[input_layer], outputs=[last_layer])
the_model.compile(loss=likelihood_loss, optimizer=optimizers.Adam(lr=0.001))

【问题讨论】:

  • 看看 tdf.independent。这可能行得通。
  • 我认为Independent 更像是一种将分布分成独立部分的方法。它是相关的,但我想做的是将许多发行版(它们的列表)混合成一个。 Blockwise 似乎有效,我将注释行更改为last_layer = tfp_layers.DistributionLambda(make_distribution_fn=distributions.Blockwise)(output_layers),但现在我无法保存模型:Github issue

标签: python tensorflow keras tf.keras tensorflow-probability


【解决方案1】:

问题是你的输入,而不是你的输出层;)

Input:0 在您的错误消息中被引用。 您能否尝试更具体地说明您的意见?

【讨论】:

  • 错误信息不是指整个模型的输入,它似乎是指保存模型时正在创建的内部JointDistributionSequential模型的第一个输入。
猜你喜欢
  • 2020-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-29
  • 2020-12-29
  • 1970-01-01
  • 1970-01-01
  • 2018-07-31
相关资源
最近更新 更多