def __init__(self,config:const.Config()): super(TextCNN, self).__init__() self.embedding_size=config.embedding_size self.filter_size=config.filter_size self.filter_num=config.filter_num self.vocab_size=config.vocab_size self.vocab=config.vocab self.word_embedding=nn.Embedding(self.vocab_size,self.embedding_size) self.load_pretrained_embedding(config) self.convs=nn.ModuleList( [nn.Conv2d(1,self.filter_num,(size,self.embedding_size)) for size in self.filter_size] ) self.dropout=nn.Dropout(config.dropout) self.fc=nn.Linear(len(self.filter_size)*self.filter_num,2)
def get_config(): configs = const.configs( decoder_method=model, train_method=loss_method, triple_number=triple_number, epoch_number=epoch_number, save_freq=save_freq, decoder_num_units=hidden_size, encoder_num_units=hidden_size, cell_name=cell_name, learning_rate=learning_rate, batch_size=batch_size, decoder_output_max_length=triple_number * 3, dataset_name=dataset, exp_name=exp_name, standard_outputs_build_method=standard_outputs_build_method, restore_epoch=restore_epoch, ) config = const.Config(configs) return config, train_test_valid, gpu, rl_init_path, evaluation_epoch
print('_' * 60, datetime.datetime.now()) print("epoch %d \t loss: %f \t F1: %f \t P: %f \t R: %f" % (epoch, loss.item(), f1, precision, recall)) print("relation \t F1: %f \t P: %f \t R: %f \t" % (r_f1, r_precision, r_recall)) print("entity \t F1: %f \t P: %f \t R: %f \t" % (e_f1, e_precision, e_recall)) end_time = datetime.datetime.now() print('start:', start_time, 'end:', end_time) if __name__ == '__main__': config_filename = './' + args.dataset + '.json' config = const.Config(config_filename=config_filename, cell_name=cell_name, decoder_type=decoder_type) assert cell_name in ['lstm', 'gru'] assert decoder_type in ['one', 'multi'] if config.dataset_name == const.DataSet.NYT: prepare = data_prepare.NYTPrepare(config) elif config.dataset_name == const.DataSet.WEBNLG: prepare = data_prepare.WebNLGPrepare(config) else: print('illegal dataset name: %s' % config.dataset_name) exit() device = torch.device('cuda:' + args.gpu)
input_sentence_append_eos = append_eos2sentence( sentence_fw, self.config) # relations_append_eos = self.append_eos2relations(len(sentence_fw), self.config) return [ id, sentence_length, sentence_fw, sentence_bw, [None] * len(sentence_fw), [None] * len(sentence_fw), input_sentence_append_eos, id, id ] if __name__ == '__main__': # pass import const config_filename = './config.json' config = const.Config(config_filename=config_filename, cell_name='lstm', decoder_type='one') p = CCKSPrepare(config) data = p.load_data('test') # print(len(data)) # for token,label in zip(data[0],data[1]): # print(len(token), len(label)) # if(len(label)!=0): # print(len(label)/(len(token)+1)) # for i in range(5): # # if((i+1)*(len(token)+1)<=len(label)): # l=label[i*(len(token)+1):(i+1)*(len(token)+1)] # if(l[0]>=30): # print(l[0])
import const import utils import os import sys import argparse import model import torch import torch.nn.functional as F from torch import optim config = const.Config() parse = argparse.ArgumentParser() parse.add_argument('--mode', default='train', help='train/test') parse.add_argument('--cuda', default=False) parse.add_argument('--device', default="3") args = parse.parse_args() mode = args.mode use_cuda = args.cuda device_id = args.device if use_cuda: torch.cuda.manual_seed(1) os.environ["CUDA_VISIBLE_DEVICES"] = device_id model = model.TextCNN(config) if use_cuda: model.cuda() if mode == "test": print("loading model")
type=str, default='', help='experiment name') parser.add_argument('-eve', dest='detailed_epoch', type=int, default=-1, help='detailed epoch shown') args = parser.parse_args() config_filename = args.configfile cell_name = args.cell_name is_train = [True, False, False][args.is_train] train_test_valid = ['Train', 'Test', 'Valid'][args.is_train] # 调用配置 config = const.Config(config_filename=config_filename, cell_name=cell_name) gpu = args.gpu # set the batch size in test. Because the test data size maybe smaller then batch size if not is_train: if config.dataset_name == const.DataSet.NYT: config.batch_size = 1000 if config.dataset_name == const.DataSet.CONLL04 or config.dataset_name == const.DataSet.WEBNLG: config.batch_size = 2 logger = logging.getLogger('mylogger') def setup_logging( default_path='logging.json', default_level=logging.DEBUG, env_key='LOG_CFG', ):