nlpmodels/models/text_cnn.py
def forward(self, data: Tuple) -> torch.Tensor:
        """
        The main function call of the model.

        Args:
            data (Tuple):
                a Tuple containing a tensor of size (batch_size, max_sequence_length)
        Returns:
            a tensor of (batch_size, class_size) of probability predictions
        """
        # this is (target, text) tuple. done for trainer normalization
        data = data[1]

        # (1) get embeddings
        # (batch_size, max_sequence_length) ->
        # (batch_size,max_sequence_length, dim_model)
        embeddings = self._embeddings(data)

        # (batch_size, max_sequence_length, dim_model) ->
        # (batch_size, channels_in=1, max_sequence_length, dim_model)
        conv_input = embeddings.unsqueeze(1)

        conv_stack = []
        for conv_layer in self._convs:
            # (2) apply convolution layer + RELU
            # (batch_size, channels_in=1, h_in=max_sequence_length, w_in=dim_model) ->
            # (batch_size, channel_out=num_filters, h_out=max_sequence_length, w_out=1)
            conv_output = F.relu(conv_layer(conv_input)).squeeze(3)

            # This isn't super important since we are pooling across dim2,
            # but I wanted to make sure I got the same padding working.
            assert conv_output.size(0) == data.size(0) and \
                   conv_output.size(2) == data.size(1)

            # (3) Apply max pool along h_out dimension
            # (batch_size, channel_out=num_filters, h_out=max_sequence_length) ->
            # (batch_size, num_filters)
            pooled_output = F.max_pool1d(conv_output,
                                         conv_output.size(2)).squeeze(2)
            # (batch_size, num_filters), add to stack
            conv_stack.append(pooled_output)

        # flatten along dim=1
        # [(batch_size, num_filters)]*len(window_sizes) ->
        # (batch_size, num_filters)*len(window_sizes))
        pooled_values = torch.cat(conv_stack, 1)

        # (4) apply usual drop-out
        pooled_values = self._dropout(pooled_values)

        # (5) final linear layer, get into terms we care about
        # (batch_size, num_filters*len(window_sizes)) ->
        # (batch_size, class_num)
        y_hat = self._final_linear(pooled_values)

        # note: don't need to apply softmax, that is handled in CE function
        return y_hat

        The main function call of the model.

        Args:
            data (Tuple):
                a Tuple containing a tensor of size (batch_size, max_sequence_length)
        Returns:
            a tensor of (batch_size, class_size) of probability predictions
        
The main function call of the model.
The main function call of the model .
def forward(self, data: Tuple) -> torch.Tensor:
        data = data[1]
        embeddings = self._embeddings(data)
        conv_input = embeddings.unsqueeze(1)
        conv_stack = []
        for conv_layer in self._convs:
            conv_output = F.relu(conv_layer(conv_input)).squeeze(3)
            assert conv_output.size(0) == data.size(0) and \
                   conv_output.size(2) == data.size(1)
            pooled_output = F.max_pool1d(conv_output,
                                         conv_output.size(2)).squeeze(2)
            , add to stack
            conv_stack.append(pooled_output)
        *len(window_sizes))
        pooled_values = torch.cat(conv_stack, 1)
        pooled_values = self._dropout(pooled_values)
        y_hat = self._final_linear(pooled_values)
        return y_hat
def forward ( self , data : Tuple ) -> torch . Tensor : data = data [ 1 ] embeddings = self . _embeddings ( data ) conv_input = embeddings . unsqueeze ( 1 ) conv_stack = [ ] for conv_layer in self . _convs : conv_output = F . relu ( conv_layer ( conv_input ) ) . squeeze ( 3 ) assert conv_output . size ( 0 ) == data . size ( 0 ) and conv_output . size ( 2 ) == data . size ( 1 ) pooled_output = F . max_pool1d ( conv_output , conv_output . size ( 2 ) ) . squeeze ( 2 ) conv_stack . append ( pooled_output ) pooled_values = torch . cat ( conv_stack , 1 ) pooled_values = self . _dropout ( pooled_values ) y_hat = self . _final_linear ( pooled_values ) return y_hat
The main function call of the model.
The main function call of the model .
"""
        The main function call of the model.

        Args:
            data (Tuple):
                a Tuple containing a tensor of size (batch_size, max_sequence_length)
        Returns:
            a tensor of (batch_size, class_size) of probability predictions
        """ # this is (target, text) tuple. done for trainer normalization # (1) get embeddings # (batch_size, max_sequence_length) -> # (batch_size,max_sequence_length, dim_model) # (batch_size, max_sequence_length, dim_model) -> # (batch_size, channels_in=1, max_sequence_length, dim_model) # (2) apply convolution layer + RELU # (batch_size, channels_in=1, h_in=max_sequence_length, w_in=dim_model) -> # (batch_size, channel_out=num_filters, h_out=max_sequence_length, w_out=1) # This isn't super important since we are pooling across dim2, # but I wanted to make sure I got the same padding working. # (3) Apply max pool along h_out dimension # (batch_size, channel_out=num_filters, h_out=max_sequence_length) -> # (batch_size, num_filters) # (batch_size, num_filters), add to stack # flatten along dim=1 # [(batch_size, num_filters)]*len(window_sizes) -> # (batch_size, num_filters)*len(window_sizes)) # (4) apply usual drop-out # (5) final linear layer, get into terms we care about # (batch_size, num_filters*len(window_sizes)) -> # (batch_size, class_num) # note: don't need to apply softmax, that is handled in CE function
a tensor of (batch_size, class_size) of probability predictions
a tensor of ( batch_size class_size ) of probability predictions
self
data
Tuple
a Tuple containing a tensor of size (batch_size, max_sequence_length)
a Tuple containing a tensor of size ( batch_size max_sequence_length )