簡體   English   中英

Keras變分自動編碼器示例 - 潛在輸入的使用

[英]Keras variational autoencoder example - usage of latent input

我是Keras的新手,並且在他們的官方github的變量autoencoder示例中一直在努力理解變量z的用法。 我不明白為什么不使用z代替變量latent_inputs 我運行代碼,它似乎工作,但我不明白z是否在幕后使用,以及Keras負責它的機制是什么。 以下是相關的代碼段:

# VAE model = encoder + decoder
# build encoder model
inputs = Input(shape=input_shape, name='encoder_input')
x = Dense(intermediate_dim, activation='relu')(inputs)
z_mean = Dense(latent_dim, name='z_mean')(x)
z_log_var = Dense(latent_dim, name='z_log_var')(x)

# use reparameterization trick to push the sampling out as input
# note that "output_shape" isn't necessary with the TensorFlow backend
z = Lambda(sampling, output_shape=(latent_dim,), name='z')([z_mean, z_log_var])

# instantiate encoder model
encoder = Model(inputs, [z_mean, z_log_var, z], name='encoder')
encoder.summary()
plot_model(encoder, to_file='vae_mlp_encoder.png', show_shapes=True)

# build decoder model
latent_inputs = Input(shape=(latent_dim,), name='z_sampling')
x = Dense(intermediate_dim, activation='relu')(latent_inputs)
outputs = Dense(original_dim, activation='sigmoid')(x)

# instantiate decoder model
decoder = Model(latent_inputs, outputs, name='decoder')
decoder.summary()
plot_model(decoder, to_file='vae_mlp_decoder.png', show_shapes=True)

# instantiate VAE model
outputs = decoder(encoder(inputs)[2])
vae = Model(inputs, outputs, name='vae_mlp')

您的encoder被定義為一個模型,它接受輸入inputs並給出輸出[z_mean, z_log_var, z] 然后,您可以單獨定義解碼器以獲取一些輸入,此處稱為latent_inputs和輸出outputs 最后,您的整體模型在以下行中定義:

outputs = decoder(encoder(inputs)[2])

這意味着你將在你的inputs上運行encoder ,產生[z_mean, z_log_var, z] ,然后它的第三個元素(稱為result[2] )作為decoder的輸入參數傳入。 換句話說,當您實現網絡時,您將latent_inputs設置latent_inputs等於編碼器的第三個輸出,或者[z_mean, z_log_var, z][2] = z 您可以將其視為(可能不是有效的代碼):

encoder_outputs = encoder(inputs)  # [z_mean, z_log_var, z]
outputs = decoder(latent_inputs=encoder_outputs[2])  # latent_inputs = z

它們只是單獨定義編碼器和解碼器,因此它們可以單獨使用:

  • 給定一些inputsencoder計算它們的潛在向量/低表示z_mean, z_log_var, z (您可以z_mean, z_log_var, z使用encoder ,例如存儲那些低維表示,或者為了更容易比較)。

  • 給定這樣的較低維度表示latent_inputsdecoder返回解碼的信息outputs (例如,如果您需要重用存儲的較低表示)。

為了訓練/使用完整的VAE,兩種操作都可以按照實際的方式進行鏈接: outputs = decoder(encoder(inputs)[2])decoder latent_inputs接收encoderz輸出)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM