aboutsummaryrefslogblamecommitdiff
path: root/nerv/init.lua
blob: 4d7b6872426a83a552d5dff31420b4bf5cf6848d (plain) (tree)
1
2
3
4
5
6
7
8





                                                                            
                 
 


                                                                  
                                            

                                         
 



                                          



                                             



                                  


                                            
                              






                                                       

   


                                                     

                                                                      

   




                                                                            
                            
                

                                              

   




                                                                            


                                                 
                                              

   




                                                                    























                                                                              





















                                                                    




                                                                              













                                                          
 


                                     



                                                   


                                                









                                        

   


                                               








                                                             




                                                                               

                                                      
                                                   

   
                                                








                                                                              

                                                                                                        





                                                                               
                                                  






                                                      
                           








                                                            


                              



                                                
                                                                            
                                         









                                                                       
                                                                          














                                                              
                                                     











                                                                       
                                                         




                                                   
                                                                                       






                                                                   
                                                            


                                                 
                                                                       












                                                                            
                            




                     

































                                                                             















                                                                      

                                                                  



                               
                            
--- NERV: a Lua-based toolkit for high-performance deep learning.
-- This file contains misc utility functions of NERV and finally initializes
-- NERV by including `init.lua` of other basic modules.
-- @author Ted Yin <ted.sybil@gmail.com>
-- @module nerv

require 'libnerv'

--- Dummy function.
-- Display a friendly error message when user attempts to invoke a
-- non-implemented function.
function nerv.error_method_not_implemented()
    nerv.error("method not implemented");
end

function nerv.set_logfile(filename)
    nerv._logfile = io.open(filename, "w")
end

--- Format a string just like `sprintf` in C.
-- @param fmt the format string
-- @param ... args, the data to be formatted
-- @return the formatted string
function nerv.sprintf(fmt, ...)
    return string.format(fmt, ...)
end

--- Print a formatted string to stdout.
-- @param fmt the format string
-- @param ... args, the data to be formatted
function nerv.printf(fmt, ...)
    local line = nerv.sprintf(fmt, ...)
    io.stderr:write(line)
    -- duplicate the all output to the log file, if set
    if nerv._logfile then
        nerv._logfile:write(line)
        nerv._logfile:flush()
    end
end

--- Raise an global error with the formatted message.
-- @param fmt the format string
-- @param ... args, the data to be formatted
function nerv.error(fmt, ...)
    error(nerv.sprintf("[nerv] internal error: " .. fmt .. "\n", ...))
end

--- Print a notification message that begins with "info" and a timestamp.
-- Instead of using `nerv.printf`, normal users should use this to print any
-- notification information.
-- @param fmt the format string
-- @param ... args, the data to be formatted
function nerv.info(fmt, ...)
    nerv.printf(
        string.format("(%s)[nerv] info: %s\n",
            os.date("%H:%M:%S %F"), fmt), ...)
end

--- Print a warning message that begins with "warning" and a timestamp.
-- Instead of using `nerv.printf`, normal users should use this to print any
-- warnings.
-- @param fmt the format string
-- @param ... args, the data to be formatted
function nerv.warning(fmt, ...)
    nerv.printf(
        string.format("(%s)[nerv] warning: %s\n",
            os.date("%H:%M:%S %F"), fmt), ...)
end

--- Create a class (Torch-compatible).
-- Use this to create a class in NERV.
-- @param tname the class name
-- @param parenttname the parent class name (from which it inherits)
-- @return the created class
function nerv.class(tname, parenttname)

   local function constructor(...)
      local self = {}
      nerv.setmetatable(self, tname)
      if self.__init then
         self:__init(...)
      end
      return self
   end

   local function factory()
      local self = {}
      nerv.setmetatable(self, tname)
      return self
   end

   local mt = nerv.newmetatable(tname, parenttname, constructor, nil, factory)
   local mpt
   if parenttname then
      mpt = nerv.getmetatable(parenttname)
   end
   return mt, mpt
end

function table.val_to_str(v)
  if "string" == type(v) then
    v = string.gsub(v, "\n", "\\n")
    if string.match(string.gsub(v,"[^'\"]",""), '^"+$') then
      return "'" .. v .. "'"
    end
    return '"' .. string.gsub(v,'"', '\\"') .. '"'
  else
    return "table" == type(