Repository navigation
Expand file tree
/
Copy pathinitialize.py
More file actions
72 lines (59 loc) · 2.08 KB
/
Copy pathinitialize.py
File metadata and controls
72 lines (59 loc) · 2.08 KB
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
import json
import tqdm as tq
from BERT_CRF import *
def my_initialize():
with open('test_data.json', encoding='utf-8') as fin:
test_instances = json.load(fin)
class_dict = {
'O': 0,
'B-Cue': 1,
'I-Cue': 2,
'B-Content': 3,
'I-Content': 4,
'B-Source': 5,
'I-Source': 6,
'X': 11
}
# Get the inverted class dictionary
inv_class_dict = {class_dict[k]: k for k in class_dict}
# Batch size
batch_size = 2
# Hidden size of MLP
hidden_size = 200
# Learning rate
lr = 1e-3
# Max epochs of system
epochs = 1
# Patience of system
max_patience = 10
# Window of context
window = 2
model = BERT_CRF(b_size=batch_size,
n_classes_output=len(class_dict),
hidden_size=hidden_size,
window=window)
# Preprocess all test batches
print('Preprocessing test batches')
test_batches = [test_instances[i * batch_size: (i + 1) * batch_size] for i in
tq.trange((len(test_instances) // batch_size) + 1)]
if not test_batches[-1]:
test_batches = test_batches[:-1]
test_batches = [model.preprocess_batch(batch) for batch in tq.tqdm(test_batches)]
# Load the best state from the file that it is stored
state = torch.load('system_best_epoch.pth.tar')
# Load the parameters into the model
model.load_state_dict(state['model'])
my_seed = 1
random.seed(my_seed)
np.random.seed(my_seed)
torch.manual_seed(my_seed)
cudnn.benchmark = True
torch.backends.cudnn.deterministic = True
gpu_device = 0
use_cuda = torch.cuda.is_available()
if (use_cuda):
torch.cuda.manual_seed(my_seed)
print("Using GPU")
else:
print("NOT Using GPU")
return model, class_dict, inv_class_dict, test_batches, use_cuda, 0