首页 > 解决方案 > 如何对中间层特征强制执行概率分布?

问题描述

我有一个 CNN-LSTM 架构。现在,我想对中间层的特征实施概率分布,比如混合正态分布。我怎样才能在 Keras 中做到这一点?

我看到了 keras 的 MDN 实现(https://github.com/cpmpercussion/keras-mdn-layer)。但这要求它应该用作网络的最后一层。另外,我查看了 tensorflow_probability,但我不确定是否可以为此使用 tensorflow_probability 层。

enc.add(LSTM(units=64,activation='tanh',return_sequences=False))

# probability layer
enc.add(tfpl.MixtureNormal(num_classes,[64]))

enc.add(Dense(units=num_classes,activation='softmax'))

当我尝试上面的代码时,它给了我一个错误如下:

tensorflow.python.framework.errors_impl.InvalidArgumentError: 2 root error(s) found.

  (0) Invalid argument: Number of ways to split should evenly divide the split dimension, but got split_dim 2 (size = 15) and num_split 2
     [[{{node mixture_normal/MixtureSameFamily/independent_normal/IndependentNormal/split}}]]
     [[metrics/categorical_accuracy/Identity/_251]]

  (1) Invalid argument: Number of ways to split should evenly divide the split dimension, but got split_dim 2 (size = 15) and num_split 2
     [[{{node mixture_normal/MixtureSameFamily/independent_normal/IndependentNormal/split}}]]

标签: tensorflowkerasdeep-learningtensorflow-probability

解决方案


如果您不需要严格执行分配而是惩罚分歧,您可以考虑使用KLDivergenceRegularizerwith 。use_exact=False

请注意,大多数 Keras 层都接受activity_regularizerarg,例如 https://www.tensorflow.org/api_docs/python/tf/keras/layers/Dense


推荐阅读