随机函数是静态的,因为你不想实例化(instantiate)你的模型以访问它,但为什么要将其添加到模型本身呢?因为它通常与模型自定义参数绑定。注意,这个函数必须是纯粹的(pure),这样才能根据需要复杂化。 示例 init 函数是最简单的版本,它会加载现存文件夹或(如果 result_dir为空)使用 init_op随机初始化。 The __init__ 你能在文件夹结构看到初始化脚本(The __init__ ),其和机器学习并没有什么关联。但该脚本是令你的代码对你或其他人更加易读的简单方式。 该脚本通过添加几行代码令任何模型类别都能从命名空间 models 直接可读取:所以你能在代码任一处输入:from models import MyModel,该代码行能导入模型而不用管模型的文件夹路径有多么深。 这里有一个脚本案例来实现这一任务: from models.basic_model import BasicModelfrom agents.other_model import SomeOtherModel__all__ = [ "BasicModel", "SomeOtherModel"]def make_model(config, env): if config['model_name'] in __all__: return globals()[config['model_name']](config, env) else: raise Exception('The model name %s does not exist' % config['model_name'])def get_model_class(config): if config['model_name'] in __all__: return globals()[config['model_name']] else: raise Exception('The model name %s does not exist' % config['model_name'])这并没有多高端,但我发现这一脚本十分有用,所以我把它加到本文中了。 API 外壳(The shell API) 我们有一个全局一致的文件夹架构和一个很好的基础类别来构建我们的模型,一个好的 python 脚本很容易加载我们的类(class),但是设计「shell API」,特别是其默认值是同样重要的。 因为与机器学习研究交互的主要结束点就是你使用任何工具的外壳(shell),程序外壳是你实验的基石。 你想要做的最后一件事就是调整你代码中的硬编码值来迭代这些实验,所以你需要从外壳中直接访问所有的超参数。同样你还需要访问所有其他参数,就像结果索引或 stage (HP search/Training/inferring) 等那样。 同样为了更进一步理解,我们将为你直接展示注释文件(commented file): import os, jsonimport tensorflow as tf# See the __init__ in the models folder# `make_models` is a helper function to load any models you havefrom models import make_models from hpsearch import hyperband, randomsearch# I personally always like to make my paths absolute# to be independent from where the python binary is calleddir = os.path.dirname(os.path.realpath(__file__))# I won't dig into TF interaction with the shell, feel free to explore the documentationflags = tf.app.flags# Hyper-parameters search configurationflags.DEFINE_boolean('fullsearch', False, 'Perform a full search of hyperparameter space ex:(hyperband -> lr search -> hyperband with best lr)')flags.DEFINE_boolean('dry_run', False, 'Perform a dry_run (testing purpose)')flags.DEFINE_integer('nb_process', 4, 'Number of parallel process to perform a HP search')# fixed_params is a trick I use to be able to fix some parameters inside the model random function# For example, one might want to explore different models fixing the learning rate, see the basic_model get_random_config functionflags.DEFINE_string('fixed_params', "{}", 'JSON inputs to fix some params in a HP search, ex: '{"lr": 0.001}'')# Agent configurationflags.DEFINE_string('model_name', 'DQNAgent', 'Unique name of the model')flags.DEFINE_boolean('best', False, 'Force to use the best known configuration')flags.DEFINE_float('initial_mean', 0., 'Initial mean for NN')flags.DEFINE_float('initial_stddev', 1e-2, 'Initial standard deviation for NN')flags.DEFINE_float('lr', 1e-3, 'The learning rate of SGD')flags.DEFINE_float('nb_units', 20, 'Number of hidden units in Deep learning agents')# Environment configurationflags.DEFINE_boolean('debug', False, 'Debug mode')flags.DEFINE_integer('max_iter', 2000, 'Number of training step')flags.DEFINE_boolean('infer', False, 'Load an agent for playing')# This is very important for TensorBoard# each model will end up in its own unique folder using time module# Obviously one can also choose to name the output folderflags.DEFINE_string('result_dir', dir + '/results/' + flags.FLAGS.model_name + '/' + str(int(time.time())), 'Name of the directory to store/log the model (if it exists, the model will be loaded from it)')# Another important point, you must provide an access to the random seed# to be able to fully reproduce an experimentflags.DEFINE_integer('random_seed', random.randint(0, sys.maxsize), 'Value of random seed')def main(_): config = flags.FLAGS.__flags.copy() # fixed_params must be a string to be passed in the shell, let's use JSON config["fixed_params"] = json.loads(config["fixed_params"]) if config['fullsearch']: # Some code for HP search ... else: model = make_model(config) if config['infer']: # Some code for inference ... else: # Some code for training ...if __name__ == '__main__': tf.app.run()以上就是本文想要描述的,我们希望它能帮助新入门者辅助研究,我们同样也欢迎自由评论或提问。 在文章最后,作者还列出了一批有关 TensorFlow 文章,感兴趣的读者可通过英文原文查看。 原文链接:https://blog.metaflow.fr/tensorflow-a-proposal-of-good-practices-for-files-folders-and-models-architecture-f23171501ae3
本文为机器之心编译,转载请联系本公众号获得授权。 ?------------------------------------------------ 加入机器之心(全职记者/实习生):[email protected] 投稿或寻求报道:[email protected] 广告&商务合作:[email protected] (责任编辑:本港台直播) |