tensorflow - 如何对中间层特征强制执行概率分布?
问题描述
我有一个 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}}]]
解决方案
如果您不需要严格执行分配而是惩罚分歧,您可以考虑使用KLDivergenceRegularizer
with 。use_exact=False
请注意,大多数 Keras 层都接受activity_regularizer
arg,例如
https://www.tensorflow.org/api_docs/python/tf/keras/layers/Dense
推荐阅读
- rotation - 使用 OpenLayers WebGL 点层基于属性的旋转和偏移
- android - 文本字段 jetpack compose 中的固定占位符
- c - 用于读\写的不同搜索指针
- vue.js - PWA Vue Vuetify - 减少包大小
- flutter - 如何在手机图库上方显示应用栏?
- azure - 阻止 Azure Virtual PC 访问本地物理机
- html - flex-srink 是如何自动计算的?
- javascript - 如何从JS中的数组中找到重复记录
- javascript - 如何在复选框上添加/删除完整日历的事件源?
- uml - 箭头是否应该将枚举数据类型连接到类图中的类?