-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtune_hps_singletask.py
184 lines (152 loc) · 8.03 KB
/
tune_hps_singletask.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
# coding=utf-8
# Copyright 2018 The Google AI Language Team Authors and The HugginFace Inc. team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import sys
import argparse
import logging
import random
import numpy as np
import torch
import pandas as pd
from run_singletask import run
def main():
parser = argparse.ArgumentParser()
## Basic parameters
parser.add_argument("--task_dir", default="data", required=True)
parser.add_argument("--train_file", default="data", required=False)
parser.add_argument("--dev_file", default="data", required=False)
parser.add_argument("--test_file", default="data", required=False)
parser.add_argument("--dataset", default="nlp_forest_single", required=False)
parser.add_argument("--model", default="facebook/bart-base", required=False)
parser.add_argument("--output_dir", default=None, type=str, required=True)
parser.add_argument("--do_train", action='store_true')
parser.add_argument("--do_predict", action='store_true')
parser.add_argument("--predict_checkpoint", type=str, default="best-model.pt")
## Model parameters
parser.add_argument("--checkpoint", type=str)
parser.add_argument("--do_lowercase", action='store_true', default=False)
parser.add_argument("--freeze_embeds", action='store_true', default=False)
# Preprocessing/decoding-related parameters
parser.add_argument('--max_input_length', type=int, default=512)
parser.add_argument('--max_output_length', type=int, default=64)
parser.add_argument('--num_beams', type=int, default=4)
parser.add_argument("--append_another_bos", action='store_true', default=False)
# Training-related parameters
parser.add_argument("--train_batch_size", default=64, type=int,
help="Batch size per GPU/CPU for training.")
parser.add_argument("--predict_batch_size", default=64, type=int,
help="Batch size per GPU/CPU for evaluation.")
parser.add_argument("--learning_rate", default=3e-5, type=float,
help="The initial learning rate for Adam.")
parser.add_argument("--warmup_proportion", default=0.01, type=float,
help="Weight decay if we apply some.")
parser.add_argument("--weight_decay", default=0.01, type=float,
help="Weight deay if we apply some.")
parser.add_argument("--adam_epsilon", default=1e-8, type=float,
help="Epsilon for Adam optimizer.")
parser.add_argument("--max_grad_norm", default=0.1, type=float,
help="Max gradient norm.")
parser.add_argument("--gradient_accumulation_steps", default=1, type=int,
help="Max gradient norm.")
parser.add_argument("--num_train_epochs", default=100000.0, type=float,
help="Total number of training epochs to perform.")
parser.add_argument("--warmup_steps", default=500, type=int,
help="Linear warmup over warmup_steps.")
parser.add_argument("--total_steps", default=100000, type=int,
help="Linear warmup over warmup_steps.")
parser.add_argument('--wait_step', type=int, default=10000000000)
# Other parameters
parser.add_argument("--quiet", action='store_true',
help="If true, all of the warnings related to data processing will be printed. "
"A number of warnings are expected for a normal SQuAD evaluation.")
parser.add_argument('--eval_period', type=int, default=2000,
help="Evaluate & save model")
parser.add_argument('--prefix', type=str, default='',
help="Prefix for saving predictions")
parser.add_argument('--debug', action='store_true',
help="Use a subset of data for debugging")
parser.add_argument('--seed', type=int, default=42,
help="random seed for initialization")
# to tune
parser.add_argument("--learning_rate_list", nargs="*", type=float, default=[])
parser.add_argument("--bsz_list", nargs="*", type=int, default=[])
args = parser.parse_args()
if os.path.exists(args.output_dir) and os.listdir(args.output_dir):
print("Output directory () already exists and is not empty.")
if not os.path.exists(args.output_dir):
os.makedirs(args.output_dir, exist_ok=True)
##### Start writing logs
log_filename = "{}log.txt".format("" if args.do_train else "eval_")
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(name)s - %(message)s',
datefmt='%m/%d/%Y %H:%M:%S',
level=logging.INFO,
handlers=[logging.FileHandler(os.path.join(args.output_dir, log_filename)),
logging.StreamHandler()])
logger = logging.getLogger(__name__)
logger.info(args)
logger.info(args.output_dir)
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
args.n_gpu = torch.cuda.device_count()
if args.n_gpu > 0:
torch.cuda.manual_seed_all(args.seed)
if not args.do_train and not args.do_predict:
raise ValueError("At least one of `do_train` or `do_predict` must be True.")
if args.do_train:
if not args.train_file:
raise ValueError("If `do_train` is True, then `train_dir` must be specified.")
if not args.dev_file:
raise ValueError("If `do_train` is True, then `predict_dir` must be specified.")
if args.do_predict:
if not args.test_file:
raise ValueError("If `do_predict` is True, then `predict_dir` must be specified.")
logger.info("Using {} gpus".format(args.n_gpu))
files = sorted(os.listdir(args.task_dir))
prefixes = []
for filename in files:
if not filename.endswith(".tsv"):
continue
prefix = "_".join(filename.split("_")[:-1])
if prefix not in prefixes:
prefixes.append(prefix)
logger.info("Fine-tuning the following samples: {}".format(prefixes))
df = pd.DataFrame(columns=["prefix", "lr", "bsz", "dev_performance", "test_performance"])
for prefix in prefixes:
args.train_file = os.path.join(args.task_dir, prefix + "_train.tsv")
args.dev_file = os.path.join(args.task_dir, prefix + "_dev.tsv")
args.test_file = os.path.join(args.task_dir, prefix + "_test.tsv")
best_dev_performance = -1.0
best_config = None
for lr in args.learning_rate_list:
for bsz in args.bsz_list:
logger.info("Running ... prefix={}, lr={}, bsz={} ...".format(prefix, lr, bsz))
args.learning_rate = lr
args.train_batch_size = bsz
dev_performance, test_performance = run(args, logger)
logger.info("prefix={}, lr={}, bsz={}, dev_performance={}, test_performance={}".format(prefix, lr, bsz, dev_performance, test_performance))
df.loc[len(df.index)] = [prefix, lr, bsz, dev_performance, test_performance]
df.to_csv(os.path.join(args.output_dir, "result.csv"))
if dev_performance > best_dev_performance:
best_dev_performance = dev_performance
best_config = [prefix, lr, bsz, dev_performance, test_performance]
best_config[0] = best_config[0] + "_best"
df.loc[len(df.index)] = best_config
df.to_csv(os.path.join(args.output_dir, "result.csv"))
if __name__=='__main__':
main()