def print_model_settings(locals_var, path=None, sys_arg=False):
"""
Prints all variables in upper case in locals_var,
except for T which usually stands for theano.tensor.
If locals() passed as input to this method, will print
all the variables in upper case defined so far, that is
model settings.
With `path` as an address to a directory it will _append_ it
as a file named `model_settings.txt` as well.
With `sys_arg` set to True, log information about Python, Numpy,
and Theano and passed arguments to the script will be added too.
args.pkl would be overwritten, specially in case of resuming a job.
But again that wouldn't be much of a problem as all the passed args
to the script except for '--resume' should be the same.
With both `path` and `sys_arg` passed, dumps the theano.config.
:usage:
>>> import theano.tensor as T
>>> import lib
>>> BATCH_SIZE, DIM = 128, 512
>>> DATA_PATH = '/Path/to/dataset'
>>> lib.print_model_settings(locals(), path='./')
"""
log = ""
if sys_arg:
try:
log += "Python:\n"
log += "\tsys.version_info\t{}\n".format(str(sys.version_info))
log += "Numpy:\n"
log += "\t.__version__\t{}\n".format(numpy.__version__)
log += "Theano:\n"
log += "\t.__version__\t{}\n".format(theano.__version__)
log += "\n\nAll passed args:\n"
log += str(sys.argv)
log += "\n"
except:
print "Something went wrong during sys_arg logging. Continue anyway!"
log += "\nModel settings:"
all_vars = [(k,v) for (k,v) in locals_var.items() if (k.isupper() and k != 'T')]
all_vars = sorted(all_vars, key=lambda x: x[0])
for var_name, var_value in all_vars:
log += ("\n\t%-20s %s" % (var_name, var_value))
print log
if path is not None:
ensure_dir(path)
# Don't override, just append if by mistake there is something in the file.
with open(os.path.join(path, __model_setting_file_name), 'a+') as f:
f.write(log)
if sys_arg:
with open(os.path.join(path, 'th_conf.txt'), 'a+') as f:
f.write(str(theano.config))
with open(os.path.join(path, 'args.pkl'), 'wb') as f:
pickle.dump(sys.argv, f)
# To load:
# >>> import cPickle as pickle
# >>> args = pickle.load(open(os.path.join(path, 'args.pkl'), 'rb'))
评论列表
文章目录