Esempio n. 1
0
 def __init__(self, word_vocab_size, word_embedding_dim, word_out_channels, char_vocab_size, 
              char_embedding_dim, char_out_channels, decoder_hidden_units, tag_to_id, cap_input_dim=4, 
              cap_embedding_dim=0, pretrained=None):
     
     super(CNN_CNN_LSTM, self).__init__()
     
     self.word_vocab_size = word_vocab_size
     self.word_embedding_dim = word_embedding_dim
     self.word_out_channels = word_out_channels
     
     self.char_vocab_size = char_vocab_size
     self.char_embedding_dim = char_embedding_dim
     self.char_out_channels = char_out_channels
     
     self.cap_input_dim = cap_input_dim
     self.cap_embedding_dim = cap_embedding_dim
     
     self.tag_to_ix = tag_to_id
     self.tagset_size = len(tag_to_id)
     
     self.initializer = Initializer()
     self.loader = Loader()
     
     if self.cap_input_dim and self.cap_embedding_dim:
         self.cap_embedder = nn.Embedding(self.cap_input_dim, self.cap_embedding_dim)
         self.initializer.init_embedding(self.cap_embedder.weight)
     
     self.char_encoder = CharEncoderCNN(char_vocab_size, char_embedding_dim, char_out_channels, 
                                        kernel_width=3, pad_width=1)
     
     self.initializer.init_embedding(self.char_encoder.embedding.weight)
     
     self.word_encoder = WordEncoderCNN(word_vocab_size, word_embedding_dim, char_out_channels,
                                        kernel_width = 5, pad_width = 2, input_dropout_p=0.5,
                                        output_dropout_p=0.5, out_channels=word_out_channels)
     
     if pretrained is not None:
         self.word_encoder.embedding.weight = nn.Parameter(torch.FloatTensor(pretrained))
     
     augmented_decoder_inp_size = (word_out_channels + word_embedding_dim + 
                                   char_out_channels + cap_embedding_dim)
     self.decoder = DecoderRNN(augmented_decoder_inp_size, decoder_hidden_units, self.tagset_size, 
                               self.tag_to_ix, input_dropout_p=0.5)
Esempio n. 2
0
 def __init__(self, word_vocab_size, word_embedding_dim, word_hidden_dim, char_vocab_size,
              char_embedding_dim, char_out_channels, tag_to_id, cap_input_dim=4 ,
              cap_embedding_dim=0, pretrained=None):
     
     super(CNN_BiLSTM_CRF_MC, self).__init__()
     
     self.word_vocab_size = word_vocab_size
     self.word_embedding_dim = word_embedding_dim
     self.word_hidden_dim = word_hidden_dim
     
     self.char_vocab_size = char_vocab_size
     self.char_embedding_dim = char_embedding_dim
     self.char_out_channels = char_out_channels
     
     self.cap_input_dim = cap_input_dim
     self.cap_embedding_dim = cap_embedding_dim
     
     self.tag_to_ix = tag_to_id
     self.tagset_size = len(tag_to_id)
     
     self.initializer = Initializer()
     self.loader = Loader()
     
     if self.cap_embedding_dim:
         self.cap_embedder = nn.Embedding(self.cap_input_dim, self.cap_embedding_dim)
         self.initializer.init_embedding(self.cap_embedder.weight)
     
     self.char_encoder = CharEncoderCNN(char_vocab_size, char_embedding_dim, char_out_channels, 
                                        kernel_width=3, pad_width=1)
     
     self.initializer.init_embedding(self.char_encoder.embedding.weight)
     
     self.word_encoder = WordEncoderRNN(word_vocab_size, word_embedding_dim ,word_hidden_dim, 
                                        char_out_channels, cap_embedding_dim, input_dropout_p=0.5)
     
     if pretrained is not None:
         self.word_encoder.embedding.weight = nn.Parameter(torch.FloatTensor(pretrained))
         
     self.initializer.init_lstm(self.word_encoder.rnn)
     
     self.decoder = DecoderCRF(word_hidden_dim*2, self.tag_to_ix, input_dropout_p=0.5)
     self.initializer.init_linear(self.decoder.hidden2tag)
Esempio n. 3
0
    parameters['sigmp'] = float(np.exp(-3))
    parameters['acqmd'] = 'b'
    
else:
    raise NotImplementedError()
    

use_dataset = opt.dataset
dataset_path = os.path.join('datasets', use_dataset)
result_path = os.path.join(opt.result_path, use_dataset)
model_name = opt.usemodel
model_load = opt.reload
checkpoint = opt.checkpoint
init_percent = opt.initdata
acquire_method = opt.acquiremethod
loader = Loader()

print('Model:', model_name)
print('Dataset:', use_dataset)
print('Acquisition:', acquire_method)

if not os.path.exists(result_path):
    os.makedirs(result_path)
    
if not os.path.exists(os.path.join(result_path, model_name)):
    os.makedirs(os.path.join(result_path, model_name))

if not os.path.exists(os.path.join(result_path, model_name, 'active_checkpoint', acquire_method)):
    os.makedirs(os.path.join(result_path, model_name, 'active_checkpoint', acquire_method))

if opt.dataset == 'conll':
Esempio n. 4
0
def main(config):
    os.chdir(hydra.utils.get_original_cwd())
    config.parameters.model = config.opt.usemodel
    config.parameters.wrdim = config.opt.worddim
    config.parameters.ptrnd = config.opt.pretrnd

    use_dataset = config.opt.dataset
    dataset_path = os.path.join('datasets', use_dataset)

    tz = datetime.timezone(datetime.timedelta(hours=3))
    dt = datetime.datetime.now(tz=tz)
    date_path = f'{dt.date()}'
    time_path = f'{dt.time()}'.replace(':', '-').split('.')[0]

    result_path = os.path.join(config.opt.result_path, use_dataset, date_path,
                               time_path)
    # model_name = config.opt.usemodel
    # model_load = config.opt.reload
    loader = Loader()

    log.info(f'Model: {config.opt.usemodel}')
    log.info(f'Dataset: {use_dataset}')

    if not os.path.exists(result_path):
        os.makedirs(result_path)

    if not os.path.exists(os.path.join(result_path, config.opt.usemodel)):
        os.makedirs(os.path.join(result_path, config.opt.usemodel))

    with open(os.path.join(result_path, config.opt.usemodel, 'params'),
              'wt') as fobj:
        for param in config.parameters:
            fobj.write(f'{param}:\t{config.parameters[param]}\n')

    if use_dataset == 'conll':
        train_data, dev_data, test_data, test_train_data, mappings = loader.load_conll(
            dataset_path, config.parameters)
    elif use_dataset == 'ontonotes':
        train_data, dev_data, test_data, mappings = loader.load_ontonotes(
            dataset_path, config.parameters)
        test_train_data = train_data[-10000:]
    else:
        raise KeyError('unknown dataset name')

    log.info('Load Complete')

    model = make_model(config, mappings, result_path)

    model.cuda()
    learning_rate = config.parameters['lrate']
    log.info(f'Initial learning rate is: {learning_rate:.6f}')
    optimizer = torch.optim.SGD(model.parameters(),
                                lr=learning_rate,
                                momentum=0.9)

    trainer = Trainer(model,
                      optimizer,
                      result_path,
                      config.opt.usemodel,
                      usedataset=config.opt.dataset,
                      mappings=mappings)
    losses, all_F = trainer.train_model(
        config.opt.num_epochs,
        train_data,
        dev_data,
        test_train_data,
        test_data,
        learning_rate=learning_rate,
        batch_size=config.parameters['batch_size'],
        lr_decay=config.opt.lr_decay)

    plt.plot(losses)
    plt.savefig(os.path.join(result_path, config.opt.usemodel, 'lossplot.png'))