gpt4 book ai didi

python - 在构建编码器/解码器模型之前训练自动编码器是否有效?

转载 作者:行者123 更新时间:2023-11-30 09:31:32 25 4
gpt4 key购买 nike

我正在关注教程https://blog.keras.io/building-autoencoders-in-keras.html构建我的自动编码器。为此,我有两个策略:

A) 步骤1:构建自动编码器;步骤2:构建编码器;步骤3:构建解码器;步骤4:编译自动编码器;第5步:训练自动编码器。

B) 第 1 步:构建自动编码器;步骤2:编译自动编码器;步骤3:训练自动编码器;步骤4:构建编码器;第五步:构建解码器。

对于这两种情况,模型收敛到损失 0.100。然而,对于教程中所述的策略 ​​A,重建效果非常差。在策略 B 的情况下,重建要好得多。

在我看来,这是有道理的,因为在策略 A 中,编码器和解码器模型的权重是在未经训练的层上构建的,并且结果是随机的。另一方面,在策略 B 中,我在训练后更好地定义了权重,因此重建效果更好。

我的问题是,策略 B 有效还是我在重建过程中作弊?在策略 A 的情况下,Keras 是否应该自动更新编码器和解码器模型的权重,因为它们的模型是基于自动编码器层构建的?

###### Code for Strategy A

# Step 1
features = Input(shape=(x_train.shape[1],))

encoded = Dense(1426, activation='relu')(features)
encoded = Dense(732, activation='relu')(encoded)
encoded = Dense(328, activation='relu')(encoded)

encoded = Dense(encoding_dim, activation='relu')(encoded)

decoded = Dense(328, activation='relu')(encoded)
decoded = Dense(732, activation='relu')(decoded)
decoded = Dense(1426, activation='relu')(decoded)
decoded = Dense(x_train.shape[1], activation='relu')(decoded)

autoencoder = Model(inputs=features, outputs=decoded)

# Step 2
encoder = Model(features, encoded)

# Step 3
encoded_input = Input(shape=(encoding_dim,))
decoder_layer = autoencoder.layers[-4](encoded_input)
decoder_layer = autoencoder.layers[-3](decoder_layer)
decoder_layer = autoencoder.layers[-2](decoder_layer)
decoder_layer = autoencoder.layers[-1](decoder_layer)

decoder = Model(encoded_input, decoder_layer)

# Step 4
autoencoder.compile(optimizer='adam', loss='mse')

# Step 5
history = autoencoder.fit(x_train,
x_train,
epochs=150,
batch_size=256,
shuffle=True,
verbose=1,
validation_split=0.2)

# Testing encoding
encoded_fts = encoder.predict(x_test)
decoded_fts = decoder.predict(encoded_fts)

###### Code for Strategy B

# Step 1
features = Input(shape=(x_train.shape[1],))

encoded = Dense(1426, activation='relu')(features)
encoded = Dense(732, activation='relu')(encoded)
encoded = Dense(328, activation='relu')(encoded)

encoded = Dense(encoding_dim, activation='relu')(encoded)

decoded = Dense(328, activation='relu')(encoded)
decoded = Dense(732, activation='relu')(decoded)
decoded = Dense(1426, activation='relu')(decoded)
decoded = Dense(x_train.shape[1], activation='relu')(decoded)

autoencoder = Model(inputs=features, outputs=decoded)

# Step 2
autoencoder.compile(optimizer='adam', loss='mse')

# Step 3
history = autoencoder.fit(x_train,
x_train,
epochs=150,
batch_size=256,
shuffle=True,
verbose=1,
validation_split=0.2)
# Step 4
encoder = Model(features, encoded)

# Step 5
encoded_input = Input(shape=(encoding_dim,))
decoder_layer = autoencoder.layers[-4](encoded_input)
decoder_layer = autoencoder.layers[-3](decoder_layer)
decoder_layer = autoencoder.layers[-2](decoder_layer)
decoder_layer = autoencoder.layers[-1](decoder_layer)

decoder = Model(encoded_input, decoder_layer)

# Testing encoding
encoded_fts = encoder.predict(x_test)
decoded_fts = decoder.predict(encoded_fts)

最佳答案

My questions are, is strategy B valid or I am cheating on the reconstruction?

AB 是等效的;不,你没有作弊。

In case of strategy A, is Keras supposed to update the weights of the encoder and decoder models automatically since their models were built based on the autoencoder layers?

解码器模型仅使用自动编码器层。如果A:

decoder.layers
Out:
[<keras.engine.input_layer.InputLayer at 0x7f8a44d805c0>,
<keras.layers.core.Dense at 0x7f8a44e58400>,
<keras.layers.core.Dense at 0x7f8a44e746d8>,
<keras.layers.core.Dense at 0x7f8a44e14940>,
<keras.layers.core.Dense at 0x7f8a44e2dba8>]

autoencoder.layers
Out:[<keras.engine.input_layer.InputLayer at 0x7f8a44e91c18>,
<keras.layers.core.Dense at 0x7f8a44e91c50>,
<keras.layers.core.Dense at 0x7f8a44e91ef0>,
<keras.layers.core.Dense at 0x7f8a44e89080>,
<keras.layers.core.Dense at 0x7f8a44e89da0>,
<keras.layers.core.Dense at 0x7f8a44e58400>,
<keras.layers.core.Dense at 0x7f8a44e746d8>,
<keras.layers.core.Dense at 0x7f8a44e14940>,
<keras.layers.core.Dense at 0x7f8a44e2dba8>]

每个列表最后 4 行的十六进制数字(对象 ID)完全相同 - 因为它们是相同的对象。当然,他们也分享自己的体重。

如果B:

decoder.layers
Out:
[<keras.engine.input_layer.InputLayer at 0x7f8a41de05f8>,
<keras.layers.core.Dense at 0x7f8a41ee4828>,
<keras.layers.core.Dense at 0x7f8a41eaceb8>,
<keras.layers.core.Dense at 0x7f8a41e50ac8>,
<keras.layers.core.Dense at 0x7f8a41e5d780>]

autoencoder.layers
Out:
[<keras.engine.input_layer.InputLayer at 0x7f8a41da3940>,
<keras.layers.core.Dense at 0x7f8a41da3978>,
<keras.layers.core.Dense at 0x7f8a41da3a90>,
<keras.layers.core.Dense at 0x7f8a41da3b70>,
<keras.layers.core.Dense at 0x7f8a44720cf8>,
<keras.layers.core.Dense at 0x7f8a41ee4828>,
<keras.layers.core.Dense at 0x7f8a41eaceb8>,
<keras.layers.core.Dense at 0x7f8a41e50ac8>,
<keras.layers.core.Dense at 0x7f8a41e5d780>]

-层是相同的。

因此,AB 的训练顺序是等效的。更一般地说,如果共享层(以及权重),则在大多数情况下构建、编译和训练的顺序并不重要,因为它们位于同一个 tensorflow 图中。

我在 mnist 数据集上运行这个示例,它们显示出相同的性能并很好地重建图像。我想,如果您遇到了 case A 的麻烦,那么您已经错过了其他事情(不知道是怎么回事,因为我复制粘贴了您的代码,一切都正常)。

如果您使用 jupyter,有时重新启动并从上到下运行会有所帮助。

关于python - 在构建编码器/解码器模型之前训练自动编码器是否有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55312734/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com