deepmorpheus/model.py
def forward(self, sentence):
        """The main forward function, this does the actual heavy lifting"""
        words = torch.tensor([word for word, _, _ in sentence]).to(self.device)

        word_embeddings = self.word_embeddings(words)
        word_embeddings_bs = word_embeddings.view(len(sentence), self.hparams.batch_size, self.hparams.word_embedding_dim)

        word_repr = []
        if not self.hparams.disable_char_level:
            for word_idx in range(len(sentence)):
                self.init_char_hidden() # Don't store representation between words
                chars = torch.tensor(sentence[word_idx][1]).to(self.device)

                # Character-level representation is the LSTM output of the last character.
                chars_repr = None
                for char in chars:
                    char_embed = self.char_embeddings(char)
                    chars_repr, self.char_lstm_hidden = self.char_lstm(
                        char_embed.view(1, self.hparams.batch_size, self.hparams.char_embedding_dim), self.char_lstm_hidden
                    )

                chars_repr = chars_repr.view(1, self.hparams.char_lstm_hidden_dim * self.directions)

                word_repr.append(word_embeddings[0].unsqueeze(0))
                word_repr.append(chars_repr)
        else:
            for word_idx in range(len(sentence)):
                word_repr.append(word_embeddings[0].unsqueeze(0))

        # Each sentence embedding dimensions are word embedding dimensions + character representation dimensions
        word_repr = torch.cat(word_repr, dim=1) # From row to column

        sentence_repr, self.word_lstm_hidden = self.word_lstm(
            word_repr.view(len(sentence), self.hparams.batch_size, self.word_lstm_input_dim),
            self.word_lstm_hidden,
        )

        sentence_repr = sentence_repr.view(len(sentence), self.hparams.word_lstm_hidden_dim * self.directions)

        all_word_scores = [[] for _ in range(len(sentence))]

        for tag_name in self.vocab.tag_names:
            hidden_output = self.tag_fc[tag_name](sentence_repr)
            tag_scores = F.log_softmax(hidden_output, dim=1)
            
            for word_idx, word_scores in enumerate(tag_scores):
                all_word_scores[word_idx].append(word_scores)

        # Shape: (sentence_len, 9, num_tag_output)
        return all_word_scores
The main forward function, this does the actual heavy lifting
The main forward function, this does the actual heavy lifting
The main forward function this does the actual heavy lifting
def forward(self, sentence):
        words = torch.tensor([word for word, _, _ in sentence]).to(self.device)
        word_embeddings = self.word_embeddings(words)
        word_embeddings_bs = word_embeddings.view(len(sentence), self.hparams.batch_size, self.hparams.word_embedding_dim)
        word_repr = []
        if not self.hparams.disable_char_level:
            for word_idx in range(len(sentence)):
                self.init_char_hidden() 
                chars = torch.tensor(sentence[word_idx][1]).to(self.device)
                chars_repr = None
                for char in chars:
                    char_embed = self.char_embeddings(char)
                    chars_repr, self.char_lstm_hidden = self.char_lstm(
                        char_embed.view(1, self.hparams.batch_size, self.hparams.char_embedding_dim), self.char_lstm_hidden
                    )
                chars_repr = chars_repr.view(1, self.hparams.char_lstm_hidden_dim * self.directions)
                word_repr.append(word_embeddings[0].unsqueeze(0))
                word_repr.append(chars_repr)
        else:
            for word_idx in range(len(sentence)):
                word_repr.append(word_embeddings[0].unsqueeze(0))
        word_repr = torch.cat(word_repr, dim=1) 
        sentence_repr, self.word_lstm_hidden = self.word_lstm(
            word_repr.view(len(sentence), self.hparams.batch_size, self.word_lstm_input_dim),
            self.word_lstm_hidden,
        )
        sentence_repr = sentence_repr.view(len(sentence), self.hparams.word_lstm_hidden_dim * self.directions)
        all_word_scores = [[] for _ in range(len(sentence))]
        for tag_name in self.vocab.tag_names:
            hidden_output = self.tag_fc[tag_name](sentence_repr)
            tag_scores = F.log_softmax(hidden_output, dim=1)
            for word_idx, word_scores in enumerate(tag_scores):
                all_word_scores[word_idx].append(word_scores)
        return all_word_scores
def forward ( self , sentence ) : words = torch . tensor ( [ word for word , _ , _ in sentence ] ) . to ( self . device ) word_embeddings = self . word_embeddings ( words ) word_embeddings_bs = word_embeddings . view ( len ( sentence ) , self . hparams . batch_size , self . hparams . word_embedding_dim ) word_repr = [ ] if not self . hparams . disable_char_level : for word_idx in range ( len ( sentence ) ) : self . init_char_hidden ( ) chars = torch . tensor ( sentence [ word_idx ] [ 1 ] ) . to ( self . device ) chars_repr = None for char in chars : char_embed = self . char_embeddings ( char ) chars_repr , self . char_lstm_hidden = self . char_lstm ( char_embed . view ( 1 , self . hparams . batch_size , self . hparams . char_embedding_dim ) , self . char_lstm_hidden ) chars_repr = chars_repr . view ( 1 , self . hparams . char_lstm_hidden_dim * self . directions ) word_repr . append ( word_embeddings [ 0 ] . unsqueeze ( 0 ) ) word_repr . append ( chars_repr ) else : for word_idx in range ( len ( sentence ) ) : word_repr . append ( word_embeddings [ 0 ] . unsqueeze ( 0 ) ) word_repr = torch . cat ( word_repr , dim = 1 ) sentence_repr , self . word_lstm_hidden = self . word_lstm ( word_repr . view ( len ( sentence ) , self . hparams . batch_size , self . word_lstm_input_dim ) , self . word_lstm_hidden , ) sentence_repr = sentence_repr . view ( len ( sentence ) , self . hparams . word_lstm_hidden_dim * self . directions ) all_word_scores = [ [ ] for _ in range ( len ( sentence ) ) ] for tag_name in self . vocab . tag_names : hidden_output = self . tag_fc [ tag_name ] ( sentence_repr ) tag_scores = F . log_softmax ( hidden_output , dim = 1 ) for word_idx , word_scores in enumerate ( tag_scores ) : all_word_scores [ word_idx ] . append ( word_scores ) return all_word_scores
The main forward function, this does the actual heavy lifting
The main forward function this does the actual heavy lifting
"""The main forward function, this does the actual heavy lifting""" # Don't store representation between words # Character-level representation is the LSTM output of the last character. # Each sentence embedding dimensions are word embedding dimensions + character representation dimensions # From row to column # Shape: (sentence_len, 9, num_tag_output)
self
sentence