-
Notifications
You must be signed in to change notification settings - Fork 1
/
TrainPlotter.lua
63 lines (53 loc) · 1.65 KB
/
TrainPlotter.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
-- From https://github.com/joeyhng/trainplot
require 'torch'
require 'optim'
local json = require 'cjson'
local TrainPlotter = torch.class('TrainPlotter') -- declare a class
function TrainPlotter:__init(path)
--If the path already exists, then concatenate the information
if(file_exists(path)) then
local file = assert(io.open(path, "r"))
local json_str = file:read("*all")
self.figures = json.decode(json_str)
else
-- Otherwise initialize empty
self.figures = {}
end
self.path = path
end
-- Information string displayed in the page
function TrainPlotter:info(s)
self.figures['info_str'] = s
end
function TrainPlotter:add(fig_id, plot_id, iter, data)
if data ~= data then data = -1 end
if data == 1/0 then data = 1e-30 end
if data == -1/0 then data = -1e-30 end
if not fig_id then
fig_id = plot_id
end
-- create figure if not exists
if not self.figures[fig_id] then
self.figures[fig_id] = {}
self.figures[fig_id]['data'] = {}
self.figures[fig_id]['layout'] = {['title']=fig_id}
end
local fig_data = self.figures[fig_id]['data']
local plot -- not initialized ?
for k, v in pairs(fig_data) do -- v is dic?
if v['name'] == plot_id then plot = v end
end
if not plot then
plot = {['name'] = plot_id, ['x'] = {}, ['y'] = {}}
table.insert(fig_data, plot)
end
table.insert(plot['x'], iter)
table.insert(plot['y'], data)
local file = io.open(self.path, 'w')
file:write(json.encode(self.figures))
file:close()
end
function file_exists(name)
local f=io.open(name,"r")
if f~=nil then io.close(f) return true else return false end
end