def get_encoder_layers(name,
                       input_layer,
                       head_num,
                       hidden_dim,
                       attention_activation=None,
                       feed_forward_activation='relu',
                       dropout_rate=0.0,
                       trainable=True,
                       use_star=False,
                       use_adapter=False,
                       adapter_units=None,
                       adapter_activation='relu'):
    """Multi-head self-attention and feed-forward layer.

    :param name: Prefix of names for internal layers.
    :param input_layer: Input layer.
    :param head_num: Number of heads in multi-head self-attention.
    :param hidden_dim: Hidden dimension of feed forward layer.
    :param attention_activation: Activation for multi-head self-attention.
    :param feed_forward_activation: Activation for feed-forward layer.
    :param dropout_rate: Dropout rate.
    :param trainable: Whether the layers are trainable.
    :param use_star:Whether to use star-transformer
    :param use_adapter: if use star-transformer, use_adapter=True. Whether to use feed-forward adapters before each residual connections.
    :param adapter_units: The dimension of the first transformation in feed-forward adapter.
    :param adapter_activation: The activation after the first transformation in feed-forward adapter.
    :return: Output layer.
    """
    if use_star:
        attention_name_1 = '%s-1-MultiHeadSelfAttention' % name
        attention_name_2 = '%s-2-MultiHeadSelfAttention' % name
        # (batch_size, seq_len, d_model) = keras.backend.int_shape(input_layer)
        # h_extand = keras.backend.zeros((batch_size, seq_len + 2, d_model), dtype=keras.backend.floatx())
        # h_extand[:, 1:seq_len + 1, :] = input_layer  # head and tail padding(not cycle)
        # input_layer = input_layer.reshape([batch_size, 1, d_model])
        # s_expand = input_layer.expand([batch_size, seq_len, d_model])
        # context = keras.backend.concatenate((h_extand[:, 0:seq_len, :],
        #                                      h_extand[:, 1:seq_len + 1, :],
        #                                      h_extand[:, 2:seq_len + 2, :],
        #                                      input_layer,
        #                                      s_expand), 2)
        # context = context.reshape([batch_size * seq_len, 5, d_model])
        # h = input_layer.reshape([batch_size * seq_len, 1, d_model])
        #
        # h, _ = self.slf_attn_satellite(

        attention_layer = _common_wrap_layer(name=attention_name_1,
                                             input_layer=input_layer,
                                             build_func=build_attention(name=attention_name_1,
                                                                        head_num=head_num,
                                                                        activation=attention_activation,
                                                                        history_only=False,
                                                                        trainable=trainable,
                                                                        ),
                                            dropout_rate=dropout_rate,
                                            trainable=trainable,
                                            use_star=use_star,
                                            use_adapter=use_adapter,
                                            adapter_units=adapter_units,
                                            adapter_activation=adapter_activation,)
        feed_forward_layer = _common_wrap_layer(name=attention_name_2,
                                                input_layer=attention_layer,
                                                build_func=build_attention(name=attention_name_2,
                                                                           head_num=head_num,
                                                                           activation=attention_activation,
                                                                           history_only=False,
                                                                           trainable=trainable,
                                                                           ),
                                                dropout_rate=dropout_rate,
                                                trainable=trainable,
                                                use_star=use_star,
                                                use_adapter=use_adapter,
                                                adapter_units=adapter_units,
                                                adapter_activation=adapter_activation,)
    else:
        attention_name = '%s-MultiHeadSelfAttention' % name
        feed_forward_name = '%s-FeedForward' % name
        attention_layer = _common_wrap_layer(name=attention_name,
                                             input_layer=input_layer,
                                             build_func=build_attention(name=attention_name,
                                                                        head_num=head_num,
                                                                        activation=attention_activation,
                                                                        history_only=False,
                                                                        trainable=trainable,
                                                                        ),
                                            dropout_rate=dropout_rate,
                                            trainable=trainable,
                                            use_star=use_star,
                                            use_adapter=use_adapter,
                                            adapter_units=adapter_units,
                                            adapter_activation=adapter_activation,)
        feed_forward_layer = _common_wrap_layer(name=feed_forward_name,
                                                input_layer=attention_layer,
                                                build_func=build_feed_forward(name=feed_forward_name,
                                                                              hidden_dim=hidden_dim,
                                                                              activation=feed_forward_activation,
                                                                              trainable=trainable,),
                                                dropout_rate=dropout_rate,
                                                trainable=trainable,
                                                use_star=use_star,
                                                use_adapter=use_adapter,
                                                adapter_units=adapter_units,
                                                adapter_activation=adapter_activation,)
    return feed_forward_layer
Multi-head self-attention and feed-forward layer.

    :param name: Prefix of names for internal layers.
    :param input_layer: Input layer.
    :param head_num: Number of heads in multi-head self-attention.
    :param hidden_dim: Hidden dimension of feed forward layer.
    :param attention_activation: Activation for multi-head self-attention.
    :param feed_forward_activation: Activation for feed-forward layer.
    :param dropout_rate: Dropout rate.
    :param trainable: Whether the layers are trainable.
    :param use_star:Whether to use star-transformer
    :param use_adapter: if use star-transformer, use_adapter=True. Whether to use feed-forward adapters before each residual connections.
    :param adapter_units: The dimension of the first transformation in feed-forward adapter.
    :param adapter_activation: The activation after the first transformation in feed-forward adapter.
    :return: Output layer.
    
keras_textclassification/keras_layers/transformer.py