-
Notifications
You must be signed in to change notification settings - Fork 0
/
ensemble_size_gbert.py
319 lines (271 loc) · 12.4 KB
/
ensemble_size_gbert.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import hashlib
import os
import sys
from os.path import realpath, join, dirname
import numpy as np
import pandas as pd
import tensorflow as tf
import torch
from keras.layers import Dense, Input
from keras.models import Sequential
from sklearn.model_selection import KFold
from sklearn.preprocessing import StandardScaler
from tensorflow.python.keras.callbacks import EarlyStopping, ModelCheckpoint
from tensorflow.python.keras.models import load_model
from transformers import (
AutoTokenizer,
TrainingArguments,
AutoModelForSequenceClassification,
set_seed,
)
sys.path.insert(0, realpath(join(dirname(__file__), '..')))
from util.helpers import (
compute_metrics,
load_dataset_with_features, get_hugging_face_name, TCCDataset, RegressionTrainer,
compute_metrics_for_regression, OptimizedESCallback
)
os.environ['CUDA_DEVICE_ORDER'] = 'PCI_BUS_ID'
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
gpus = tf.config.experimental.list_physical_devices('GPU')
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
BOOTSTRAP_SIZE = 1000 # 1000
MAX_ENSEMBLE_SIZE = 60 # 60
ENSEMBLE_POOL_SIZE = 100 # 100
N_FOLDS = 5
MODEL_NAME = 'gbert' # ['gbert', 'gelectra', 'gottbert', 'gerpt']
TRAIN_BATCH_SIZE = 16
VALID_BATCH_SIZE = 16
N_EVAL_STEPS = 23
EXPERIMENT_NAME = f'ensemble_{MODEL_NAME}'
EXPERIMENT_DIR = f'cache/{EXPERIMENT_NAME}'
df_train, feature_columns = load_dataset_with_features('training')
def get_predictions(
df_train_folds,
df_val_fold,
n_epochs=100,
n_log_steps=10,
):
# store predictions in dataframe
# columns: Sentence, Prediction of Model 1, Prediction of Model 2, ...
df_predictions_val_fold = df_val_fold[['ID', 'Sentence DE']].copy()
# get tokenizer
tokenizer = AutoTokenizer.from_pretrained(get_hugging_face_name(MODEL_NAME))
X_val_fold = df_val_fold['Sentence DE'].values
X_val_fold_features = df_val_fold[feature_columns].values
# tokenize
tokens_val_fold = tokenizer(X_val_fold.tolist(), padding='max_length', return_tensors='pt', truncation=True,
max_length=128)
for k in range(ENSEMBLE_POOL_SIZE):
df_early_stopping = df_train_folds.sample(frac=0.1, random_state=k)
df_train_no_es = df_train_folds.drop(
df_train_folds[
df_train_folds['ID'].isin(df_early_stopping['ID'])
].index
)
X_early_stopping = df_early_stopping['Sentence DE'].values
X_early_stopping_features = df_early_stopping[feature_columns].values
y_early_stopping = df_early_stopping['MOS'].values
X_training = df_train_no_es['Sentence DE'].values
X_training_features = df_train_no_es[feature_columns].values
y_training = df_train_no_es['MOS'].values
# tokenize
tokens_early_stopping = tokenizer(X_early_stopping.tolist(), padding='max_length', return_tensors='pt',
truncation=True, max_length=128)
tokens_training = tokenizer(X_training.tolist(), padding='max_length', return_tensors='pt', truncation=True,
max_length=128)
hash = (
hashlib.sha256(
pd.util.hash_pandas_object(df_train_no_es['ID'], index=True).values
).hexdigest()
+ '_'
+ get_hugging_face_name(MODEL_NAME)[
get_hugging_face_name(MODEL_NAME).find('/') + 1:
]
)
# load model and, if necessary, train it
try:
print(f'{EXPERIMENT_DIR}/models/{MODEL_NAME}/{hash}')
model = AutoModelForSequenceClassification.from_pretrained(
f'{EXPERIMENT_DIR}/models/{MODEL_NAME}/{hash}', local_files_only=True, num_labels=1
)
except EnvironmentError:
# create training dataset
early_stopping_dataset = TCCDataset(tokens_early_stopping, y_early_stopping)
training_dataset = TCCDataset(tokens_training, y_training)
training_args = TrainingArguments(
output_dir=f'{EXPERIMENT_DIR}/{MODEL_NAME}_trainer/',
num_train_epochs=n_epochs,
per_device_train_batch_size=TRAIN_BATCH_SIZE,
per_device_eval_batch_size=VALID_BATCH_SIZE,
warmup_ratio=0.3,
learning_rate=5e-5,
no_cuda=False,
metric_for_best_model='root_mean_squared_error',
greater_is_better=False,
load_best_model_at_end=True,
save_steps=N_EVAL_STEPS * 100_000,
# we never want to save a model through this function, but the parameter must be set, because of load_best_model_at_end=True
save_total_limit=1, # can be 1, because we only save, when we find a better model
eval_steps=N_EVAL_STEPS,
evaluation_strategy='steps',
seed=k,
logging_steps=n_log_steps,
logging_dir=f'{EXPERIMENT_DIR}/logs/member_{k}',
logging_strategy='steps',
)
set_seed(training_args.seed)
model = AutoModelForSequenceClassification.from_pretrained(
get_hugging_face_name(MODEL_NAME), num_labels=1
)
trainer = RegressionTrainer(
model=model,
args=training_args,
train_dataset=training_dataset,
eval_dataset=early_stopping_dataset,
compute_metrics=compute_metrics_for_regression,
callbacks=[OptimizedESCallback(patience=5, initial_steps_wo_save=300)],
)
# training
trainer.train()
# save model
model.save_pretrained(f'{EXPERIMENT_DIR}/models/{MODEL_NAME}/{hash}')
# load hidden states of model for validation and test data
hidden_state_val_fold = extract_hidden_state(model, tokens_val_fold)
# normalize data with StandardScaler
scaler = StandardScaler()
scaler.fit(df_train_folds[feature_columns].values)
X_val_fold_features_scaled = scaler.transform(X_val_fold_features)
X_val_fold_with_features = np.concatenate((hidden_state_val_fold.detach().numpy(), X_val_fold_features_scaled),
axis=1)
# load MLP model and, if necessary, train it
try:
mlp = load_model(f'{EXPERIMENT_DIR}/models/mlp/{hash}_mlp.h5')
except Exception:
hidden_state_train = extract_hidden_state(model, tokens_training)
hidden_state_early_stopping = extract_hidden_state(model, tokens_early_stopping)
np.random.seed(k)
mlp = Sequential(
[
Input(shape=(model.config.hidden_size + len(feature_columns),), name='input'),
Dense(model.config.hidden_size, activation='relu', name='layer1'),
Dense(1, activation='linear', name='layer2'),
]
)
mlp.compile(
optimizer='rmsprop',
loss=tf.keras.losses.MeanSquaredError(),
metrics=[tf.keras.metrics.RootMeanSquaredError()],
)
es = EarlyStopping(monitor='val_root_mean_squared_error', mode='min', verbose=1, patience=100)
mc = ModelCheckpoint(f'{EXPERIMENT_DIR}/models/mlp/{hash}_mlp.h5',
monitor='val_root_mean_squared_error',
mode='min', verbose=1, save_best_only=True)
# normalize data with StandardScaler
scaler = StandardScaler()
scaler.fit(X_training_features)
X_train_features_scaled = scaler.transform(X_training_features)
X_es_features_scaled = scaler.transform(X_early_stopping_features)
X_train_with_features = np.concatenate((hidden_state_train.detach().numpy(), X_train_features_scaled),
axis=1)
X_es_with_features = np.concatenate((hidden_state_early_stopping.detach().numpy(), X_es_features_scaled),
axis=1)
mlp.fit(X_train_with_features, y_training,
validation_data=(X_es_with_features, y_early_stopping),
batch_size=TRAIN_BATCH_SIZE,
epochs=5000, callbacks=[es, mc])
mlp = load_model(f'{EXPERIMENT_DIR}/models/mlp/{hash}_mlp.h5')
# predict MLP on validation and test sets
prediction_val_fold = mlp.predict(X_val_fold_with_features, batch_size=VALID_BATCH_SIZE)
df_predictions_val_fold[f'{MODEL_NAME}_prediction_{k}'] = prediction_val_fold
return df_predictions_val_fold
def extract_hidden_state(model, tokens, batch_size=16):
last_last_hidden_state = torch.zeros((len(tokens.input_ids), model.config.hidden_size))
model = model.cuda().eval()
with torch.no_grad():
for i in range(0, len(tokens.input_ids), batch_size):
if i + batch_size > len(tokens.input_ids):
input_i = tokens.input_ids[i:]
else:
input_i = tokens.input_ids[i:i + batch_size]
output = model(input_i.cuda(), output_hidden_states=True)
last_hidden_state = output.hidden_states[-1].cpu()
idx_last_token = torch.zeros(len(input_i)).long()
last_last_hidden_state[i:i + len(idx_last_token)] = last_hidden_state[
torch.arange(len(idx_last_token)), idx_last_token]
return last_last_hidden_state
# dataframe for each metric for each model for each ensemble size
# 3d array: [ensemble_size, model_index, metric_index]
df_macro_ensemble_scores = pd.DataFrame(
columns=[
'ensemble_size',
'model_name',
'mean_absolute_error_mean',
'mean_absolute_error_std',
'mean_squared_error_mean',
'mean_squared_error_std',
'root_mean_squared_error_mean',
'root_mean_squared_error_std',
]
)
for fold, (train_idx, val_idx) in enumerate(KFold(n_splits=N_FOLDS).split(df_train)):
df_train_folds = df_train.loc[train_idx]
df_val_fold = df_train.loc[val_idx]
# fill na with mean of columns of train data
df_train_folds = df_train_folds.fillna(df_train_folds.mean(numeric_only=True))
df_val_fold = df_val_fold.fillna(df_train_folds.mean(numeric_only=True))
y_val_fold = df_val_fold['MOS'].values
pool_predictions_val_fold = get_predictions(df_train_folds, df_val_fold)
for current_ensemble_size in range(1, MAX_ENSEMBLE_SIZE + 1):
np.random.seed(current_ensemble_size)
idx = np.random.choice(
ENSEMBLE_POOL_SIZE,
size=(BOOTSTRAP_SIZE, current_ensemble_size),
)
idx_mapped = np.array(
[
np.array(
[pool_predictions_val_fold[f'{MODEL_NAME}_prediction_{k}'] for k in j]
)
for j in idx
]
)
ensemble_predictions = np.array(
[np.sum(j, axis=0) / len(j) for j in idx_mapped]
)
ensemble_scores = [
compute_metrics(y_val_fold, pred) for pred in ensemble_predictions
]
df_ensemble_scores = pd.DataFrame(ensemble_scores).sort_index(axis=1)
# add to dataframe
df_macro_ensemble_scores = df_macro_ensemble_scores.append(
{
'ensemble_size': current_ensemble_size,
'model_name': MODEL_NAME,
'mean_absolute_error_mean': df_ensemble_scores[
'mean_absolute_error'
].mean(),
'mean_absolute_error_std': df_ensemble_scores[
'mean_absolute_error'
].std(),
'mean_squared_error_mean': df_ensemble_scores[
'mean_squared_error'
].mean(),
'mean_squared_error_std': df_ensemble_scores[
'mean_squared_error'
].std(),
'root_mean_squared_error_mean': df_ensemble_scores[
'root_mean_squared_error'
].mean(),
'root_mean_squared_error_std': df_ensemble_scores[
'root_mean_squared_error'
].std(),
},
ignore_index=True,
)
# write to csv for each model_name
df_macro_ensemble_scores[
df_macro_ensemble_scores['model_name'] == MODEL_NAME
].to_csv(
f'ensemble_scores_{MODEL_NAME}.csv', index=False, sep=';', encoding='utf-8'
)