def __init__( self, dictionary, word_dropout_params=None, embed_dim=512, freeze_embed=False, hidden_dim=512, num_layers=1, cell_type="lstm", dropout_in=0.1, dropout_out=0.1, residual_level=None, bidirectional=False, add_encoder_output_as_decoder_input=False, char_rnn_params=None, ): super().__init__(dictionary) self.dictionary = dictionary self.dropout_in = dropout_in self.dropout_out = dropout_out self.residual_level = residual_level self.hidden_dim = hidden_dim self.bidirectional = bidirectional self.add_encoder_output_as_decoder_input = add_encoder_output_as_decoder_input num_embeddings = len(dictionary) self.padding_idx = dictionary.pad() self.char_rnn_params = char_rnn_params if self.char_rnn_params is not None: self.char_rnn_encoder = char_rnn_encoder.CharRNN( dictionary=self.dictionary, embed_dim=embed_dim, hidden_dim=char_rnn_params["char_rnn_units"], num_layers=char_rnn_params["char_rnn_layers"], bidirectional=True, word_delimiter=char_rnn_params["word_delimiter"], ) self.word_dim = char_rnn_params["char_rnn_units"] else: self.embed_tokens = Embedding( num_embeddings=num_embeddings, embedding_dim=embed_dim, padding_idx=self.padding_idx, freeze_embed=freeze_embed, ) self.word_dim = embed_dim self.cell_type = cell_type self.layers = nn.ModuleList([]) for layer in range(num_layers): self.layers.append( RNNLayer( self.word_dim if layer == 0 else hidden_dim, hidden_dim, self.cell_type, True if bidirectional and layer == 0 else False, )) self.num_layers = len(self.layers) self.word_dropout_module = None if (word_dropout_params and word_dropout_params["word_dropout_freq_threshold"] is not None and word_dropout_params["word_dropout_freq_threshold"] > 0): self.word_dropout_module = word_dropout.WordDropout( dictionary, word_dropout_params)
def __init__( self, dictionary, embed_dim=512, freeze_embed=False, cell_type="lstm", hidden_dim=512, num_layers=1, dropout_in=0.1, dropout_out=0.1, residual_level=None, bidirectional=False, word_dropout_params=None, padding_value=0, char_rnn_params=None, ): assert cell_type == "lstm", 'sequence-lstm requires cell_type="lstm"' super().__init__(dictionary) self.dictionary = dictionary self.dropout_in = dropout_in self.dropout_out = dropout_out self.residual_level = residual_level self.hidden_dim = hidden_dim self.bidirectional = bidirectional num_embeddings = len(dictionary) self.padding_idx = dictionary.pad() self.padding_value = padding_value self.char_rnn_params = char_rnn_params if self.char_rnn_params is not None: self.char_rnn_encoder = char_rnn_encoder.CharRNN( dictionary=self.dictionary, embed_dim=embed_dim, hidden_dim=char_rnn_params["char_rnn_units"], num_layers=char_rnn_params["char_rnn_layers"], bidirectional=True, word_delimiter=char_rnn_params["word_delimiter"], ) self.word_dim = char_rnn_params["char_rnn_units"] else: self.embed_tokens = Embedding( num_embeddings=num_embeddings, embedding_dim=embed_dim, padding_idx=self.padding_idx, freeze_embed=freeze_embed, ) self.word_dim = embed_dim self.layers = nn.ModuleList([]) for layer in range(num_layers): is_layer_bidirectional = self.bidirectional and layer == 0 self.layers.append( LSTMSequenceEncoder.LSTM( self.word_dim if layer == 0 else hidden_dim, hidden_dim // 2 if is_layer_bidirectional else hidden_dim, num_layers=1, dropout=self.dropout_out, bidirectional=is_layer_bidirectional, )) self.num_layers = len(self.layers) self.word_dropout_module = None if (word_dropout_params and word_dropout_params["word_dropout_freq_threshold"] is not None and word_dropout_params["word_dropout_freq_threshold"] > 0): self.word_dropout_module = word_dropout.WordDropout( dictionary, word_dropout_params)