-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
122 lines (95 loc) · 5.42 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from argparse import ArgumentParser
from config import config
from transformers import AutoTokenizer, AutoModel
from torch.utils.data import DataLoader
from src.train import train, evaluate
from src.model import Coref
from src.data_loader import ECBDataset
from src.utils import *
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument('-M', '--mode', help='select mode to run', required=True, choices=['train', 'test'])
parser.add_argument('-m', '--model_path', help='provide save/load path for the model', required=True)
parser.add_argument('-t', '--mention_type', help='mention type to extract', required=True)
parser.add_argument('-d', '--data_path', help='provide data path', required=False)
args = vars(parser.parse_args())
device = torch.device("cuda" if torch.cuda.is_available else "cpu")
# device = torch.device("cpu")
print(device)
if args['mode'] == 'train':
model = Coref(config, device)
model.to(device)
all_sentences, tokens, batch_indices, mentions, gold_starts, gold_ends, clusters = process_ecb_plus(args['data_path'], args['mention_type'])
tokenizer = AutoTokenizer.from_pretrained("SpanBERT/spanbert-base-cased", use_fast=True)
encodings = tokenizer(all_sentences, return_offsets_mapping=True, is_split_into_words=True, truncation=True, padding=True)
encoded_tokens = fix_tokens_with_offsets(tokens, encodings.offset_mapping, batch_indices)
encoded_sentence_map = create_unmasked_sentence_map(encodings.offset_mapping, batch_indices)
encodings.pop('offset_mapping')
encoded_tokens, gold_starts, gold_ends, mentions = process_gold_mentions(encoded_tokens, gold_starts, gold_ends, mentions, encodings.attention_mask, batch_indices)
cluster_ids = get_cluster_ids(mentions, clusters)
dataset = ECBDataset(
encodings=encodings,
batch_indices=batch_indices,
sentence_map=encoded_sentence_map,
gold_starts=gold_starts,
gold_ends=gold_ends,
cluster_ids=cluster_ids)
loader = DataLoader(
dataset,
batch_size=1,
shuffle=False
)
optimizer = torch.optim.Adam(model.parameters(), lr=config['learning_rate'])
spanbert_save_path = 'models/spanbert_' + args['mention_type']
train(config['num_epoch'], loader, model, optimizer, args['model_path'], device, spanbert_save_path)
if args['mode'] == 'test':
spanbert_save_path = 'models/spanbert_' + args['mention_type']
model = Coref(config, device, spanbert_save_path)
model.to(device)
all_sentences, tokens, batch_indices, mentions, gold_starts, gold_ends, clusters = process_ecb_plus(args['data_path'], args['mention_type'])
tokenizer = AutoTokenizer.from_pretrained("SpanBERT/spanbert-base-cased", use_fast=True)
encodings = tokenizer(all_sentences, return_offsets_mapping=True, is_split_into_words=True, truncation=True, padding=True)
encoded_tokens = fix_tokens_with_offsets(tokens, encodings.offset_mapping, batch_indices)
encoded_sentence_map = create_unmasked_sentence_map(encodings.offset_mapping, batch_indices)
encodings.pop('offset_mapping')
encoded_tokens, gold_starts, gold_ends, mentions = process_gold_mentions(encoded_tokens, gold_starts, gold_ends, mentions, encodings.attention_mask, batch_indices)
cluster_ids = get_cluster_ids(mentions, clusters)
dataset = ECBDataset(
encodings=encodings,
batch_indices=batch_indices,
sentence_map=encoded_sentence_map,
gold_starts=gold_starts,
gold_ends=gold_ends,
cluster_ids=cluster_ids)
loader = DataLoader(
dataset,
batch_size=1,
shuffle=False
)
model.load_state_dict(torch.load(args['model_path']))
# print(model)
print(model.parameters)
evaluate(loader, model, device)
evaluate(loader, model, device)
# if args['mode'] == 'predict':
# all_sentences, tokens, batch_indices, mentions, gold_starts, gold_ends, clusters = process_ecb_plus(args['data_path'], args['mention_type'])
# tokenizer = AutoTokenizer.from_pretrained("SpanBERT/spanbert-base-cased", use_fast=True)
# encodings = tokenizer(all_sentences, return_offsets_mapping=True, is_split_into_words=True, truncation=True, padding=True)
# encoded_tokens = fix_tokens_with_offsets(tokens, encodings.offset_mapping, batch_indices)
# encoded_sentence_map = create_unmasked_sentence_map(encodings.offset_mapping, batch_indices)
# encoded_tokens, gold_starts, gold_ends, mentions = process_gold_mentions(encoded_tokens, gold_starts, gold_ends, mentions, encodings.attention_mask, batch_indices)
# cluster_ids = get_cluster_ids(mentions, clusters)
# dataset = ECBDataset(
# encodings=encodings,
# batch_indices=batch_indices,
# sentence_map=encoded_sentence_map,
# gold_starts=gold_starts,
# gold_ends=gold_ends,
# cluster_ids=cluster_ids)
# loader = DataLoader(
# dataset,
# batch_size=1,
# shuffle=False
# )
# model.load_state_dict(torch.load(args['model_path']))
# predict(loader, model, device, encodings.offset_mapping, all_sentences, batch_indices)