-
Notifications
You must be signed in to change notification settings - Fork 5
/
arg_convertor.py
291 lines (241 loc) · 8.37 KB
/
arg_convertor.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
'''=================================================
@Project -> File:新建文件夹->arg_convertor
@IDE:PyCharm
@coding: utf-8
@time:2021/7/24 16:57
@author:Pengzhangzhi
@Desc:
=================================================='''
import json
import torch
def json2dict(json_path, verbose=True):
"""convert json file into class args."""
with open(json_path) as f:
arg_dict = json.load(f)
if verbose:
print("*********preprocessing hyper-parameters ***********")
for key, value in arg_dict.items():
print(f"{key}:{value}\t", end=" ")
print()
print("*" * 30)
return arg_dict
def json2args(json_path, verbose=True):
"""
json file to arg class.
:param json_path:
:param verbose:
:return:
"""
arg_dict = json2dict(json_path, verbose=verbose)
return type("temp", (), arg_dict)
def arg_class2dict(arg_class):
"""convert arg class to dict."""
arg_dict = arg_class.__dict__.copy()
temp_dict = {}
for key, values in arg_dict.items():
if not key.startswith("_"):
temp_dict[key] = values
return temp_dict
def arg_class2json(arg_class, path):
"""generate args json file."""
temp_dict = arg_class2dict(arg_class)
with open(path, 'w', encoding='utf-8') as file:
json.dump(temp_dict, file, ensure_ascii=False, indent=2)
def convert_TaxiBJ(c=4, t=1, depth=2, pretrain_epoch=600, sp=True, pretrain_way_="random", config_name="TaxiBJ",
pretrain_times_=1, ex=28):
class arg:
split = 0.1
batch_size = 128
lr = 0.001
lrf = 0.01
epochs = 600
device = "cuda" if torch.cuda.is_available() else "cpu"
consider_external_info = True
len_closeness = c
len_period = 0
len_trend = t
T = 48
nb_flow = 2
days_test = 28
map_height = 32
map_width = 32
m_factor = 1.0
m_factor_2 = 1.0
dataset = "TaxiBJ"
prediction_offset = 0
random_pick = True
pretrain_epochs = pretrain_epoch
pretrain_times = pretrain_times_
pretrain_way = pretrain_way_
experiment_name = config_name
ext_dim = 28
drop_prob = 0.1
conv_channels = 64
pre_conv = True
seq_pool = False
shortcut = True
patch_size = 8
close_channels = len_closeness * nb_flow
trend_channels = len_trend * nb_flow
close_dim = 128
trend_dim = 128
close_depth = depth
trend_depth = depth
close_head = 2
trend_head = 2
close_mlp_dim = 512
trend_mlp_dim = 512
arg_class2json(arg, f"config\{config_name}.json")
def convert_BikeNYC(c=6, t=2,
pt=0, pw="random",
pc=True, sp=True, sc=True,
depth=2,
ps=4,
config_name="BikeNYC", ):
class arg:
split = 0.1
batch_size = 128
lr = 0.001
lrf = 0.01
epochs = 1000
device = "cuda"
consider_external_info = True
len_closeness = c
len_period = 0
len_trend = t
T = 24
nb_flow = 2
days_test = 10
map_height = 16
map_width = 8
m_factor = 1
m_factor_2 = 1
# m_factor = 1.2570787221094177
# m_factor_2 = 1.5802469135802468
dataset = "BikeNYC"
prediction_offset = 0
random_pick = False
pretrain_epochs = 600
pretrain_times = pt
pretrain_way = pw
experiment_name = config_name
ext_dim = 24
drop_prob = 0.1
conv_channels = 64
pre_conv = pc
seq_pool = False
shortcut = sc
patch_size = ps
close_channels = len_closeness * nb_flow
trend_channels = len_trend * nb_flow
close_dim = 128
trend_dim = 128
close_depth = depth
trend_depth = depth
close_head = 2
trend_head = 2
close_mlp_dim = 512
trend_mlp_dim = 512
arg_class2json(arg, f"config\{config_name}.json")
def convert_TaxiNYC(c=6, t=2,
pt=1, pw="random",
pc=True, sp=True, sc=True,
depth=2, ps=8, trans_dim=128,
config_name="TaxiNYC", ):
class arg:
split = 0.1
batch_size = 128
lr = 0.005
lrf = 0.01
epochs = 800
device = "cuda"
consider_external_info = 1
len_closeness = c
len_period = 0
len_trend = t
T = 24
nb_flow = 2
days_test = 28
map_height = 16
map_width = 8
m_factor = 1
m_factor_2 = 1
dataset = "TaxiNYC"
prediction_offset = 0
random_pick = False
pretrain_epochs = 600
pretrain_times = pt
pretrain_way = pw
shuffle_mode = ""
experiment_name = config_name
ext_dim = 24
drop_prob = 0.1
conv_channels = 64
pre_conv = pc
seq_pool = False
shortcut = sc
patch_size = ps
close_channels = len_closeness * nb_flow
trend_channels = len_trend * nb_flow
close_dim = trans_dim
trend_dim = trans_dim
close_depth = depth
trend_depth = depth
close_head = 2
trend_head = 2
close_mlp_dim = 512
trend_mlp_dim = 512
arg_class2json(arg, f"config\{config_name}.json")
def generate_idx_depth_args():
for depth in [2, 8]:
for index in range(1, 8):
convert_TaxiBJ(index=index, depth=depth, config_name=f"exchange_idx={index}_depth={depth}")
print(fr"exchange_idx={index}, depth={depth}")
convert_BikeNYC()
convert_TaxiNYC()
def generate_depth_args():
for depth in range(1, 9):
convert_TaxiBJ(depth=depth, config_name=f"depth={depth}_c10_p4_no_pretrain_pretrain_1")
print(fr" depth={depth}")
def generate_depth_shuffle_args():
for depth in range(2, 9):
for pretrain_time in [1, 4]:
convert_TaxiBJ(c=10, t=4, pretrain_way_="shuffle",
pretrain_times_=pretrain_time, depth=depth,
config_name=f"shuffle_each_pretrain_{pretrain_time}_depth={depth}_c10_p4")
print(fr" depth={depth}")
def generate_shuffle_with_default_data_Scale():
for pretrain_way in ["shuffle", "same_shuffle"]:
for shuffle_mode in ["shuffle_his", "shuffle_all"]:
for pretrain_time in [1, 4]:
convert_TaxiBJ(c=6, t=2, depth=2,
pretrain_way_=pretrain_way,
shuffle_mode_=shuffle_mode,
pretrain_times_=pretrain_time,
config_name=f"c6t2_depth_2{pretrain_way}_{shuffle_mode}_pretrain_times_{pretrain_time}")
def generate_c4_t1_random_args():
for depth in range(2, 9):
convert_TaxiBJ(c=4, t=1, pretrain_way_="random",
depth=depth, pretrain_times_=1,
config_name=f"random_c4_t1_pretrain_1_depth={depth}")
print(f"random_c4_t1_pretrain_1_depth={depth}")
def generate_random_pick(c=4, t=1, depth=6, pretrain_way_="random", pretrain_times_=1):
convert_TaxiBJ(c=c, t=t, depth=depth,
pretrain_way_=pretrain_way_,
pretrain_times_=pretrain_times_,
config_name=f"c{c}t{t}_depth_{depth}_pretrain_way_{pretrain_way_}_pretrain_times_{pretrain_times_}",
)
def generate_TaxiBJ(c=4, t=1, pretrain_times_=1, ):
for i in range(1, 11):
convert_TaxiBJ(c=c, t=t, sp=False, pretrain_times_=pretrain_times_, config_name=f"TaxiBJ{i}")
def generate_TaxiNYC():
for i in range(1, 11):
convert_TaxiNYC(c=10, t=4, pt=1, sp=False, config_name=f"TaxiNYC{i}")
if __name__ == '__main__':
# convert_BikeNYC()
generate_TaxiBJ()
generate_TaxiNYC()
# convert_TaxiBJ(c=6, t=2, depth=2,
# pretrain_epoch=600,
# pretrain_way_="random", config_name="TaxiBJ",
# pretrain_times_=1)