I am following the tutorial https://blog.keras.io/building-autoencoders-in-keras.html to build my autoencoder. For that, I have two strategies:
A) Step 1: build autoencoder; Step 2: build encoder; Step 3: build decoder; Step 4: compile autoencoder; Step 5: train autoencoder.
B) Step 1: build autoencoder; Step 2: compile autoencoder; Step 3: train autoencoder; Step 4: build encoder; Step 5: build decoder.
For both cases the model converges to loss 0.100. However, in case of strategy A, which is the one stated in the tutorial, the reconstruction is very poor. In case of strategy B the reconstruction is much better.
In my opinion this makes sense because in strategy A the weights of the encoder and decoder models were built over untrained layers and the result is random. In strategy B, on the other hand, I have the weights better defined after training, hence the reconstruction is better.
My questions are, is strategy B valid or I am cheating on the reconstruction? 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?
###### 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)