def __init__(self, word_seq_indexer, tag_seq_indexer, class_num, batch_size=1, rnn_hidden_dim=100,
              freeze_word_embeddings=False, dropout_ratio=0.5, rnn_type='GRU', gpu=-1):
     super(TaggerBiRNNCRF, self).__init__(word_seq_indexer, tag_seq_indexer, gpu, batch_size)
     self.tag_seq_indexer = tag_seq_indexer
     self.class_num = class_num
     self.rnn_hidden_dim = rnn_hidden_dim
     self.freeze_embeddings = freeze_word_embeddings
     self.dropout_ratio = dropout_ratio
     self.rnn_type = rnn_type
     self.gpu = gpu
     self.word_embeddings_layer = LayerWordEmbeddings(word_seq_indexer, gpu, freeze_word_embeddings)
     self.dropout = torch.nn.Dropout(p=dropout_ratio)
     if rnn_type == 'GRU':
         self.birnn_layer = LayerBiGRU(input_dim=self.word_embeddings_layer.output_dim,
                                       hidden_dim=rnn_hidden_dim,
                                       gpu=gpu)
     elif rnn_type == 'LSTM':
         self.birnn_layer = LayerBiLSTM(input_dim=self.word_embeddings_layer.output_dim,
                                        hidden_dim=rnn_hidden_dim,
                                        gpu=gpu)
     elif rnn_type == 'Vanilla':
         self.birnn_layer = LayerBiVanilla(input_dim=self.word_embeddings_layer.output_dim+self.char_cnn_layer.output_dim,
                                        hidden_dim=rnn_hidden_dim,
                                        gpu=gpu)
     else:
         raise ValueError('Unknown rnn_type = %s, must be either "LSTM" or "GRU"')
     self.lin_layer = nn.Linear(in_features=self.birnn_layer.output_dim, out_features=class_num + 2)
     self.crf_layer = LayerCRF(gpu, states_num=class_num + 2, pad_idx=tag_seq_indexer.pad_idx, sos_idx=class_num + 1,
                               tag_seq_indexer=tag_seq_indexer)
     if gpu >= 0:
         self.cuda(device=self.gpu)
Example #2
0
    def __init__(self, embedding_indexer: SeqIndexerBaseEmbeddings, gpu,
                 feat_num, filter_window_sizes):
        """
        :param embedding_indexer: the seq_indexer which could give the index /embedding vectors of a word
        :param gpu: whether to work on gpu for higher speed
        :param feat_num: the numbers of different classification you want to have.
        :param filer_window_sizes: a list contains the size of the different filters.
        """

        super(TextCNN, self).__init__()
        self.gpu = gpu
        self.filter_window_sizes = filter_window_sizes
        self.filter_groups = len(filter_window_sizes)
        self.embeding = LayerWordEmbeddings(embedding_indexer, gpu)
        self.filter_num = 2

        self.convs = []
        for filter_window_size in filter_window_sizes:
            self.convs.append(
                LayerWordCNN(gpu=self.gpu,
                             word_embeddings_dim=embedding_indexer.emb_dim,
                             filter_num=self.filter_num,
                             word_window_size=filter_window_size))

        self.linear1 = torch.nn.Linear(self.filter_num * self.filter_groups,
                                       50)
        self.linear2 = torch.nn.Linear(50, feat_num)
        self.act_func = torch.nn.LeakyReLU()

        if (gpu >= 0):
            self.cuda(device=gpu)
Example #3
0
    def __init__(self, embedding_indexer: SeqIndexerBaseEmbeddings, gpu,
                 feat_num):
        super(MLP, self).__init__()
        self.embeding = LayerWordEmbeddings(embedding_indexer, gpu)
        self.linear1 = torch.nn.Linear(embedding_indexer.emb_dim, 50)
        self.linear2 = torch.nn.Linear(50, feat_num)
        self.act_func = torch.nn.LeakyReLU()

        if (gpu >= 0):
            self.cuda(device=gpu)
Example #4
0
 def __init__(self, embedding_indexer: SeqIndexerBaseEmbeddings, gpu,
              feat_num, dropout):
     super(MLP, self).__init__()
     self.embedding = LayerWordEmbeddings(embedding_indexer)
     self.linear1 = torch.nn.Linear(embedding_indexer.emb_dim, 50)
     self.linear2 = torch.nn.Linear(50, feat_num)
     self.dropout = torch.nn.Dropout(dropout)
     self.gpu = gpu
     if (gpu >= 0):
         self.cuda(device=gpu)
 def __init__(self,
              word_seq_indexer,
              tag_seq_indexer,
              class_num,
              batch_size=1,
              rnn_hidden_dim=100,
              freeze_word_embeddings=False,
              dropout_ratio=0.5,
              rnn_type='GRU',
              gpu=-1):
     super(TaggerBiRNN, self).__init__(word_seq_indexer, tag_seq_indexer,
                                       gpu, batch_size)
     self.tag_seq_indexer = tag_seq_indexer
     self.class_num = class_num
     self.rnn_hidden_dim = rnn_hidden_dim
     self.freeze_embeddings = freeze_word_embeddings
     self.dropout_ratio = dropout_ratio
     self.rnn_type = rnn_type
     self.gpu = gpu
     self.word_embeddings_layer = LayerWordEmbeddings(
         word_seq_indexer, gpu, freeze_word_embeddings)
     self.dropout = torch.nn.Dropout(p=dropout_ratio)
     if rnn_type == 'GRU':
         self.birnn_layer = LayerBiGRU(
             input_dim=self.word_embeddings_layer.output_dim,
             hidden_dim=rnn_hidden_dim,
             gpu=gpu)
     elif rnn_type == 'LSTM':
         self.birnn_layer = LayerBiLSTM(
             input_dim=self.word_embeddings_layer.output_dim,
             hidden_dim=rnn_hidden_dim,
             gpu=gpu)
     elif rnn_type == 'Vanilla':
         self.birnn_layer = LayerBiVanilla(
             input_dim=self.word_embeddings_layer.output_dim +
             self.char_cnn_layer.output_dim,
             hidden_dim=rnn_hidden_dim,
             gpu=gpu)
     else:
         raise ValueError(
             'Unknown rnn_type = %s, must be either "LSTM" or "GRU"')
     # We add an additional class that corresponds to the zero-padded values not to be included to the loss function
     self.lin_layer = nn.Linear(in_features=self.birnn_layer.output_dim,
                                out_features=class_num + 1)
     self.log_softmax_layer = nn.LogSoftmax(dim=1)
     if gpu >= 0:
         self.cuda(device=self.gpu)
     self.nll_loss = nn.NLLLoss(
         ignore_index=0
     )  # "0" target values actually are zero-padded parts of sequences
 def __init__(self,
              embedding_indexer: SeqIndexerBaseEmbeddings,
              gpu,
              feat_num,
              dropout,
              kernel_size,
              channel=50):
     super(TextCNN, self).__init__()
     self.embedding = LayerWordEmbeddings(embedding_indexer)
     self.convs = torch.nn.ModuleList([
         torch.nn.Conv2d(1, channel, [x, embedding_indexer.emb_dim])
         for x in kernel_size
     ])
     self.fc = torch.nn.Linear(len(kernel_size) * channel, feat_num)
     self.dropout = dropout
     self.gpu = gpu
     if gpu >= 0:
         self.cuda(device=gpu)