我尝试将序列与序列模型结合使用VAE,我的代码是:
encoder_inputs = Input(shape=(None,))
encoder_emb = Embedding(input_dim=vocab_in_size,
output_dim=embedding_dim)
encoder_lstm =LSTM(units=units, return_sequences=False, return_state=True)
encoder_outputs, state_h, state_c =
encoder_lstm(encoder_emb(encoder_inputs))
encoder_states = [state_h, state_c]
latent_dim =256
# output layer for mean and log variance
z_mu = Dense(latent_dim)(encoder_outputs) #remplacer h
z_log_var = Dense(latent_dim)(encoder_outputs)
def sampling(args):
batch_size=1
z_mean, z_log_sigma = args
epsilon = K.random_normal(shape=(batch_size, latent_dim),
mean=0., stddev=1.)
return z_mean + K.exp(z_log_sigma/2) * epsilon
z = Lambda(sampling, output_shape=(None,))([z_mu, z_log_var])
def vae_loss(y_true, y_pred):
# E[log P(X|z)]
recon = K.sum(K.binary_crossentropy(y_pred, y_true), axis=-1)
kl_loss = - 0.5 * K.sum(1 + z_log_var - K.square(z_mean) -
K.exp(z_log_var), axis=-1)
return recon + kl_loss
decoder_inputs = Input(shape=(None,))
decoder_emb = Embedding(input_dim=vocab_out_size, output_dim=embedding_dim)
decoder_lstm = LSTM(units=units, return_sequences=True, return_state=True)
decoder_lstm_out, _, _ = decoder_lstm(decoder_emb(decoder_inputs), initial_state=encoder_states)
decoder_d2 = Dense(vocab_out_size, activation="softmax")
decoder_out = decoder_d2(decoder_lstm_out)
model = Model([encoder_inputs, decoder_inputs], decoder_out)
model.compile(optimizer='rmsprop',
loss=vae_loss, metrics=
['accuracy'])
我想将表示形式潜在z传递给解码器。我发现的所有示例在解码器中都没有嵌入层。所以,我有关于形状的错误。我应该在哪里确切使用z? 拜托我需要你的帮忙 谢谢。