【问题标题】:Connecting Keras models / replacing input but keeping layers连接 Keras 模型/替换输入但保留层
【发布时间】:2018-10-15 11:10:34
【问题描述】:

这个问题类似于Keras replacing input layer

我有一个分类器网络和一个自动编码器网络,我想使用自动编码器的输出(即编码 + 解码,作为预处理步骤)作为分类器的输入 - 但在分类器已经接受常规训练之后数据。

分类网络是用这样的函数式API构建的(基于this example):

clf_input = Input(shape=(28,28,1))
clf_layer = Conv2D(...)(clf_input)
clf_layer = MaxPooling2D(...)(clf_layer)
...
clf_output = Dense(num_classes, activation='softmax')(clf_layer)
model = Model(clf_input, clf_output)
model.compile(...)
model.fit(...)

还有像这样的自动编码器(基于this example):

ae_input = Input(shape=(28,28,1))
x = Conv2D(...)(ae_input)
x = MaxPooling2D(...)(x)
...
encoded = MaxPooling2D(...)(x)
x = Conv2d(...)(encoded)
x = UpSampling2D(...)(x)
...
decoded = Conv2D(...)(x)
autoencoder = Model(ae_input, decoded)
autoencoder.compile(...)
autoencoder.fit(...)

我可以像这样连接两个模型(我仍然需要原始模型,因此需要复制):

model_copy = keras.models.clone_model(model)
model_copy.set_weights(model.get_weights())
# remove original input layer
model_copy.layers.pop(0)
# set the new input
new_clf_output = model_copy(decoded)
# get the stacked model
stacked_model = Model(ae_input, new_clf_output)
stacked_model.compile(...)

当我想要做的只是将模型应用于新的测试数据时,这很有效,但它会在这样的情况下出现错误:

for layer in stacked_model.layers:
    print layer.get_config()

它到达自动编码器的末尾,但随后在分类器模型获取其输入的位置出现 KeyError 失败。此外,当使用keras.utils.plot_model 绘制模型时,我得到了这个:

您可以在其中看到自动编码器层,但最后,不是分类器模型中的各个层,而是一个块中只有完整的模型。

有没有办法连接两个模型,这样新的堆叠模型实际上是由所有单独的层组成的?

【问题讨论】:

    标签: python tensorflow deep-learning keras


    【解决方案1】:

    好的,我能想出的是真正手动遍历模型的每一层并像这样再次将它们重新连接:

    l = model.layers[1](decoded)  # layer 0 is the input layer, which we're replacing
    for i in range(2, len(model.layers)):
        l = model.layers[i](l)
    stacked_model = Model(ae_input, l)
    stacked_model.compile(...)
    

    虽然这可以工作并产生正确的情节并且没有错误,但这似乎不是最优雅的解决方案......

    (顺便说一句,模型的复制实际上似乎是不必要的,因为我没有重新训练任何东西。)

    【讨论】:

      猜你喜欢
      • 2018-09-07
      • 1970-01-01
      • 2018-03-16
      • 2018-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-02
      相关资源
      最近更新 更多