-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
189 lines (157 loc) · 5.33 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import warnings
warnings.filterwarnings("ignore")
import argparse
import os
import numpy as np
import pandas as pd
import tensorflow as tf
import transformers
from tensorflow.keras import backend as K
from transformers import AutoTokenizer
from data.datasets import data_generator, regular_encode
from data.text import tokenized_text_normalize, vlsp_impute
from data.utils import seed_all
from trainer.model import build_model, scheduler
print("Using Tensorflow version:", tf.__version__)
print("Using Transformers version:", transformers.__version__)
parser = argparse.ArgumentParser()
os.environ["CUDA_VISIBLE_DEVICES"] = "1"
parser.add_argument(
"--train_file",
type=str,
default="../data/final_data/train_5_folds.csv",
help="path to k-fold-splited and tokenized train data",
)
parser.add_argument(
"--test_file",
default="../data/final_data/private_test.csv",
type=str,
help="path to tokenized test data",
)
parser.add_argument(
"--do_train",
default=True,
type=bool,
help="whether train the pretrained model with provided train data",
)
parser.add_argument(
"--do_infer",
default=True,
type=bool,
help="whether predict the pretrained model with provided test data",
)
parser.add_argument(
"--pretrained_bert",
default="vinai/phobert-base",
type=str,
help="path to pretrained bert model path or directory",
)
parser.add_argument(
"--max_len",
default=256,
type=int,
help="max sequence length for padding and truncation",
)
parser.add_argument(
"--batch_size",
default=24,
type=int,
help="num examples per batch",
)
parser.add_argument(
"--n_epochs",
default=5,
type=int,
help="num epochs required for training",
)
parser.add_argument(
"--output_dir",
default="../outputs",
type=str,
help="path to output model weights directory",
)
parser.add_argument(
"--seed",
default=1710,
type=int,
help="seed for reproceduce",
)
args = parser.parse_args()
if __name__ == "__main__":
#
seed_all(args.seed)
gpus = tf.config.experimental.list_physical_devices("GPU")
if gpus:
try:
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
except RuntimeError as e:
print(e)
# Load and process data
train_df = pd.read_csv(args.train_file)
test_df = pd.read_csv(args.test_file)
train_df["post_message"] = train_df["post_message"].astype(str)
test_df["post_message"] = test_df["post_message"].astype(str)
train_df["post_message"] = train_df["post_message"].apply(tokenized_text_normalize)
test_df["post_message"] = test_df["post_message"].apply(tokenized_text_normalize)
train_df = vlsp_impute(train_df)
test_df = vlsp_impute(test_df)
roberta = args.pretrained_bert
roberta_tokenizer = AutoTokenizer.from_pretrained(roberta)
model = build_model(roberta, max_len=args.max_len)
model.summary()
DISPLAY = 1 # USE display=1 FOR INTERACTIVE
os.makedirs(args.output_dir, exist_ok=True)
strategy = tf.distribute.MirroredStrategy()
if args.do_train:
folds = train_df["fold"].unique()
for fold in sorted(folds):
print("*" * 100)
print(f"FOLD: {fold+1}/{len(folds)}")
K.clear_session()
with strategy.scope():
model = build_model(roberta, max_len=args.max_len)
reduce_lr = tf.keras.callbacks.LearningRateScheduler(scheduler)
model_dir = os.path.join(args.output_dir, f"Fold_{fold+1}.h5")
sv = tf.keras.callbacks.ModelCheckpoint(
model_dir,
monitor="val_auc",
verbose=1,
save_best_only=True,
save_weights_only=True,
mode="max",
save_freq="epoch",
)
train_df_ = train_df[train_df["fold"] != fold]
val_df_ = train_df[train_df["fold"] == fold]
train_dataset, valid_dataset = data_generator(
train_df_, val_df_, roberta_tokenizer, max_len=args.max_len, batch_size=args.batch_size
)
n_steps = train_df_.shape[0] // args.batch_size + 1
train_history = model.fit(
train_dataset,
steps_per_epoch=n_steps,
callbacks=[
sv,
reduce_lr,
],
validation_data=valid_dataset,
epochs=args.n_epochs,
)
if args.do_infer:
X_test = regular_encode(test_df, roberta_tokenizer, max_len=args.max_len)
y_test = np.zeros((len(test_df), 1))
test_dataset = tf.data.Dataset.from_tensor_slices((X_test, y_test)).batch(args.batch_size)
model = build_model(roberta, max_len=args.max_len)
preds = []
for i, file_name in enumerate(os.listdir(args.output_dir)):
print("*" * 100)
K.clear_session()
model_path = os.path.join(args.output_dir, file_name)
print(f"Inferencing with model from: {model_path}")
model.load_weights(model_path)
pred = model.predict(test_dataset, batch_size=128, verbose=DISPLAY)
preds.append(pred)
preds = np.mean(preds, axis=0)
test_df["prediction"] = preds
test_df["prediction"].to_csv("submission.csv", header=False)