-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
159 lines (131 loc) · 4.66 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
import wandb
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import optimizers, callbacks
from wandb.keras import WandbCallback
from hydra import initialize, compose
from model.model import DialogTrain
from utils.data import get_emotion_dataset, get_chatbot_dataset
def pretrain(config_path: str, config_name: str, data_path: str, save_path: str):
"""pretrain
Args:
config_name (str): config file name
config_path (str): config path
data_path (str): train and val data path
save_path (str): dialoGPT save path
"""
# Wandb & Hydra
initialize(config_path)
cfg = compose(config_name)
#wandb.init(project="DialoGPT", 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 = DialogTrain(
cfg.model.vocab_size,
cfg.processing.max_len,
cfg.model.d_h,
cfg.model.head,
cfg.model.d_ff,
cfg.processing.rate,
cfg.model.n_layer,
)
# 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(save_path)
def pretrain_tpu(config_path: str, config_name: str, data_path: str, save_path: str):
"""pretrain
Args:
config_name (str): config file name
config_path (str): config path
data_path (str): data path
save_path (str): DialoGPT save 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)
localhost_save_option = tf.saved_model.SaveOptions(experimental_io_device="/job:localhost")
# Wandb & Hydra
initialize(config_path)
cfg = compose(config_name)
wandb.init(project="DialoGPT", 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 = DialogTrain(
cfg.model.vocab_size,
cfg.processing.max_len,
cfg.model.d_h,
cfg.model.head,
cfg.model.d_ff,
cfg.processing.rate,
cfg.model.n_layer,
)
# 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),
callbacks.ModelCheckpoint(save_path, monitor='val_ppl', options=localhost_save_option, save_best_only=True)
]
)
if __name__=="__main__":
version = "small"
config_name = f"{version}.yaml"
config_path = "./configs"
data_path = {
'train': "./data/emotion1/idx_train.json",
'val': "./data/emotion1/idx_val.json"
}
save_path = f"./save/{version}"
type = input("type of device: ")
if type == 'gpu' or type == 'GPU' or type == 'cpu' or type == 'CPU':
pretrain(config_path, config_name, data_path, save_path)
elif type == 'tpu' or type == 'TPU':
pretrain_tpu(config_path, config_name, data_path, save_path)
else:
print(f"{type} device doesn't exists")