-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
126 lines (106 loc) · 4.49 KB
/
run.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
123
124
125
126
import paddle
from paddle.io import Dataset, DataLoader
import numpy as np
from datasets import load_dataset
from nystromformer_paddle.nystromformer_config import NystromformerConfig
from nystromformer_paddle.nystromformer_paddle import NystromformerForSequenceClassification
from nystromformer_paddle.nystromformer_tokenizer import NystromformerTokenizer
from nystromformer_paddle.utils import update_metrics, get_f1_score
import pickle
import reprod_log
dataset = 'imdb'
max_len = 512
batch_size = 32
device = 'gpu'
lr = 3e-5
epochs = 4
mixed_precision = True
def prepare_loader(split):
data_path = 'data/tokenized_' + dataset + '_' + split + '_' + str(max_len) + '.pkl'
try:
with open(data_path, 'rb') as f:
tokenized_data, labels = pickle.load(f)
except FileNotFoundError:
raw_data = load_dataset(dataset)
tokenizer = NystromformerTokenizer('/home/zhao/Nystromformer-Paddle/pretrained_files/vocab.txt')
tokenized_data_list = tokenizer(
raw_data[split]['text'][:10],
max_seq_len=max_len - 2, pad_to_max_seq_len=True,
return_position_ids=True, return_token_type_ids=True, return_attention_mask=True,
)
tokenized_data = [
(np.asarray(data['input_ids'], dtype=np.long),
np.asarray(data['token_type_ids'], dtype=np.long),
np.asarray(data['attention_mask'], dtype=np.long))
for data in tokenized_data_list
]
f = open(data_path, 'wb')
labels = raw_data[split]['label']
pickle.dump((tokenized_data, labels), f)
class TextDataset(Dataset):
def __len__(self):
return len(tokenized_data)
def __getitem__(self, idx):
return tokenized_data[idx][0], tokenized_data[idx][1], tokenized_data[idx][2], labels[idx]
loader = DataLoader(dataset=TextDataset(), batch_size=batch_size, shuffle=split == 'train')
return loader
def main():
paddle.device.set_device(device)
train_loader = prepare_loader('train')
valid_loader = prepare_loader('test')
model_config = NystromformerConfig()
model_config.load_config_json('pretrained_files/config.json')
model = NystromformerForSequenceClassification(model_config)
model.nystromformer.load_dict(paddle.load('pretrained_files/nystromformer_model.params'))
optimizer = paddle.optimizer.AdamW(
parameters=model.parameters(),
learning_rate=lr,
beta1=0.9, beta2=0.999, epsilon=1e-6, weight_decay=0.01
)
amp_scaler = paddle.amp.GradScaler() if mixed_precision and device != 'cpu' else None
log = reprod_log.ReprodLogger()
for epoch in range(epochs):
precision, recall = paddle.metric.Precision(), paddle.metric.Recall()
precision.reset()
recall.reset()
model.train()
for i, batch_data in enumerate(train_loader):
outputs = model(
input_ids=batch_data[0],
token_type_ids=batch_data[1],
attention_mask=batch_data[2],
labels=batch_data[3]
)
logits, loss = outputs['logits'], outputs['loss']
update_metrics(logits, batch_data[3], [precision, recall])
if amp_scaler is not None:
scaled = amp_scaler.scale(loss)
scaled.backward()
amp_scaler.minimize(optimizer, scaled)
else:
loss.backward()
optimizer.minimize(loss)
optimizer.clear_gradients()
if i % 50 == 0:
print('epoch:', epoch, 'loss:', loss.numpy())
train_f1_score = get_f1_score(precision, recall)
precision.reset()
recall.reset()
model.eval()
for batch_data in valid_loader:
outputs = model(
input_ids=batch_data[0],
token_type_ids=batch_data[1],
attention_mask=batch_data[2],
labels=batch_data[3]
)
logits, loss = outputs['logits'], outputs['loss']
update_metrics(logits, batch_data[3], [precision, recall])
valid_f1_score = get_f1_score(precision, recall)
print('----------------------------------------------')
print('epoch:', epoch, 'finished. train_f1_score:', train_f1_score, 'valid_f1_score:', valid_f1_score)
print('----------------------------------------------')
log.add('epoch' + str(epoch), np.asarray(valid_f1_score))
log.save('fine_tune_log.npy')
if __name__ == '__main__':
main()