-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathrun_language_modeling.py
More file actions
166 lines (146 loc) 路 5.03 KB
/
Copy pathrun_language_modeling.py
File metadata and controls
166 lines (146 loc) 路 5.03 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
"""
@Time : 2020-11-26 17:03:29
@File : run_pretraining.py
@Author : Abtion
@Email : abtion{at}outlook.com
"""
from sklearn.model_selection import train_test_split
import os
from tqdm import tqdm
from simpletransformers.language_modeling import LanguageModelingModel
import logging
def proc_data():
import json
all_text = []
data_path = '/ml/nlp/data'
# for fn in os.listdir(data_path):
# if os.path.isdir(os.path.join(data_path, fn)):
# for txt_name in tqdm(os.listdir(os.path.join(data_path, fn))):
# txt_path = os.path.join(data_path, fn, txt_name)
# if txt_path.endswith('.txt'):
# with open(txt_path, 'r', encoding='utf8') as f:
# for line in f:
# line = line.strip()
# if len(line) > 1:
# all_text.append(line)
data_path = '/ml/nlp/data/wiki'
for txt_name in tqdm(os.listdir(data_path)):
txt_path = os.path.join(data_path, txt_name)
if txt_path.endswith('.txt'):
with open(txt_path, 'r', encoding='utf8') as f:
for line in f:
line = line.strip()
if len(line) > 1:
all_text.append(line)
with open('data/data.json', 'r', encoding='utf8') as f:
data = json.load(f)
for d in data:
all_text.append(d['Title'])
all_text.append(d['Content'])
train, test = train_test_split(all_text, test_size=0.1)
with open("data/train.txt", "w") as f:
for line in train:
f.write(line + "\n")
with open("data/test.txt", "w") as f:
for line in test:
f.write(line + "\n")
def main():
logging.basicConfig(level=logging.INFO)
transformers_logger = logging.getLogger("transformers")
transformers_logger.setLevel(logging.WARNING)
train_args = {
"reprocess_input_data": False,
"overwrite_output_dir": True,
"num_train_epochs": 50,
"save_eval_checkpoints": True,
"save_model_every_epoch": False,
"learning_rate": 1e-3,
"warmup_steps": 10000,
"train_batch_size": 64,
"eval_batch_size": 128,
"gradient_accumulation_steps": 2,
"block_size": 128,
"max_seq_length": 128,
"dataset_type": "simple",
"wandb_project": "Esperanto - ConvBert",
"wandb_kwargs": {"name": "ConvBert-SMALL"},
"logging_steps": 100,
"evaluate_during_training": True,
"evaluate_during_training_steps": 3000,
"evaluate_during_training_verbose": True,
"use_cached_eval_features": True,
"sliding_window": False,
"tokenizer_name": "bert-base-chinese",
"use_multiprocessing": True,
"process_count": 8,
"vocab_size": 21128,
"generator_config": {
"attention_probs_dropout_prob": 0.1,
"directionality": "bidi",
"embedding_size": 128,
"hidden_act": "gelu",
"hidden_dropout_prob": 0.1,
"hidden_size": 64,
"initializer_range": 0.02,
"intermediate_size": 256,
"layer_norm_eps": 1e-12,
"max_position_embeddings": 512,
"model_type": "convbert",
"num_attention_heads": 1,
"num_hidden_layers": 12,
"pad_token_id": 0,
"summary_activation": "gelu",
"summary_last_dropout": 0.1,
"summary_type": "first",
"summary_use_proj": True,
"type_vocab_size": 2,
"vocab_size": 21128
},
"discriminator_config": {
"attention_probs_dropout_prob": 0.1,
"embedding_size": 128,
"hidden_act": "gelu",
"hidden_dropout_prob": 0.1,
"hidden_size": 256,
"initializer_range": 0.02,
"intermediate_size": 1024,
"layer_norm_eps": 1e-12,
"max_position_embeddings": 512,
"model_type": "convbert",
"num_attention_heads": 4,
"num_hidden_layers": 12,
"output_past": True,
"pad_token_id": 0,
"summary_activation": "gelu",
"summary_last_dropout": 0.1,
"summary_type": "first",
"summary_use_proj": True,
"type_vocab_size": 2,
"vocab_size": 21128
},
}
train_file = "data/train.txt"
test_file = "data/test.txt"
model = LanguageModelingModel(
"convbert",
None,
args=train_args,
train_files=train_file,
cuda_device=1,
)
model.train_model(
train_file, eval_file=test_file,
)
model.eval_model(test_file)
def save_best_model():
model = LanguageModelingModel(
'convbert',
'outputs/best_model',
args={"output_dir": "discriminator_trained"}
)
model.save_discriminator()
if __name__ == '__main__':
os.environ["CUDA_VISIBLE_DEVICES"] = "1"
# proc_data()
main()
# save_best_model()