使用 Tensorflow.keras 组织项目.应该一个子类 tf.keras.Model 吗? [英] Project organization with Tensorflow.keras. Should one subclass tf.keras.Model?

查看:23
本文介绍了使用 Tensorflow.keras 组织项目.应该一个子类 tf.keras.Model 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Tensorflow 1.14 和 tf.keras API 来构建数量 (>10) 个不同的神经网络.(我也对使用 Tensorflow 2 回答这个问题感兴趣).我想知道我应该如何组织我的项目.

I'm using Tensorflow 1.14 and the tf.keras API to build a number (>10) of differnet neural networks. (I'm also interested in the answers to this question using Tensorflow 2). I'm wondering how I should organize my project.

我使用 tf.keras.estimator.model_to_estimator 和 Tensorboard 将 keras 模型转换为估算器以进行可视化.我有时也使用 model.summary().我的每个模型都有一个(> 20)个超参数,并将三种类型的输入数据之一作为输入.我有时会使用超参数优化,因此在尝试下一组超参数之前,我经常手动删除模型并使用 tf.keras.backend.clear_session().

I convert the keras models into estimators using tf.keras.estimator.model_to_estimator and Tensorboard for visualization. I'm also sometimes using model.summary(). Each of my models has a number (>20) of hyperparameters and takes as input one of three types of input data. I sometimes use hyperparameter optimization, such that I often manually delete models and use tf.keras.backend.clear_session() before trying the next set of hyperparameters.

目前我使用的函数将超参数作为参数并返回相应的编译后的 keras 模型以转换为估算器.我使用三种不同的Main_Datatype.py"脚本来训练三种不同输入数据类型的模型.所有数据都是从 .tfrecord 文件加载的,每种数据类型都有一个输入函数,所有将这种类型的数据作为输入的估算器都使用它.我在主脚本中的模型(即返回模型的函数)之间切换.我还有一些构建块,它们属于多个模型的一部分,为此我使用辅助函数返回它们,并使用 Keras 函数式 API 拼凑出最终结果.

Currently I'm using functions that take hyperparameters as arguments and return the respective compiled keras model to be turned into an estimator. I use three different "Main_Datatype.py" scripts to train models for the three different input data types. All data is loaded from .tfrecord files and there is an input function for each data type, which is used by all estimators taking that type of data as input. I switch between models (i.e. functions returning a model) in the Main scripts. I also have some building blocks that are part of more than one model, for which I use helper functions returning them, piecing together the final result using the Keras functional API.

不同模型的轻微不兼容开始让我感到困惑,我决定使用类来组织项目.我打算为每个模型创建一个类,用于跟踪超参数和每个模型及其模型目录的正确命名.但是,我想知道在 Tensorflow 中是否有既定或推荐的方法来做到这一点.

The slight incompatibilities of the different models are begining to confuse me and I've decided to organise the project using classes. I'm planing to make a class for each model that keeps track of hyperparameters and correct naming of each model and its model directory. However, I'm wondering if there are established or recomended ways to do this in Tensorflow.

问题:我是否应该继承 tf.keras.Model 而不是使用函数来构建模型或封装它们的 Python 类?将 keras.Model 子类化会破坏(或需要大量工作才能启用)我与 keras 估算器和张量板一起使用的任何功能吗?我已经看到人们在使用自定义模型类时遇到的许多问题,并且有点不愿意投入工作却发现它对我不起作用.您对如何更好地组织我的项目还有其他建议吗?

Question: Should I be subclassing tf.keras.Model instead of using functions to build models or python classes that encapsulate them? Would subclassing keras.Model break (or require much work to enable) any of the functionality that I use with keras estimators and tensorboard? I've seen many issues people have with using custom Model classes and am somewhat reluctant to put in the work only to find that it doesn't work for me. Do you have other suggestions how to better organize my project?

非常感谢您.

推荐答案

仅在您绝对需要时才子类化.我个人更喜欢遵循以下实施顺序.如果您正在设计的模型的复杂性无法使用前两个选项实现,那么当然子类化是唯一的选择.

Subclass only if you absolutely need to. I personally prefer following the following order of implementation. If the complexity of the model you are designing, can not be achieved using the first two options, then of course subclassing is the only option left.

  1. tf.keras 顺序 API
  2. tf.keras 函数式 API
  3. 子类 tf.keras.Model

这篇关于使用 Tensorflow.keras 组织项目.应该一个子类 tf.keras.Model 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆