-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
51 lines (43 loc) · 2.24 KB
/
train.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
import multiprocessing
import torch
from tokenizers.implementations import BertWordPieceTokenizer
from torch import nn, optim
import data
from configuration import Configuration
from data import create_data_loader
from model import PuncRec
from train_funcs import train
from visualize import visualize_results
if __name__ == '__main__':
torch.set_num_threads(multiprocessing.cpu_count())
config = Configuration(segment_size=32,
top_epochs=1,
top_iterations=2,
all_epochs=1,
all_iterations=2,
smoke_run=True)
tokenizer = BertWordPieceTokenizer(config.flavor + '/vocab.txt', lowercase=True)
train_data = data.load_data(config.train_path)
test_data = data.load_data(config.test_path)
X_train, y_train = data.preprocess_data(train_data, tokenizer, config.punctuation_encoding, config.segment_size)
X_test, y_test = data.preprocess_data(test_data, tokenizer, config.punctuation_encoding, config.segment_size)
puncRec = nn.DataParallel(PuncRec(config).to(config.device))
print('TRAINING TOP LAYER...')
data_loader_train = create_data_loader(X_train, y_train, True, 1024)
data_loader_valid = create_data_loader(X_test, y_test, False, 512)
for p in puncRec.module.bert.parameters():
p.requires_grad = False
optimizer = optim.Adam(puncRec.parameters(), lr=1e-5)
loss = nn.CrossEntropyLoss()
puncRec, optimizer, best_val_loss = train(puncRec, optimizer, loss, config, data_loader_train, data_loader_valid,
best_val_loss=1e9, top_learning=True)
print('TRAINING ALL LAYERS...')
data_loader_train = create_data_loader(X_train, y_train, True, 256)
data_loader_valid = create_data_loader(X_test, y_test, False, 128)
for p in puncRec.module.bert.parameters():
p.requires_grad = True
optimizer = optim.Adam(puncRec.parameters(), lr=1e-5)
loss = nn.CrossEntropyLoss()
bert_punc, optimizer, best_val_loss = train(puncRec, optimizer, loss, config,
data_loader_train, data_loader_valid, best_val_loss=best_val_loss)
visualize_results(config.save_path, save_file=True)