How to train an autoencoder in a supervised manner?

159 views Asked by At

I have two datasets, one numeric and one semantic information of the numeric ones. I want to train an autoencoder to give latent embeddings that should match the semantic dataset. That is: ae_model = Model(input = X_tr, target = [X_tr, S_tr]) where S_tr is the semantic embedding that should match with the encoder outputs or the latent embeddings.

# Load the data
(x_train, y_train), (x_test,       y_test) =    tf.keras.datasets.mnist.load_data()

# Load the target embeddings
 target_embeddings = tf.keras.datasets.mnist.load_data()[1]

# Define the autoencoder
encoder = tf.keras.Sequential([
 tf.keras.layers.Flatten(input_shape=(28, 28)),
  tf.keras.layers.Dense(128, activation='relu'),
   tf.keras.layers.Dense(64, activation='relu'),

])

decoder = tf.keras.Sequential([
  tf.keras.layers.Dense(128, activation='relu'),
 tf.keras.layers.Dense(28 * 28, activation='sigmoid'),
 tf.keras.layers.Reshape((28, 28)),

])

ae_model = tf.keras.Model(encoder, decoder)

# Compile the autoencoder
   ae_model.compile(optimizer='adam', loss='mse')

# Train the autoencoder
ae_model.fit(x_train, target_embeddings, epochs=10)

I have tried this, but this passes Target_embeddings as the target, I want the latent embeddings match the target_embeddings, how can I do that.

1

There are 1 answers

0
Mortaza On

Try this one:

import tensorflow as tf

(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()

target_embeddings = tf.keras.datasets.mnist.load_data()[1]
# encoder block you provided
encoder = tf.keras.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(64, activation='relu')
])

# decoder block you provided
decoder = tf.keras.Sequential([
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(28 * 28, activation='sigmoid'),
    tf.keras.layers.Reshape((28, 28))
])

input_layer = tf.keras.layers.Input(shape=(28, 28))
latent_embedding = encoder(input_layer)
reconstructed_output = decoder(latent_embedding)

ae_model = tf.keras.Model(inputs=input_layer, outputs=[reconstructed_output, 
latent_embedding])

# We need to define loss for both encoder and decoder sides of the model:
# TODO: you can change these weights to get the best output 
loss_weights = {'reconstructed_output': 1.0, 'latent_embedding': 0.1}

ae_model.compile(optimizer='adam', loss={'reconstructed_output': 'mse', 
'latent_embedding': 'mse'}, loss_weights=loss_weights)

ae_model.fit(x_train, {'reconstructed_output': x_train, 'latent_embedding': 
target_embeddings}, epochs=10)