def get_recurrent_encoder(config: RecurrentEncoderConfig, prefix: str) -> 'Encoder':
    """
    Returns an encoder stack with a bi-directional RNN, and a variable number of uni-directional forward RNNs.

    :param config: Configuration for recurrent encoder.
    :param prefix: Prefix for variable names.
    :return: Encoder instance.
    """
    # TODO give more control on encoder architecture
    encoder_seq = EncoderSequence([], config.dtype)

    if config.conv_config is not None:
        encoder_seq.append(ConvolutionalEmbeddingEncoder, config=config.conv_config,
                           prefix=prefix + C.CHAR_SEQ_ENCODER_PREFIX)
        if config.conv_config.add_positional_encoding:
            # If specified, add positional encodings to segment embeddings
            encoder_seq.append(AddSinCosPositionalEmbeddings,
                               num_embed=config.conv_config.num_embed,
                               scale_up_input=False,
                               scale_down_positions=False,
                               prefix="%s%sadd_positional_encodings" % (prefix, C.CHAR_SEQ_ENCODER_PREFIX))
        encoder_seq.append(ConvertLayout, infer_hidden=True, target_layout=C.TIME_MAJOR)
    else:
        encoder_seq.append(ConvertLayout, target_layout=C.TIME_MAJOR, num_hidden=0)

    if config.reverse_input:
        encoder_seq.append(ReverseSequence, infer_hidden=True)

    if config.rnn_config.residual:
        utils.check_condition(config.rnn_config.first_residual_layer >= 2,
                              "Residual connections on the first encoder layer are not supported")

    # One layer bi-directional RNN:
    encoder_seq.append(BiDirectionalRNNEncoder,
                       rnn_config=config.rnn_config.copy(num_layers=1),
                       prefix=prefix + C.BIDIRECTIONALRNN_PREFIX,
                       layout=C.TIME_MAJOR)

    if config.rnn_config.num_layers > 1:
        # Stacked uni-directional RNN:
        # Because we already have a one layer bi-rnn we reduce the num_layers as well as the first_residual_layer.
        remaining_rnn_config = config.rnn_config.copy(num_layers=config.rnn_config.num_layers - 1,
                                                      first_residual_layer=config.rnn_config.first_residual_layer - 1)
        encoder_seq.append(RecurrentEncoder,
                           rnn_config=remaining_rnn_config,
                           prefix=prefix + C.STACKEDRNN_PREFIX,
                           layout=C.TIME_MAJOR)

    encoder_seq.append(ConvertLayout, infer_hidden=True, target_layout=C.BATCH_MAJOR)

    return encoder_seq
def get_recurrent_encoder ( config : RecurrentEncoderConfig , prefix : str ) -> 'Encoder' : encoder_seq = EncoderSequence ( [ ] , config . dtype ) if config . conv_config is not None : encoder_seq . append ( ConvolutionalEmbeddingEncoder , config = config . conv_config , prefix = prefix + C . CHAR_SEQ_ENCODER_PREFIX ) if config . conv_config . add_positional_encoding : encoder_seq . append ( AddSinCosPositionalEmbeddings , num_embed = config . conv_config . num_embed , scale_up_input = False , scale_down_positions = False , prefix = "%s%sadd_positional_encodings" % ( prefix , C . CHAR_SEQ_ENCODER_PREFIX ) ) encoder_seq . append ( ConvertLayout , infer_hidden = True , target_layout = C . TIME_MAJOR ) else : encoder_seq . append ( ConvertLayout , target_layout = C . TIME_MAJOR , num_hidden = 0 ) if config . reverse_input : encoder_seq . append ( ReverseSequence , infer_hidden = True ) if config . rnn_config . residual : utils . check_condition ( config . rnn_config . first_residual_layer >= 2 , "Residual connections on the first encoder layer are not supported" ) encoder_seq . append ( BiDirectionalRNNEncoder , rnn_config = config . rnn_config . copy ( num_layers = 1 ) , prefix = prefix + C . BIDIRECTIONALRNN_PREFIX , layout = C . TIME_MAJOR ) if config . rnn_config . num_layers > 1 : remaining_rnn_config = config . rnn_config . copy ( num_layers = config . rnn_config . num_layers - 1 , first_residual_layer = config . rnn_config . first_residual_layer - 1 ) encoder_seq . append ( RecurrentEncoder , rnn_config = remaining_rnn_config , prefix = prefix + C . STACKEDRNN_PREFIX , layout = C . TIME_MAJOR ) encoder_seq . append ( ConvertLayout , infer_hidden = True , target_layout = C . BATCH_MAJOR ) return encoder_seq
Returns an encoder stack with a bi-directional RNN, and a variable number of uni-directional forward RNNs.

    :param config: Configuration for recurrent encoder.
    :param prefix: Prefix for variable names.
    :return: Encoder instance.