-
Notifications
You must be signed in to change notification settings - Fork 13
/
Experiment.lua
137 lines (122 loc) · 4 KB
/
Experiment.lua
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
local Xp = torch.class("hypero.Experiment")
function Xp:__init(conn, hexId)
self.conn = conn
assert(torch.isTypeOf(conn, "hypero.Connect"))
if torch.isTypeOf(hexId, "hypero.Battery") then
local bat = hexId
local batId, verId = bat.id, bat:version()
assert(torch.type(batId) == 'number' or torch.type(batId) == 'string')
assert(torch.type(verId) == 'number' or torch.type(verId) == 'string')
assert(pcall(function() return tonumber(batId) and tonumber(verId) end))
-- get a new experiment id
local err
local row, err = self.conn:fetchOne([[
INSERT INTO %s.experiment (bat_id, ver_id)
VALUES (%s, %s) RETURNING hex_id
]], {self.conn.schema, batId, verId})
if not row then
error("Experiment error :\n"..err)
end
self.id = tonumber(row[1])
else
assert(torch.type(hexId) == 'number' or torch.type(hexId) == 'string')
self.id = tonumber(hexId)
local row, err = self.conn:fetchOne([[
SELECT * FROM %s.experiment WHERE hex_id = %s
]], {self.conn.schema, hexId})
assert(row, "Non existent experiment id : "..hexId)
end
end
-- hyper-param get/set
function Xp:setParam(hp, update)
assert(torch.type(hp) == 'table')
-- set
local jsonVal = json.encode.encode(hp)
local cur, err = self.conn:execute([[
INSERT INTO %s.param (hex_id, hex_param) VALUES (%s, '%s')
]], {self.conn.schema, self.id, jsonVal})
if update and not cur then
-- handle insert conflict
local cur, err = self.conn:execute([[
UPDATE %s.param SET hex_param = '%s' WHERE hex_id = %s
]], {self.conn.schema, jsonVal, self.id})
if not cur then
error("Experiment:setParam UPDATE err :\n"..err)
end
elseif not cur then
error("Experiment:setParam INSERT err :\n"..err)
end
return value
end
function Xp:getParam()
-- get
local row = self.conn:fetchOne([[
SELECT hex_param FROM %s.param WHERE hex_id = %s
]], {self.conn.schema, self.id})
if row then
return json.decode.decode(row[1])
else
return nil, err
end
end
-- meta-data get/set
-- Unlike hyper-params, metadata should not influence the results of the experiment.
function Xp:setMeta(md, update)
assert(torch.type(md) == 'table')
-- set
local jsonVal = json.encode.encode(md)
local cur, err = self.conn:execute([[
INSERT INTO %s.meta (hex_id, hex_meta) VALUES (%s, '%s')
]], {self.conn.schema, self.id, jsonVal})
if update and not cur then
-- handle insert conflict
local cur, err = self.conn:execute([[
UPDATE %s.meta SET hex_meta = '%s' WHERE hex_id = %s
]], {self.conn.schema, jsonVal, self.id})
if not cur then
error("Experiment:setMeta UPDATE err :\n"..err)
end
elseif not cur then
error("Experiment:setMeta INSERT err :\n"..err)
end
end
function Xp:getMeta()
-- get
local row, err = self.conn:fetchOne([[
SELECT hex_meta FROM %s.meta WHERE hex_id = %s
]], {self.conn.schema, self.id})
if row then
return json.decode.decode(row[1])
else
return nil, err
end
end
function Xp:setResult(res, update)
assert(torch.type(res) == 'table')
-- set
local jsonVal = json.encode.encode(res)
local cur, err = self.conn:execute([[
INSERT INTO %s.result (hex_id, hex_result) VALUES (%s, '%s')
]], {self.conn.schema, self.id, jsonVal})
if update and not cur then
-- handle insert conflict
local cur, err = self.conn:execute([[
UPDATE %s.result SET hex_result = '%s' WHERE hex_id = %s
]], {self.conn.schema, jsonVal, self.id})
if not cur then
error("Experiment:setResult UPDATE err :\n"..err)
end
elseif not cur then
error("Experiment:setResult INSERT err :\n"..err)
end
end
function Xp:getResult()
local row, err = self.conn:fetchOne([[
SELECT hex_result FROM %s.result WHERE hex_id = %s
]], {self.conn.schema, self.id})
if row then
return json.decode.decode(row[1])
else
return nil, err
end
end