batch_size, gpu, mpath, checkpoint, bert_type, hans = parse_args() if mpath == 'default': mpath = 'output/{}.state_dict'.format(bert_type) gpu = bool(gpu) hans = bool(hans) checkpoint = bool(checkpoint) print('=====Arguments=====') print('bert type:\t{}'.format(bert_type)) print('trained model path:\t{}'.format(mpath)) print('gpu:\t{}'.format(gpu)) print('checkpoint:\t{}'.format(checkpoint)) print('batch size:\t{}'.format(batch_size)) print('hans data:\t{}'.format(hans)) # Read the dataset nli_reader = NLIDataReader('./datasets/SUFE') test_data = nli_reader.get_examples('try.gz', max_examples=50) if hans: nli_reader = NLIDataReader('./datasets/Hans') test_data += nli_reader.get_hans_examples( 'heuristics_evaluation_set.txt') model = BertNLIModel(model_path=mpath, batch_size=batch_size, bert_type=bert_type) print('test data size: {}'.format(len(test_data))) evaluate(model, test_data, checkpoint, test_bs=batch_size) print('all done!')
# Read the dataset nli_reader = NLIDataReader('datasets/AllNLI') train_num_labels = nli_reader.get_num_labels() all_data = nli_reader.get_examples('train.gz') #,max_examples=5000) random.shuffle(all_data) train_data = all_data[:int(train_rate * len(all_data))] dev_data = all_data[int(train_rate * len(all_data)):] logging.info('train data size {}'.format(len(train_data))) logging.info('dev data size {}'.format(len(dev_data))) total_steps = math.ceil(epoch_num * len(train_data) * 1. / batch_size) warmup_steps = int(total_steps * warmup_percent) model = BertNLIModel(gpu=gpu, batch_size=batch_size, bert_type=bert_type, model_path=trained_model) optimizer = AdamW(model.parameters(), lr=2e-5, eps=1e-6, correct_bias=False) scheduler = get_scheduler(optimizer, scheduler_setting, warmup_steps=warmup_steps, t_total=total_steps) if fp16: try: from apex import amp except ImportError: raise ImportError( "Please install apex from https://www.github.com/nvidia/apex to use fp16 training."
all_data = msnli_data + hans_data random.shuffle(all_data) train_num = int(len(all_data) * 0.95) train_data = all_data[:train_num] dev_data = all_data[train_num:] logging.info('train data size {}'.format(len(train_data))) logging.info('dev data size {}'.format(len(dev_data))) total_steps = math.ceil(epoch_num * len(train_data) * 1. / batch_size) warmup_steps = int(total_steps * warmup_percent) model = BertNLIModel(gpu=gpu, batch_size=batch_size, bert_type=bert_type, model_path=trained_model, reinit_num=reinit_layers, freeze_layers=freeze_layers) optimizer = AdamW(model.parameters(), lr=2e-5, eps=1e-6, correct_bias=False) scheduler = get_scheduler(optimizer, scheduler_setting, warmup_steps=warmup_steps, t_total=total_steps) if fp16: try: from apex import amp except ImportError: raise ImportError(
from bert_nli import BertNLIModel if __name__ == '__main__': model = BertNLIModel('output/sample_model.state_dict') sent_pairs = [ ('The lecturer committed plagiarism.', 'He was promoted.'), ('The lecturer got a big funding award', 'He was promoted.'), ('The lecturer won the Novel prize.', 'He was promoted.'), ('The lecturer became a professor last June.', 'He was promoted.'), ('A man inspects the uniform of a figure in some East Asian country.', 'The man is sleeping.'), ('An older and younger man smiling.', 'Two men are smiling and laughing at the cats playing on the floor.'), ('A black race car starts up in front of a crowd of people.', 'A man is driving down a lonely road.'), ('A soccer game with multiple males playing.', 'Some men are playing a sport.'), ('A smiling costumed woman is holding an umbrella.', 'A happy woman in a fairy costume holds an umbrella.') ] labels, probs = model(sent_pairs) print(labels) print(probs)
# Read the dataset nli_reader = NLIDataReader('datasets/AllNLI') train_num_labels = nli_reader.get_num_labels() all_data = nli_reader.get_examples('train.gz') #,max_examples=5000) random.shuffle(all_data) train_data = all_data[:int(train_rate * len(all_data))] dev_data = all_data[int(train_rate * len(all_data)):] logging.info('train data size {}'.format(len(train_data))) logging.info('dev data size {}'.format(len(dev_data))) total_steps = math.ceil(epoch_num * len(train_data) * 1. / batch_size) warmup_steps = int(total_steps * warmup_percent) model = BertNLIModel( gpu=gpu, batch_size=batch_size ) # rtx 2080 cannot accommodate bert-large, even with batch size 8 optimizer = AdamW(model.parameters(), lr=2e-5, eps=1e-6, correct_bias=False) scheduler = get_scheduler(optimizer, scheduler_setting, warmup_steps=warmup_steps, t_total=total_steps) if fp16: try: from apex import amp except ImportError: raise ImportError( "Please install apex from https://www.github.com/nvidia/apex to use fp16 training."
assert len(all_predict) == len(all_labels) acc = len([ i for i in range(len(all_labels)) if all_predict[i] == all_labels[i] ]) * 1. / len(all_labels) prf = precision_recall_fscore_support(all_labels, all_predict, average=None, labels=[0, 1, 2]) if not mute: print('==>acc<==', acc) print('label meanings: 0: contradiction, 1: entail, 2: neutral') print('==>precision-recall-f1<==\n', prf) return acc if __name__ == '__main__': gpu = True batch_size = 16 # Read the dataset nli_reader = NLIDataReader('./datasets/AllNLI') model = BertNLIModel(model_path='output/sample_model.state_dict', batch_size=batch_size) test_data = nli_reader.get_examples('dev.gz') #,max_examples=50) print('test data size: {}'.format(len(test_data))) evaluate(model, test_data)