Torch 日志文件的保存 logroll
怎样将 Torch 在终端显示的信息,保存到 log 文件中 ?
现在介绍一种方法:利用 logroll 的方式。
参考 https://github.com/rosejn/logroll
1. 首先安装 logroll 工具包:
luarocks install logroll
2. 在代码头部:require "logroll" 加载该工具包:
调用的代码如下:
require 'paths'
require 'os'
require 'io'
require 'string' require 'fn'
require 'pprint' logroll = {} local DEFAULT_LEVEL = 'INFO'
local LOG_LEVELS = {'DEBUG', 'INFO', 'WARN', 'ERROR'} for i, label in ipairs(LOG_LEVELS) do
logroll[label] = i
end
logroll.levels = LOG_LEVELS local function default_formatter(level, ...)
local msg = nil if #{...} > then
msg = string.format(({...})[], unpack(fn.rest({...})))
else
msg = pprint.pretty_string(({...})[])
end return string.format("[%s - %s] - %s\n", LOG_LEVELS[level], os.date("%Y_%m_%d_%X"), msg)
end local function default_writer(logger, level, ...)
if level >= logger.level then
logger.file:write(logger.formatter(level, unpack({...})))
end
end local function make_logger(file, options)
local logger = {options = options,
file = file,
formatter = options.formatter or default_formatter,
writer = options.writer or default_writer,
level = logroll[DEFAULT_LEVEL]
} return fn.reduce(function(lg, level)
lg[string.lower(level)] = fn.partial(logger.writer, logger, logroll[level])
return lg
end,
logger, LOG_LEVELS)
end -- A simple logger to print to STDIO.
function logroll.print_logger(options)
local options = options or {}
return make_logger(io.stdout, options)
end -- A logger that prints to a file.
function logroll.file_logger(path, options)
local options = options or {} if options.file_timestamp then
-- append timestamp to create unique log file
path = path .. '-'..os.date("%Y_%m_%d_%X")
end os.execute('mkdir -p "' .. paths.dirname(path) .. '"') return make_logger(io.open(path, 'w'), options)
end -- A logger that combines several other loggers
function logroll.combine(...) local joint = {
subloggers = {...}
} for _,level in ipairs(LOG_LEVELS) do
local fname = string.lower(level)
joint[fname] = function(...)
for _,lg in ipairs(joint.subloggers) do
lg[fname](...)
end
end
end return joint
end
另外,也可以参考这个代码中,保存 log 文件的方式:
https://github.com/szagoruyko/cifar.torch/blob/master/train.lua#L7
1 if testLogger then
2 paths.mkdir(opt.save)
3 testLogger:add{train_acc, confusion.totalValid * 100}
4 testLogger:style{'-','-'}
5 testLogger:plot()
6
7 if paths.filep(opt.save..'/test.log.eps') then
8 local base64im
9 do
10 os.execute(('convert -density 200 %s/test.log.eps %s/test.png'):format(opt.save,opt.save))
11 os.execute(('openssl base64 -in %s/test.png -out %s/test.base64'):format(opt.save,opt.save))
12 local f = io.open(opt.save..'/test.base64')
13 if f then base64im = f:read'*all' end
14 end
15
16 local file = io.open(opt.save..'/report.html','w')
17 file:write(([[
18 <!DOCTYPE html>
19 <html>
20 <body>
21 <title>%s - %s</title>
22 <img src="data:image/png;base64,%s">
23 <h4>optimState:</h4>
24 <table>
25 ]]):format(opt.save,epoch,base64im))
26 for k,v in pairs(optimState) do
27 if torch.type(v) == 'number' then
28 file:write('<tr><td>'..k..'</td><td>'..v..'</td></tr>\n')
29 end
30 end
31 file:write'</table><pre>\n'
32 file:write(tostring(confusion)..'\n')
33 file:write(tostring(model)..'\n')
34 file:write'</pre></body></html>'
35 file:close()
36 end
37 end
其实,最简单有效粗暴的,应该是这种:
1 -- save the information into log files.
2 local logSavePath = './log_Files/'
3 local file = io.open(logSavePath..'train_record.log','a')
4 file:write(tostring("train error: " .. error)..'\n')
5 file:close()
其中,第 4 行的保存记录,也可以是其他地方的变量以及语句,等等。可以随机应变。