Keras混合模型

标签 keras

是否可以在 Keras 中实现专家方法的 MLP 混合?
您能否通过 Keras 中的简单代码指导我解决 2 位专家的二进制问题。

它需要定义一个成本函数,如下所示:

g = gate.layers[-1].output
o1 = mlp1.layers[-1].output
o2 = mlp2.layers[-1].output

def ME_objective(y_true, y_pred):
    A = g[0] * T.exp(-0.5*T.sqr(y_true – o1))
    B = g[1] * T.exp(-0.5*T.sqr(y_true – o2))
    return -T.log((A+B).sum())  # cost

最佳答案

模型

你绝对可以在 Keras 中为这样的结构建模,使用 a merge layer ,它使您能够组合不同的输入。
这是一个 SSCCE希望你能够适应你的结构

import numpy as np
from keras.engine import Merge
from keras.models import Sequential
from keras.layers import Dense
import keras.backend as K

xdim = 4
ydim = 1
gate = Sequential([Dense(2, input_dim=xdim)])
mlp1 = Sequential([Dense(1, input_dim=xdim)])
mlp2 = Sequential([Dense(1, input_dim=xdim)])


def merge_mode(branches):
    g, o1, o2 = branches
    # I'd have liked to write
    # return o1 * K.transpose(g[:, 0]) + o2 * K.transpose(g[:, 1])
    # but it doesn't work, and I don't know enough Keras to solve it
    return K.transpose(K.transpose(o1) * g[:, 0] + K.transpose(o2) * g[:, 1])


model = Sequential()
model.add(Merge([gate, mlp1, mlp2], output_shape=(ydim,), mode=merge_mode))
model.compile(optimizer='Adam', loss='mean_squared_error')

train_size = 19
nb_inputs = 3  # one input tensor for each branch (g, o1, o2)
x_train = [np.random.random((train_size, xdim)) for _ in range(nb_inputs)]
y_train = np.random.random((train_size, ydim))
model.fit(x_train, y_train)

自定义目标

这是您描述的目标的实现。有几个数学问题 不过要记住(见下文)。

def me_loss(y_true, y_pred):
    g = gate.layers[-1].output
    o1 = mlp1.layers[-1].output
    o2 = mlp2.layers[-1].output
    A = g[:, 0] * K.transpose(K.exp(-0.5 * K.square(y_true - o1)))
    B = g[:, 1] * K.transpose(K.exp(-0.5 * K.square(y_true - o2)))
    return -K.log(K.sum(A+B))

# [...] edit the compile line from above example
model.compile(optimizer='Adam', loss=me_loss)

一些数学

简短版本:在您的模型中的某处,我认为应该至少有一个约束(可能是两个):

For any x, sum(g(x)) = 1

For any x, g0(x) > 0 and g1(x) > 0 # might not be strictly necessary



领域研究
  • o1(x)o2(x)是无限的 来自 y :
  • exp 项趋向于 +0
  • A -> B -> +-0取决于 g0(x)g1(x)标志
  • cost -> +infinitenan
  • o1(x)o2(x)是无限的 关闭 y :
  • exp 项趋向于 1
  • A -> g0(x)B -> g1(x)
  • cost -> -log(sum(g(x)))

  • 问题是log仅在 ]0, +inf[ 上定义.这意味着要始终定义目标,需要在某处设置约束以确保 sum(A(x) + B(x)) > 0任何 x .该约束的限制性更强的版本是( g0(x) > 0g1(x) > 0 )。

    收敛

    这里更重要的问题是,这个目标似乎没有被设计为向 0 收敛。当 mlp1 时。和 mlp2开始预测 y正确(情况 2),目前没有什么可以阻止优化器生成 sum(g(x))趋向于+infinite , 使 loss趋向于-inifinite .

    理想情况下,我们希望 loss -> 0 ,即 sum(g(x)) -> 1

    关于Keras混合模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40074730/

    相关文章:

    python - 在 Keras 中的 GPU 上微调 VGG-16 : memory consumption

    python - TimeDistributed 与 TimeDistributedDense Keras

    python - Keras 或 Tensorflow 函数绘制神经网络结构的 3D 图?

    python - 此 TensorFlow 二进制文件使用 Intel(R) MKL-DNN 进行了优化,可在性能关键的情况下使用以下 CPU 指令

    keras - 从 Keras 自动编码器中的瓶颈层提取特征

    machine-learning - 为什么keras中CNN的中间层输出不同?

    tensorflow - tensorflow 中的嵌套 while 循环

    python - 为什么我在 Keras 中得到负的 false_negative 计数(例如 -10)?

    python - Keras通过argmax输出单个值

    python - 使用中间层作为输入和输出的 keras 模型