singel layer for encoder.each layers has two sub-layers: the first is multi-head self-attention mechanism; the second is position-wise fully connected feed-forward network. for each sublayer. use LayerNorm(x+Sublayer(x)). input and output of last dimension: d_model
(self,Q,K_s,layer_index)
| 43 | return Q,K_s |
| 44 | |
| 45 | def encoder_single_layer(self,Q,K_s,layer_index): |
| 46 | """ |
| 47 | singel layer for encoder.each layers has two sub-layers: |
| 48 | the first is multi-head self-attention mechanism; the second is position-wise fully connected feed-forward network. |
| 49 | for each sublayer. use LayerNorm(x+Sublayer(x)). input and output of last dimension: d_model |
| 50 | :param Q: shape should be: [batch_size*sequence_length,d_model] |
| 51 | :param K_s: shape should be: [batch_size*sequence_length,d_model] |
| 52 | :return:output: shape should be:[batch_size*sequence_length,d_model] |
| 53 | """ |
| 54 | #1.1 the first is multi-head self-attention mechanism |
| 55 | multi_head_attention_output=self.sub_layer_multi_head_attention(layer_index,Q,K_s,self.type,mask=self.mask,dropout_keep_prob=self.dropout_keep_prob) #[batch_size,sequence_length,d_model] |
| 56 | #1.2 use LayerNorm(x+Sublayer(x)). all dimension=512. |
| 57 | multi_head_attention_output=self.sub_layer_layer_norm_residual_connection(K_s ,multi_head_attention_output,layer_index,'encoder_multi_head_attention',dropout_keep_prob=self.dropout_keep_prob,use_residual_conn=self.use_residual_conn) |
| 58 | |
| 59 | #2.1 the second is position-wise fully connected feed-forward network. |
| 60 | postion_wise_feed_forward_output=self.sub_layer_postion_wise_feed_forward(multi_head_attention_output,layer_index,self.type) |
| 61 | #2.2 use LayerNorm(x+Sublayer(x)). all dimension=512. |
| 62 | postion_wise_feed_forward_output= self.sub_layer_layer_norm_residual_connection(multi_head_attention_output,postion_wise_feed_forward_output,layer_index,'encoder_postion_wise_ff',dropout_keep_prob=self.dropout_keep_prob) |
| 63 | return postion_wise_feed_forward_output,postion_wise_feed_forward_output |
| 64 | |
| 65 | |
| 66 | def init(): |
no test coverage detected