-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
167 lines (139 loc) · 4.91 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
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
import wandb
import tensorflow as tf
from typing import Dict
from tensorflow.keras import optimizers, callbacks
from wandb.keras import WandbCallback
from hydra import initialize, compose
from model.model import DialogNug, DialogAll
from utils.data import get_nug_emotion_dataset, get_emotion_dataset
def pretrain(config_path: str, config_name: str, data_path: Dict, save_path: Dict):
"""pretrain
Args:
config_name (str): config file name
config_path (str): config path
data_path ({'train': str, 'val': str}): train and val data path
save_path ({'dialog': str, 'gen': str}): dialogBERT and Generator weights path
"""
# Wandb & Hydra
initialize(config_path)
cfg = compose(config_name)
#wandb.init(project="dialogBERT", entity="gj98", config=cfg)
# DATASET
train_dataset = get_emotion_dataset(data_path["train"], cfg.processing.batch_size)
val_dataset = get_emotion_dataset(data_path["val"], cfg.processing.batch_size)
print(f'train dataset: {train_dataset}\n')
print(f'val dataset: {val_dataset}\n')
# MODEL
model = DialogAll(
cfg.model.vocab_size,
cfg.processing.uttr_len,
cfg.processing.cntxt_len,
cfg.model.d_h,
cfg.model.head,
cfg.model.d_ff,
cfg.model.uttr_layer,
cfg.model.cntxt_layer,
cfg.processing.p,
)
# COMPILE
model.compile(
optimizer=optimizers.Adam(
cfg.training.learning_rate,
beta_1=cfg.training.beta1,
beta_2=cfg.training.beta2,
epsilon=cfg.training.eps),
)
# TRAIN
model.fit(
train_dataset,
epochs=cfg.training.epoch,
validation_data=val_dataset,
callbacks=[
#WandbCallback(),
callbacks.ReduceLROnPlateau(patience=1),
callbacks.EarlyStopping(patience=3)
]
)
# SAVE
model.dialog.save_weights(save_path['dialog'])
model.gen.save_weights(save_path['gen'])
def pretrain_tpu(config_path, config_name, data_path, save_path):
"""pretrain
Args:
config_name (str): config file name
config_path (str): config path
data_path (dict): train and val data path
save_path (dict): dialogBERT and Generator weights path
"""
# TPU
print("Tensorflow version " + tf.__version__)
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver() # TPU detection
print('Running on TPU ', tpu.cluster_spec().as_dict()['worker'])
except ValueError:
raise BaseException('ERROR: Not connected to a TPU runtime; please see the previous cell in this notebook for instructions!')
tf.config.experimental_connect_to_cluster(tpu)
tf.tpu.experimental.initialize_tpu_system(tpu)
tpu_strategy = tf.distribute.experimental.TPUStrategy(tpu)
# Wandb & Hydra
initialize(config_path)
cfg = compose(config_name)
wandb.init(project="dialogBERT", entity="gj98", config=cfg)
# DATASET
train_dataset = get_emotion_dataset(data_path["train"], cfg.processing.batch_size)
val_dataset = get_emotion_dataset(data_path["val"], cfg.processing.batch_size)
print(f'train dataset: {train_dataset}\n')
print(f'val dataset: {val_dataset}\n')
with tpu_strategy.scope():
# MODEL
model = DialogAll(
cfg.model.vocab_size,
cfg.processing.uttr_len,
cfg.processing.cntxt_len,
cfg.model.d_h,
cfg.model.head,
cfg.model.d_ff,
cfg.model.uttr_layer,
cfg.model.cntxt_layer,
cfg.processing.p,
)
# COMPILE
model.compile(
optimizer=optimizers.Adam(
cfg.training.learning_rate,
beta_1=cfg.training.beta1,
beta_2=cfg.training.beta2,
epsilon=cfg.training.eps),
)
# TRAIN
model.fit(
train_dataset,
epochs=cfg.training.epoch,
validation_data=val_dataset,
callbacks=[
WandbCallback(),
callbacks.ReduceLROnPlateau(patience=1),
callbacks.EarlyStopping(patience=2)
]
)
# SAVE
localhost_save_option = tf.saved_model.SaveOptions(experimental_io_device="/job:localhost")
model.dialog.save_weights(save_path['dialog'], options=localhost_save_option)
model.gen.save_weights(save_path['gen'], options=localhost_save_option)
if __name__=="__main__":
version = "small_3_6"
config_name = f"{version}.yaml"
config_path = "./configs"
data_path = {
"train": "./data/emotion/idx_train.json",
"val": "./data/emotion/idx_val.json"
}
save_path = {
"dialog": f"./save/all/dialog_{version}",
"gen": f"./save/all/gen_{version}"
}
type = False
if type:
pretrain(config_path, config_name, data_path, save_path)
else:
pretrain_tpu(config_path, config_name, data_path, save_path)