-
Notifications
You must be signed in to change notification settings - Fork 5
/
logging_example.py
92 lines (71 loc) · 2.11 KB
/
logging_example.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
import sys
import os
from cox.store import Store
from cox.readers import CollectionReader
import shutil
import numpy as np
import itertools
## Code sample to go alongside Walkthrough #1 in README.md
OUT_DIR = '/tmp/cox_example/'
try:
shutil.rmtree(OUT_DIR)
except:
pass
os.mkdir(OUT_DIR)
def f(x):
return (x - 2.03)**2 + 3
if __name__ == "__main__":
# Three parameters: initial guess for x, step size, tolerance
combos = itertools.product(np.linspace(-15, 15, 3), np.linspace(1, 1e-5, 3),
[1e-5])
for x, step, tol in combos:
store = Store(OUT_DIR)
store.add_table('metadata', {
'step_size': float,
'tolerance': float,
'initial_x': float,
'out_dir': str
})
store.add_table('result', {
'final_x': float,
'final_opt': float
})
store.add_table('running_log', {
'current_x': float,
'current_f': float
})
store['metadata'].append_row({
'step_size': step,
'tolerance': tol,
'initial_x': x,
'out_dir': '/tmp/'
})
for _ in range(1000):
# Log current x and f(x)
store['running_log'].append_row({
'current_x': x,
'current_f': f(x)
})
# Take a uniform step in the direction of decrease
if f(x + step) < f(x - step):
x += step
else:
x -= step
# If the difference between the directions
# is less than the tolerance, stop
if f(x + step) - f(x - step) < tol:
break
store['result'].update_row({
'final_x': x
})
store['result'].update_row({
'final_opt':f(x)
})
store['result'].flush_row()
print("Done", x, f(x))
store.close()
### Collection reading
reader = CollectionReader(OUT_DIR)
print(reader.df('result'))
### Running log collection as well
running_log = reader.df('running_log')