如果我将图层传递给两个 Keras 模型并只训练一个模型,那么在前者训练后两个模型是否会共享权重

If I pass layers to two Keras models and Train only one ,will both the model share weights after the former is trained

我尝试使用 Keras 构建一个简单的自动编码器,为此我从一个完全连接的神经层开始作为编码器和解码器。

> input_img = Input(shape=(784,)) 
>encoded = Dense(encoding_dim,activation='relu')(input_img) 
>decoded = Dense(784, activation='sigmoid')(encoded)
>autoencoder =Model(input_img, decoded)

我还在

的帮助下创建了一个单独的编码器模块
encoder = Model(input_img, encoded)

以及解码器型号:

encoded_input = Input(shape=(32,))
# retrieve the last layer of the autoencoder model
decoder_layer = autoencoder.layers[-1]
# create the decoder model
decoder = Model(encoded_input, decoder_layer(encoded_input))

然后我训练了模型

autoencoder.fit(x_train, x_train,
                epochs=50,
                batch_size=256,
                shuffle=True,
                validation_data=(x_test, x_test))

但即使我没有训练我的编码器和解码器,即使我在训练前通过了层,它们也会共享自动编码器的权重。我只训练了编码器,但编码器和解码器都在接受训练。

encoded_imgs = encoder.predict(x_test)
decoded_imgs = decoder.predict(encoded_imgs)

我在阅读文本时应该更加小心。 如果两个 Keras 模型共享某些层,当您训练第一个模型时,共享层的权重将在另一个模型中自动更新。

https://keras.io/getting-started/functional-api-guide/

此博客很好地说明了共享层的使用。

https://blog.keras.io/building-autoencoders-in-keras.html/