Tensorflow - 为什么 tf.nn 和 tf.layers/tf.losses/tf.contrib.layers 等中有这么多相似甚至重复的函数? [英] Tensorflow - Why are there so many similar or even duplicate functions in tf.nn and tf.layers / tf.losses / tf.contrib.layers etc?

查看:34
本文介绍了Tensorflow - 为什么 tf.nn 和 tf.layers/tf.losses/tf.contrib.layers 等中有这么多相似甚至重复的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Tensorflow(从 v1.2.1 开始)中,似乎(至少)有两个并行 API 来构建计算图.tf.nn 中有函数,如 conv2d、avg_pool、relu、dropout,然后在 tf.layers、tf.losses 和其他地方也有类似的函数,如 tf.layers.conv2d、tf.layers.dense、tf.layers.辍学.

In Tensorflow (as of v1.2.1), it seems that there are (at least) two parallel APIs to construct computational graphs. There are functions in tf.nn, like conv2d, avg_pool, relu, dropout and then there are similar functions in tf.layers, tf.losses and elsewhere, like tf.layers.conv2d, tf.layers.dense, tf.layers.dropout.

从表面上看,这种情况似乎只是为了混淆:例如,tf.nn.dropout 使用保持率",而 tf.layers.dropout 使用丢弃率"作为参数.

Superficially, it seems that this situation only serves to confuse: for example, tf.nn.dropout uses a 'keep rate' while tf.layers.dropout uses a 'drop rate' as an argument.

这种区别对最终用户/开发人员有实际意义吗?如果没有,是否有清理 API 的计划?

Does this distinction have any practical purpose for the end-user / developer? If not, is there any plan to cleanup the API?

推荐答案

Tensorflow 一方面提出了一个低级 API (tf., tf.nn....),另一方面,更高级别的 API(tf.layers.tf.losses.、...).

Tensorflow proposes on the one hand a low level API (tf., tf.nn....), and on the other hand, a higher level API (tf.layers., tf.losses.,...).

更高级别 API 的目标是提供可大大简化最常见神经网络设计的功能.较低级别的 API 适用于有特殊需求或希望更好地控制正在发生的事情的人.

The goal of the higher level API is to provide functions that greatly simplify the design of the most common neural nets. The lower level API is there for people with special needs, or who wishes to keep a finer control of what is going on.

情况有点混乱,因为有些函数名称相同或相似,而且乍一看也没有明确的方法来区分哪个命名空间对应哪个 API 级别.

The situation is a bit confused though, because some functions have the same or similar names, and also, there is no clear way to distinguish at first sight which namespace correspond to which level of the API.

现在,让我们以 conv2d 为例.tf.nn.conv2dtf.layers.conv2d 之间的显着区别在于后者负责权重和偏差所需的所有变量.一行代码,,您刚刚创建了一个卷积层.使用 tf.nn.conv2d,您必须在将权重变量传递给函数之前自行声明权重变量.至于偏差,嗯,实际上甚至没有处理:您需要稍后自己添加.

Now, let's look at conv2d for example. A striking difference between tf.nn.conv2d and tf.layers.conv2d is that the later takes care of all the variables needed for weights and biases. A single line of code, and voilà, you just created a convolutional layer. With tf.nn.conv2d, you have to take declare the weights variable yourself before passing it to the function. And as for the biases, well, they are actually not even handled: you need to add them yourself later.

除此之外,tf.layers.conv2d 还建议在同一个函数调用中添加正则化和激活,你可以想象当一个人的需求被更高层覆盖时,这将如何减少代码大小——级 API.

Add to that that tf.layers.conv2d also proposes to add regularization and activation in the same function call, you can imagine how this can reduce code size when one's need is covered by the higher-level API.

默认情况下,更高级别也会做出一些可以被视为最佳实践的决定.例如,tf.losses 中的损失默认添加到 tf.GraphKeys.LOSSES 集合中,这使得各种组件的恢复和求和变得容易且有些标准化.如果您使用较低级别的 API,则需要自己完成所有这些操作.显然,当您开始在那里混合低级和高级 API 函数时需要小心.

The higher level also makes some decisions by default that could be considered as best practices. For example, losses in tf.losses are added to the tf.GraphKeys.LOSSES collection by default, which makes recovery and summation of the various component easy and somewhat standardized. If you use the lower level API, you would need to do all of that yourself. Obviously, you would need to be careful when you start mixing low and high level API functions there.

更高级别的 API 也是对那些已经习惯于其他框架(Theano)中类似高级功能的人们的巨大需求的回应.当人们考虑构建在 tensorflow 之上的替代更高级别 API 的数量时,这一点相当明显,例如 keras 2(现在 官方 tensorflow API 的一部分)、slim(在 tf.contrib.slim 中)、tflearn、tensorlayer 等.

The higher-level API is also an answer to a great need from people that have been otherwise used to similarly high-level function in other frameworks, Theano aside. This is rather obvious when one ponders the number of alternative higher level APIs built on top of tensorflow, such as keras 2 (now part of the official tensorflow API), slim (in tf.contrib.slim), tflearn, tensorlayer, and the likes.

最后,如果我可以添加一个建议:如果您从 tensorflow 开始并且对特定 API 没有偏好,我个人会鼓励您坚持使用 tf.keras.*API:

Finally, if I may add an advice: if you are beginning with tensorflow and do not have a preference towards a particular API, I would personnally encourage you to stick to the tf.keras.* API:

  • 它的 API 很友好,至少与构建在低级 tensorflow API 之上的其他高级 API 一样好
  • 它在 tensorflow 中有一个清晰的命名空间(尽管它可以——有时应该——与来自其他命名空间的部分一起使用,例如 tf.data)
  • 它现在是 tensorflow 的一等公民(它曾经在 tf.contrib.keras 中),并且小心翼翼地制作新的 tensorflow 功能(例如 eager) 与 keras 兼容.
  • 它的通用实现可以使用其他工具包,例如 CNTK,因此不会将您锁定在 tensorflow.
  • Its API is friendly and at least as good as the other high-level APIs built on top of the low-level tensorflow API
  • It has a clear namespace within tensorflow (although it can -- and sometimes should -- be used with parts from other namespaces, such as tf.data)
  • It is now a first-class citizen of tensorflow (it used to be in tf.contrib.keras), and care is taken to make new tensorflow features (such as eager) compatible with keras.
  • Its generic implementation can use other toolkits such as CNTK, and so does not lock you to tensorflow.

这篇关于Tensorflow - 为什么 tf.nn 和 tf.layers/tf.losses/tf.contrib.layers 等中有这么多相似甚至重复的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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