forked from h2oai/h2o-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_deeplearning.py
executable file
·245 lines (215 loc) · 8.76 KB
/
gen_deeplearning.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
#!/usr/bin/env python
#
# The purpose of the script is to parse the DeepLearning.java file and emit
# code related to parameters.
#
# Currently pieces of R code get emitted and need to be pasted in manually to the R file.
#
import sys
import os
import shutil
import signal
import time
import random
import getpass
import re
import subprocess
class Blob:
def __init__(self, n, help):
self.n = n
self.help = help
def read_deeplearning_file(deeplearning_file):
"""
Read deep learning file and generate R parameter stub stuff.
@param deeplearning_file: Java source code file
@return: none
"""
try:
nlist = []
in_api = False
help = None
f = open(deeplearning_file, "r")
s = f.readline()
lineno = 0
while (len(s) != 0):
lineno = lineno + 1
stripped = s.strip()
if (len(stripped) == 0):
s = f.readline()
continue
if (stripped.startswith("@API")):
# print("")
if (in_api):
assert(False)
in_api = True
match_groups = re.search("help\s*=\s*\"([^\"]*)\"", stripped)
if (match_groups == None):
print("Missing help")
sys.exit(1)
help = match_groups.group(1)
# print(help)
s = f.readline()
continue
if (in_api):
skip = False
if "checkpoint" in stripped:
skip = True
if "expert_mode" in stripped:
skip = True
# if "activation" in stripped:
# skip = True
# if "initial_weight_distribution" in stripped:
# skip = True
# if "loss" in stripped:
# skip = True
# if "score_validation_sampling" in stripped:
# skip = True
if (skip):
in_api = False
s = f.readline()
continue
match_groups = re.search("public boolean (\S+) = (\S+);", s)
if (match_groups is not None):
t = "boolean"
n = match_groups.group(1)
v = match_groups.group(2)
print(" parms = .addBooleanParm(parms, k=\"{}\", v={})".format(n,n))
nlist.append(Blob(n, help))
# print(t, n, v)
in_api = False
s = f.readline()
continue
match_groups = re.search("public Activation (\S+) = (\S+);", s)
if (match_groups is not None):
t = "string"
n = match_groups.group(1)
v = match_groups.group(2)
print(" parms = .addStringParm(parms, k=\"{}\", v={})".format(n,n))
nlist.append(Blob(n, help))
# print(t, n, v)
in_api = False
s = f.readline()
continue
match_groups = re.search("public int\[\] (\S+) = .*;", s)
if (match_groups is not None):
t = "int array"
n = match_groups.group(1)
print(" parms = .addIntArrayParm(parms, k=\"{}\", v={})".format(n,n))
nlist.append(Blob(n, help))
# print(t, n)
in_api = False
s = f.readline()
continue
match_groups = re.search("public int (\S+) = .*;", s)
if (match_groups is not None):
t = "int"
n = match_groups.group(1)
print(" parms = .addIntParm(parms, k=\"{}\", v={})".format(n,n))
nlist.append(Blob(n, help))
# print(t, n)
in_api = False
s = f.readline()
continue
match_groups = re.search("public double (\S+) = (\S+);", s)
if (match_groups is not None):
t = "double"
n = match_groups.group(1)
v = match_groups.group(2)
print(" parms = .addDoubleParm(parms, k=\"{}\", v={})".format(n,n))
nlist.append(Blob(n, help))
# print(t, n, v)
in_api = False
s = f.readline()
continue
match_groups = re.search("public float (\S+) = (\S+);", s)
if (match_groups is not None):
t = "float"
n = match_groups.group(1)
v = match_groups.group(2)
print(" parms = .addFloatParm(parms, k=\"{}\", v={})".format(n,n))
nlist.append(Blob(n, help))
# print(t, n, v)
in_api = False
s = f.readline()
continue
match_groups = re.search("public double\[\] (\S+);", s)
if (match_groups is not None):
t = "double array"
n = match_groups.group(1)
print(" parms = .addDoubleArrayParm(parms, k=\"{}\", v={})".format(n,n))
nlist.append(Blob(n, help))
# print(t, n)
in_api = False
s = f.readline()
continue
match_groups = re.search("public long (\S+) = new Random.*;", s)
if (match_groups is not None):
t = "long"
n = match_groups.group(1)
v = -1
print(" parms = .addLongParm(parms, k=\"{}\", v={})".format(n,n))
nlist.append(Blob(n, help))
# print(t, n, v)
in_api = False
s = f.readline()
continue
match_groups = re.search("public long (\S+) = (\S+);", s)
if (match_groups is not None):
t = "long"
n = match_groups.group(1)
v = match_groups.group(2)
print(" parms = .addLongParm(parms, k=\"{}\", v={})".format(n,n))
nlist.append(Blob(n, help))
# print(t, n, v)
in_api = False
s = f.readline()
continue
if (stripped == "public InitialWeightDistribution initial_weight_distribution = InitialWeightDistribution.UniformAdaptive;"):
t = "string"
n = "initial_weight_distribution"
print(" parms = .addStringParm(parms, k=\"{}\", v={})".format(n,n))
nlist.append(Blob(n, help))
# print(t, "initial_weight_distribution", "UniformAdaptive")
in_api = False
s = f.readline()
continue
if (stripped == "public Loss loss = Loss.CrossEntropy;"):
t = "string"
n = "loss"
print(" parms = .addStringParm(parms, k=\"{}\", v={})".format(n,n))
nlist.append(Blob(n, help))
# print(t, "loss", "CrossEntropy")
in_api = False
s = f.readline()
continue
if (stripped == "public ClassSamplingMethod score_validation_sampling = ClassSamplingMethod.Uniform;"):
t = "string"
n = "score_validation_sampling"
print(" parms = .addStringParm(parms, k=\"{}\", v={})".format(n,n))
nlist.append(Blob(n, help))
# print(t, "score_validation_sampling", "Uniform")
in_api = False
s = f.readline()
continue
print("ERROR: No match group found on line ", lineno)
sys.exit(1)
s = f.readline()
f.close()
print("")
print("")
for blob in nlist:
print(" {},".format(blob.n))
print("")
print("")
for blob in nlist:
print(" \item{\code{" + blob.n + "}: " + blob.help + "}")
except IOError as e:
print("")
print("ERROR: Failure reading test list: " + deeplearning_file)
print(" (errno {0}): {1}".format(e.errno, e.strerror))
print("")
sys.exit(1)
def main(argv):
read_deeplearning_file("./src/main/java/hex/deeplearning/DeepLearning.java")
if __name__ == "__main__":
main(sys.argv)