python 如何以监督的方式训练自动编码器?

k10s72fa  于 2023-05-21  发布在  Python
关注(0)|答案(1)|浏览(147)

我有两个数据集,一个是数字数据集,另一个是数字数据集的语义信息。我想训练一个自动编码器来给予应该匹配语义数据集的潜在嵌入。也就是说:ae_model = Model(input = X_tr,target = [X_tr,S_tr])其中S_tr是应该与编码器输出或潜在嵌入匹配的语义嵌入。

# 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)

我已经尝试过了,但是它传递了Target_embeddings作为目标,我希望潜在的嵌入与target_embeddings匹配,我该怎么做呢?

mi7gmzs6

mi7gmzs61#

试试这个:

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)

相关问题