-
Notifications
You must be signed in to change notification settings - Fork 7
/
train_model.py
254 lines (244 loc) · 6.9 KB
/
train_model.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
# -*- coding: utf-8 -*-
# !/usr/bin/python
"""
Created on Mar 18th 10:58:37 2016
train model
@author: hongyuan
"""
import pickle
import time
import numpy
import theano
from theano import sandbox
import theano.tensor as tensor
import os
import sys
import scipy.io
from collections import defaultdict
from theano.tensor.shared_randomstreams import RandomStreams
import modules.utils as utils
import modules.models as models
import modules.optimizers as optimizers
import modules.controllers as controllers
import modules.data_processers as data_processers
import jpype
import run_models
import datetime
dtype=theano.config.floatX
#
import argparse
__author__ = 'Hongyuan Mei'
def main():
parser = argparse.ArgumentParser(
description='Trainning model ... '
)
#
'''
modify here accordingly ...
'''
#
parser.add_argument(
'-m', '--Model', required=False,
help='Model to be trained '
)
parser.add_argument(
'-fd', '--FileData', required=False,
help='Path of the dataset'
)
#
parser.add_argument(
'-d', '--DimModel', required=False,
help='Dimension of LSTM model '
)
parser.add_argument(
'-ns', '--NumSel', required=False,
help='Number of pre-selection in expectation '
)
parser.add_argument(
'-s', '--Seed', required=False,
help='Seed of random state'
)
#
parser.add_argument(
'-fp', '--FilePretrain', required=False,
help='File of pretrained model'
)
parser.add_argument(
'-tp', '--TrackPeriod', required=False,
help='Track period of training'
)
parser.add_argument(
'-me', '--MaxEpoch', required=False,
help='Max epoch number of training'
)
parser.add_argument(
'-sb', '--SizeBatch', required=False,
help='Size of mini-batch'
)
parser.add_argument(
'-op', '--Optimizer', required=False,
help='Optimizer of training'
)
#
parser.add_argument(
'-pjvm', '--PathJVM', required=False,
help='Path to JVM'
)
parser.add_argument(
'-pjar', '--PathJAR', required=False,
help='Path to JAR'
)
parser.add_argument(
'-md', '--MaxDiff', required=False,
help='Max difference of numerical values in BleuScore'
)
#
parser.add_argument(
'-cf', '--Coef', required=False,
help='Coefficient of regularizor'
)
#
args = parser.parse_args()
#
if args.Model == None:
args.Model = 'selgen'
if args.FileData == None:
args.FileData = os.path.abspath('./data')
#
if args.TrackPeriod == None:
args.TrackPeriod = numpy.int32(100)
else:
args.TrackPeriod = numpy.int32(args.TrackPeriod)
if args.MaxEpoch == None:
args.MaxEpoch = numpy.int32(30)
else:
args.MaxEpoch = numpy.int32(args.MaxEpoch)
if args.SizeBatch == None:
args.SizeBatch = numpy.int32(50)
else:
args.SizeBatch = numpy.int32(args.SizeBatch)
if args.Optimizer == None:
args.Optimizer = 'adam'
else:
args.Optimizer = args.Optimizer
#
if args.DimModel == None:
args.DimModel = numpy.int32(500)
else:
args.DimModel = numpy.int32(args.DimModel)
if args.NumSel == None:
args.NumSel = numpy.float32(5.0)
else:
args.NumSel = numpy.float32(args.NumSel)
if args.Seed == None:
args.Seed = numpy.int32(12345)
else:
args.Seed = numpy.int32(args.Seed)
#
if args.Coef == None:
args.Coef = numpy.float32(1e-6)
else:
args.Coef = numpy.float32(args.Coef)
#
if args.PathJVM == None:
if sys.platform == 'linux2' or sys.platform == 'linux':
args.PathJVM = os.path.abspath(
jpype.getDefaultJVMPath()
)
elif sys.platform == 'darwin':
args.PathJVM = '/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre/lib/server/libjvm.dylib'
else:
args.PathJVM = None
print "system does not match ... "
else:
args.PathJVM = os.path.abspath(args.PathJVM)
if args.PathJAR == None:
args.PathJAR = os.path.abspath(
'./dist/generation.jar'
)
else:
args.PathJAR = os.path.abspath(args.PathJAR)
if args.MaxDiff == None:
args.MaxDiff = numpy.int32(5)
else:
args.MaxDiff = numpy.int32(args.MaxDiff)
#
#
id_process = os.getpid()
time_current = datetime.datetime.now().isoformat()
#
tag_model = '_PID='+str(id_process)+'_TIME='+time_current
#
path_track = './tracks/track' + tag_model + '/'
file_log = os.path.abspath(
path_track + 'log.txt'
)
path_save = path_track
command_mkdir = 'mkdir -p ' + os.path.abspath(
path_track
)
os.system(command_mkdir)
#
## show values ##
print ("PID is : %s" % str(id_process) )
print ("TIME is : %s" % time_current )
#
print ("Model is : %s" % args.Model )
print ("FileData is : %s" % args.FileData )
print ("DimModel is : %s" % str(args.DimModel) )
print ("NumSel is : %s" % str(args.NumSel) )
print ("Seed is : %s" % str(args.Seed) )
print ("FilePretrain is : %s" % args.FilePretrain)
print ("TrackPeriod is : %s" % str(args.TrackPeriod) )
print ("MaxEpoch is : %s" % str(args.MaxEpoch) )
print ("SizeBatch is : %s" % str(args.SizeBatch) )
print ("Optimizer is : %s" % args.Optimizer)
print ("MaxDiff is : %s" % str(args.MaxDiff) )
print ("Coef is : %s" % str(args.Coef) )
#
dict_args = {
'PID': id_process,
'TIME': time_current,
'Model': args.Model,
'FileData': args.FileData,
'DimModel': args.DimModel,
'Seed': args.Seed,
'FilePretrain': args.FilePretrain,
'TrackPeriod': args.TrackPeriod,
'MaxEpoch': args.MaxEpoch,
'SizeBatch': args.SizeBatch,
'NumSel': args.NumSel,
'Optimizer': args.Optimizer,
'PathJVM': args.PathJVM,
'PathJAR': args.PathJAR,
'MaxDiff': args.MaxDiff,
'Coef': args.Coef
}
#
input_train = {
'seed_random': args.Seed,
'path_rawdata': args.FileData,
'path_pre_train': args.FilePretrain,
'track_period': args.TrackPeriod,
'max_epoch': args.MaxEpoch,
'size_batch': args.SizeBatch,
'dim_model': args.DimModel,
'optimizer': args.Optimizer,
'save_file_path': path_save,
'log_file': file_log,
'num_sel': args.NumSel,
'args': dict_args,
'path_jvm': args.PathJVM,
'path_jar': args.PathJAR,
'max_diff': args.MaxDiff,
'coef': args.Coef
}
#
if args.Model == 'selgen':
#run_models.train_selgen(input_train)
run_models.train_selgen_eval_angeli(input_train)
else:
print "Model not cleaned up yet ... "
#
#
if __name__ == "__main__": main()