-
Notifications
You must be signed in to change notification settings - Fork 52
/
generate_sequences.py
257 lines (250 loc) · 7.77 KB
/
generate_sequences.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
# -*- coding: utf-8 -*-
# !/usr/bin/python
#
"""
generate sequences
@author: hongyuan
"""
import pickle
import time
import numpy
import theano
from theano import sandbox
import theano.tensor as tensor
import os
import scipy.io
from collections import defaultdict
from theano.tensor.shared_randomstreams import RandomStreams
#
import modules.sequence_generators as seq_gens
import datetime
#
import argparse
__author__ = 'Hongyuan Mei'
def main():
parser = argparse.ArgumentParser(
description='Generating sequences... '
)
#
parser.add_argument(
'-m', '--ModelGen', #required=True,
default = 'conttime',
type = str,
choices = ['hawkes', 'hawkesinhib', 'conttime'],
help='Model used to generate data'
)
parser.add_argument(
'-sp', '--SetParams', #required=False,
default = 0,
type = int,
choices = [0, 1],
help='Do we set the params ? 0 -- False; 1 -- True'
)
parser.add_argument(
'-st', '--SumForTime',
default = 0, type = int, choices = [0, 1],
help='Do we use total intensity for time sampling? 0 -- False; 1 -- True'
)
parser.add_argument(
'-fp', '--FilePretrain', required=False,
help='File of pretrained model (e.g. ./tracks/track_PID=XX_TIME=YY/model.pkl)'
)
#
parser.add_argument(
'-s', '--Seed', #required=False,
default = 12345, type = int,
help='Seed of random state'
)
parser.add_argument(
'-k', '--DimProcess', #required=False,
default = 5, type = int,
help='Number of event types'
)
parser.add_argument(
'-d', '--DimLSTM', #required=False,
default = 32, type = int,
help='Dimension of LSTM generator'
)
parser.add_argument(
'-N', '--NumSeqs', #required=False,
default = 12000, type = int,
help='Number of sequences to simulate'
)
parser.add_argument(
'-min', '--MinLen', #required=False,
default = 20, type = int,
help='Min len of sequences '
)
parser.add_argument(
'-max', '--MaxLen', #required=False,
default = 100, type = int,
help='Max len of sequences '
)
#
args = parser.parse_args()
args.DimProcess = numpy.int32(args.DimProcess)
args.DimLSTM = numpy.int32(args.DimLSTM)
args.Seed = numpy.int32(args.Seed)
args.NumSeqs = numpy.int32(args.NumSeqs)
args.MinLen = numpy.int32(args.MinLen)
args.MaxLen = numpy.int32(args.MaxLen)
if args.SetParams == 0:
args.SetParams = False
else:
args.SetParams = True
#
if args.SumForTime == 0:
args.SumForTime = False
else:
args.SumForTime = True
#
id_process = os.getpid()
time_current = datetime.datetime.now().isoformat()
#
if args.SetParams:
tag_model = '_ModelGen='+args.ModelGen+'_SetParams'+'_PID='+str(id_process)+'_TIME='+time_current
else:
tag_model = '_ModelGen='+args.ModelGen+'_PID='+str(id_process)+'_TIME='+time_current
#
#
file_save = './data/data'+tag_model+'.pkl'
file_save = os.path.abspath(file_save)
file_model = './gen_models/model'+tag_model+'.pkl'
file_model = os.path.abspath(file_model)
#
settings_gen = {
'dim_process': args.DimProcess,
'dim_LSTM': args.DimLSTM,
#'dim_states': args.DimStates,
'seed_random': args.Seed,
'path_pre_train': args.FilePretrain,
'sum_for_time': args.SumForTime,
'args': None
}
settings_gen_seqs = {
'num_seqs': args.NumSeqs,
'min_len': args.MinLen,
'max_len': args.MaxLen
}
#print settings_gen_seqs
#
flag_1 = (
args.ModelGen == 'hawkes' or args.ModelGen == 'hawkesinhib' or args.ModelGen == 'neural' or args.ModelGen == 'neuralgeneral' or args.ModelGen == 'fst' or args.ModelGen == 'neuraladapt'
)
flag_2 = (
args.ModelGen == 'neuraladapttime' or args.ModelGen == 'neuraladapttimescale' or args.ModelGen == 'neuralreduce' or args.ModelGen == 'conttime'
)
assert( flag_1 or flag_2 )
#
if args.ModelGen == 'hawkes':
gen_model = seq_gens.HawkesGen(settings_gen)
elif args.ModelGen == 'hawkesinhib':
gen_model = seq_gens.HawkesInhibGen(settings_gen)
elif args.ModelGen == 'neural':
gen_model = seq_gens.NeuralHawkesGen(settings_gen)
elif args.ModelGen == 'neuralgeneral':
gen_model = seq_gens.GeneralizedNeuralHawkesGen(
settings_gen
)
elif args.ModelGen == 'neuraladapt':
gen_model = seq_gens.NeuralHawkesAdaptiveBaseGen(
settings_gen
)
elif args.ModelGen == 'neuraladapttime':
gen_model = seq_gens.NeuralHawkesAdaptiveBaseGen_time(
settings_gen
)
elif args.ModelGen == 'neuraladapttimescale':
gen_model = seq_gens.NeuralHawkesAdaptiveBaseGen_time_scale(
settings_gen
)
elif args.ModelGen == 'neuralreduce':
gen_model = seq_gens.NeuralHawkesAdaptiveBaseGen_time_scale_reduce(
settings_gen
)
elif args.ModelGen == 'conttime':
gen_model = seq_gens.NeuralHawkesCTLSTM(
settings_gen
)
#elif args.ModelGen == 'fst':
# gen_model = seq_gens.FSTGen(settings_gen)
else:
print "Generator NOT implemented : ", args.ModelGen
#
#
if args.SetParams:
gen_model.set_params()
args.DimProcess = gen_model.dim_process
#
## show values ##
print ("PID is : %s" % str(id_process) )
print ("TIME is : %s" % time_current )
print ("Seed is : %s" % str(args.Seed) )
print ("FilePretrain is : %s" % args.FilePretrain)
print ("Generator is : %s" % args.ModelGen )
print ("SetParams is : %s" % args.SetParams )
print ("FileSave is : %s" % file_save )
print ("FileModel is : %s" % file_model )
print ("DimProcess is : %s" % str(args.DimProcess) )
if 'neural' in args.ModelGen or 'conttime' in args.ModelGen:
print ("DimLSTM is : %s" % str(args.DimLSTM) )
#if 'fst' in args.ModelGen:
# print ("DimStates is : %s" % str(args.DimStates) )
print ("NumSeqs is : %s" % str(args.NumSeqs) )
print ("MinLen is : %s" % str(args.MinLen) )
print ("MaxLen is : %s" % str(args.MaxLen) )
print ("SumForTime is : %s" % str(args.SumForTime) )
#
#
dict_args = {
'PID': id_process,
'TIME': time_current,
'ModelGen': args.ModelGen,
'SetParams': args.SetParams,
'FileSave': file_save,
'FileModel': file_model,
'DimProcess': args.DimProcess,
'DimLSTM': args.DimLSTM,
#'DimStates': args.DimStates,
'Seed': args.Seed,
'FilePretrain': args.FilePretrain,
'NumSeqs': args.NumSeqs,
'MinLen': args.MinLen,
'MaxLen': args.MaxLen,
'SumForTime': args.SumForTime
}
#
gen_model.set_args(dict_args)
#
cut_train = numpy.int32(8000)
cut_dev = numpy.int32(9000)
cut_test = numpy.int32(10000)
print "The cut off for training, dev, test and test1 are : ", (cut_train, cut_dev, cut_test, args.NumSeqs)
#
#
time_0 = time.time()
gen_model.gen_seqs(settings_gen_seqs)
time_1 = time.time()
dtime = time_1 - time_0
gen_model.print_some()
#
gen_model.save_model(file_model)
#
dict_data = {
'train': gen_model.list_seqs[:cut_train],
'dev': gen_model.list_seqs[cut_train:cut_dev],
'test': gen_model.list_seqs[
cut_dev:cut_test
],
'test1': gen_model.list_seqs[
cut_test:
],
'args': dict_args
}
#
print "saving ... "
with open(file_save, 'wb') as f:
pickle.dump(dict_data, f)
print "finished ! Took {} seconds !!!".format(str(round(dtime,2)))
#
if __name__ == "__main__": main()