-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathto_h.py
More file actions
72 lines (63 loc) · 2.36 KB
/
to_h.py
File metadata and controls
72 lines (63 loc) · 2.36 KB
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
from pathlib import Path
import numpy as np
# CONVERTING numpy arrays to C++ h-file constants.
def make_line(arr, factor=None):
str_format = "{fval:.6f}f" if factor is None else "{int(np.round(fval * factor))}"
str_format = "f'" + str_format + "'"
line = '{'
for i in range(arr.shape[0]):
fval = arr[i]
line += eval(str_format)
if i != arr.shape[0] - 1:
line += ', '
line += '}'
return line
def dump_h1(path,
name,
arr,
arrname,
factor=None):
with open(Path(path) / (name + '.h'), 'w', newline='') as f:
f.write('#pragma once\n\n')
f.write(f'#define {arrname}_LEN {arr.shape[0]}\n\n')
if factor is not None:
f.write(f'#define {arrname}_FACTOR {int(factor)}\n\n')
f.write(f'const int {arrname}[{arrname}_LEN] = ')
else:
f.write(f'const float {arrname}[{arrname}_LEN] = ')
line = make_line(arr, factor)
f.write(line)
f.write(';\n\n')
def dump_h2(path,
name,
arr,
arrname,
factor=None):
with open(Path(path) / (name + '.h'), 'w', newline='') as f:
f.write('#pragma once\n\n')
f.write(f'#define {arrname}_ROWS {arr.shape[0]}\n\n')
f.write(f'#define {arrname}_COLS {arr.shape[1]}\n\n')
if factor is not None:
f.write(f'#define {arrname}_FACTOR {int(factor)}\n\n')
f.write(f'const int {arrname}[{arrname}_ROWS][{arrname}_COLS] = {{\n')
else:
f.write(f'const float {arrname}[{arrname}_ROWS][{arrname}_COLS] = {{\n')
for i in range(arr.shape[0]):
line = make_line(arr[i], factor)
if i != arr.shape[0] - 1:
line += ','
line += '\n'
f.write(line)
f.write('};\n\n')
if __name__ == '__main__':
in_dir = ('/home/uri-zackhem/PycharmProjects/'
'LogNNet-master/simple_model2_results')
file_name = 'LAST__MLP_model.npz'
npz_path = Path(in_dir) / file_name
data = np.load(npz_path)
print(data)
mlp_coefs = data['mlp_coefs']
mlp_intercepts = data['mlp_intercepts']
dump_h2(in_dir, 'mlp_coefs', mlp_coefs, 'MLP_COEFS', factor=10_000)
dump_h1(in_dir, 'mlp_intercepts', mlp_intercepts, 'MLP_INTERCEPTS', factor=10_000)
print('Finished')