aboutsummaryrefslogblamecommitdiff
path: root/nerv/init.lua
blob: ff944b8e5f30a0d393a877318339471ef97756f0 (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)