在使用 的模型中RepeatVector
,他们没有使用任何花哨的预测,也没有处理状态。他们让模型在内部完成所有操作,并且RepeatVector
用于将(batch, latent_dim)
向量(不是序列)转换为 a (batch, timesteps, latent_dim)
(现在是正确的序列)。
现在,在另一个没有 的模型中,RepeatVector
秘密在于这个附加功能:
def decode_sequence(input_seq):
# Encode the input as state vectors.
states_value = encoder_model.predict(input_seq)
# Generate empty target sequence of length 1.
target_seq = np.zeros((1, 1, num_decoder_tokens))
# Populate the first character of target sequence with the start character.
target_seq[0, 0, target_token_index['\t']] = 1.
# Sampling loop for a batch of sequences
# (to simplify, here we assume a batch of size 1).
stop_condition = False
decoded_sentence = ''
while not stop_condition:
output_tokens, h, c = decoder_model.predict([target_seq] + states_value)
# Sample a token
sampled_token_index = np.argmax(output_tokens[0, -1, :])
sampled_char = reverse_target_char_index[sampled_token_index]
decoded_sentence += sampled_char
# Exit condition: either hit max length
# or find stop character.
if (sampled_char == '\n' or len(decoded_sentence) > max_decoder_seq_length):
stop_condition = True
# Update the target sequence (of length 1).
target_seq = np.zeros((1, 1, num_decoder_tokens))
target_seq[0, 0, sampled_token_index] = 1.
# Update states
states_value = [h, c]
return decoded_sentence
这会运行一个基于 a 的“循环”,stop_condition
用于一个一个地创建时间步长。(这样做的好处是使句子没有固定长度)。
它还显式地获取每个步骤中生成的状态(以保持每个单独步骤之间的正确连接)。
简而言之:
- 模型 1:通过重复潜在向量来创建长度
- 模型 2:通过循环新步骤直到达到停止条件来创建长度