encoder decoder 模型是比较难理解的,理解这个模型需要清楚lstm 的整个源码细节,坦率的说这个模型我看了近十天,不敢说完全明白。
- 我把细胞的有丝分裂的图片放在开头,我的直觉细胞的有丝分裂和这个模型有相通之处
定义训练编码器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | ######################################################################################################################################################### # 定义训练编码器 #None表示可以处理任意长度的序列 # num_encoder_tokens表示特征的数目,三维张量的列数 encoder_inputs = Input(shape=(None, num_encoder_tokens),name='encoder_inputs') # 编码器,要求其返回状态,lstm 公式理解https://blog.csdn.net/qq_38210185/article/details/79376053 encoder = LSTM(latent_dim, return_state=True,name='encoder_LSTM') # 编码器的特征维的大小latent_dim,即单元数,也可以理解为lstm的层数 #lstm 的输出状态,隐藏状态,候选状态 encoder_outputs, state_h, state_c = encoder(encoder_inputs) # 取出输入生成的隐藏状态和细胞状态,作为解码器的隐藏状态和细胞状态的初始化值。 #上面两行那种写法很奇怪,看了几天没看懂,可以直接这样写 #encoder_outputs, state_h, state_c= LSTM(latent_dim, return_state=True,name='encoder_LSTM')(encoder_inputs) # 我们丢弃' encoder_output ',只保留隐藏状态,候选状态 encoder_states = [state_h, state_c] ######################################################################################################################################################### |
定义训练解码器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | ######################################################################################################################################################### # 定义解码器的输入 # 同样的,None表示可以处理任意长度的序列 # 设置解码器,使用' encoder_states '作为初始状态 # num_decoder_tokens表示解码层嵌入长度,三维张量的列数 decoder_inputs = Input(shape=(None, num_decoder_tokens),name='decoder_inputs') # 接下来建立解码器,解码器将返回整个输出序列 # 并且返回其中间状态,中间状态在训练阶段不会用到,但是在推理阶段将是有用的 # 因解码器用编码器的隐藏状态和细胞状态,所以latent_dim必等 decoder_lstm = LSTM(latent_dim, return_sequences=True, return_state=True,name='decoder_LSTM') # 将编码器输出的状态作为初始解码器的初始状态 decoder_outputs, _, _ = decoder_lstm(decoder_inputs, initial_state=encoder_states) # 添加全连接层 # 这个full层在后面推断中会被共享!! decoder_dense = Dense(num_decoder_tokens, activation='softmax',name='softmax') decoder_outputs = decoder_dense(decoder_outputs) ######################################################################################################################################################### |
定义训练模型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | # Define the model that will turn # `encoder_input_data` & `decoder_input_data` into `decoder_target_data` #################################################################################################################### model = Model([encoder_inputs, decoder_inputs], decoder_outputs) #原始模型 #################################################################################################################### #model.load_weights('s2s.h5') # Run training model.compile(optimizer='rmsprop', loss='categorical_crossentropy') model.fit([encoder_input_data, decoder_input_data], decoder_target_data, batch_size=batch_size, epochs=epochs, validation_split=0.2) # 保存模型 model.save('s2s.h5') |
显示模型的拓扑图
1 2 | import netron netron.start("s2s.h5") |
编码器,狭隘的说就是怎么将字符编码,注意输出是 encoder_states = [state_h, state_c] , 根据输入序列得到隐藏状态和候选门状态,输出是一个二元列表
1 2 3 4 5 6 7 | # 定义推断编码器 根据输入序列得到隐藏状态和细胞状态的路径图,得到模型,使用的输入到输出之间所有层的权重,与tf的预测签名一样 #################################################################################################################### encoder_model = Model(encoder_inputs, encoder_states) #编码模型 ,注意输出是 encoder_states = [state_h, state_c] #################################################################################################################### encoder_model.save('encoder_model.h5') |
1 2 | import netron netron.start('encoder_model.h5') |
注意输出是 encoder_states = [state_h, state_c] =[encoder_LSTM:1,: encoder_LSTM:2]
解码模型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #解码的隐藏层 decoder_state_input_h = Input(shape=(latent_dim,)) #解码的候选门 decoder_state_input_c = Input(shape=(latent_dim,)) #解码的输入状态 decoder_states_inputs = [decoder_state_input_h, decoder_state_input_c] decoder_outputsd, state_hd, state_cd = decoder_lstm(decoder_inputs, initial_state=decoder_states_inputs) decoder_statesd = [state_hd, state_cd] decoder_outputsd1 = decoder_dense(decoder_outputsd) decoder_model = Model( [decoder_inputs] + decoder_states_inputs, [decoder_outputsd] + decoder_statesd) |
解码模型是一个三输入,三输出的模型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | # 将输入编码为状态向量 #注意输出是 encoder_states = [state_h, state_c] # states_value 是一个有两元素的列表,每个元素的维度是256 states_value = encoder_model.predict(input_seq) # 生成长度为1的空目标序列 target_seq = np.zeros((1, 1, num_decoder_tokens)) # 用起始字符填充目标序列的第一个字符。 target_seq[0, 0, target_token_index['\t']] = 1. # 对一批序列的抽样循环(为了简化,这里我们假设批大小为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 # 退出条件:到达最大长度或找到停止字符。 if (sampled_char == '\n' or len(decoded_sentence) > max_decoder_seq_length): stop_condition = True # 更新目标序列(长度1) target_seq = np.zeros((1, 1, num_decoder_tokens)) target_seq[0, 0, sampled_token_index] = 1. print(sampled_token_index) # 更新状态 states_value = [h, c] |
我们可以想一想为什么要用下面这段代码
当输入input_seq 之后,就得到了encoder_states ,在之后一直共享这个数值
然后就像解纽扣那样,先找到第一个纽扣,就是’\t’,在target_text中’\t’就是第一个字符
【’\t’,states_value】–>下一个字符 c1
【‘c1’,states_value】–>下一个字符 c2
while循环一直到最后
代码在git