在 tensorflow 中寻找来自 gen_nn_ops 的源代码 [英] looking for source code of from gen_nn_ops in tensorflow

查看:43
本文介绍了在 tensorflow 中寻找来自 gen_nn_ops 的源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是用于深度学习的 tensorflow 的新手,并且对 tensorflow 中的反卷积(卷积转置)操作感兴趣.我需要看一下操作反卷积的源代码.函数是我猜 conv2d_transpose() in nn_ops.py.

I am new to tensorflow for deep learning and interested in deconvolution (convolution transpose) operation in tensorflow. I need to take a look at the source code for operating deconvolution. The function is I guess conv2d_transpose() in nn_ops.py.

然而,在该函数中,它调用了另一个名为 gen_nn_ops.conv2d_backprop_input() 的函数.我需要看看这个函数里面有什么,但我无法在存储库中找到它.任何帮助将不胜感激.

However, in the function it calls another function called gen_nn_ops.conv2d_backprop_input(). I need to take a look at what is inside this function, but I am unable to find it in the repository. Any help would be appreciated.

推荐答案

你找不到这个源码,因为源码是bazel自动生成的.如果你从源代码构建,你会在 bazel-genfiles 中看到这个文件.它也存在于您的本地发行版中,您可以使用 inspect 模块找到它.该文件包含自动生成的底层 C++ 实现的 Python 包装器,因此它基本上由一堆单行函数组成.找到此类生成的 Python 操作的底层 C++ 实现的快捷方式是将蛇形大小写为驼色大小写,即 conv2d_backprop_input -> Conv2dBackpropInput

You can't find this source because the source is automatically generated by bazel. If you build from source, you'll see this file inside bazel-genfiles. It's also present in your local distribution which you can find using inspect module. The file contains automatically generated Python wrappers to underlying C++ implementations, so it basically consists of a bunch of 1-line functions. A shortcut to find underlying C++ implementation of such generated Python op is to convert snake case to camel case, ie conv2d_backprop_input -> Conv2dBackpropInput

# figure out where gen_nn_ops is
print(tf.nn.conv2d_transpose.__globals__['gen_nn_ops'])

from tensorflow.python.ops import gen_nn_ops
import inspect
inspect.getsourcefile('gen_nn_ops.conv2d_backprop_input')
'/Users/yaroslav/anaconda/lib/python3.5/site-packages/tensorflow/python/ops/gen_nn_ops.py'

如果您想知道这个文件是如何真正产生的,您可以在 BUILD 文件中跟踪 bazel 依赖项.它找到从 tensorflow 源树生成它的 Bazel 目标:

If you cared to find out how this file really came about, you could follow the trail of bazel dependencies in BUILD files. It to find Bazel target that generated it from tensorflow source tree:

fullname=$(bazel query tensorflow/python/ops/gen_nn_ops.py)
bazel query "attr('srcs', $fullname, ${fullname//:*/}:*)"

//tensorflow/python:nn_ops_gen

所以现在转到 tensorflow/python 中的 BUILD 文件,您会看到这是一个 tf_gen_op_wrapper_private_py 类型的目标,定义为 这里 并从 gen_op_wrapper 调用 gen_op_wrapper>tensorflow/tensorflow.bzl 看起来像这样

So now going to BUILD file inside tensorflow/python you see that this is a target of type tf_gen_op_wrapper_private_py which is defined here and calls gen_op_wrapper_py from tensorflow/tensorflow.bzl which looks like this

def tf_gen_op_wrapper_py(name, out=None, hidden=None, visibility=None, deps=[],
....
      native.cc_binary(
      name = tool_name,

这个 native.cc_binary 构造是一种让 Bazel 目标代表任意命令执行的方法.在这种情况下,它使用一些参数调用 tool_name.通过更多的步骤,您可以发现此处的工具"是从 framework/python_op_gen_main.cc

This native.cc_binary construct is a way to have Bazel target that represents execution of an arbitrary command. In this case it calls tool_name with some arguments. With a couple more steps you can find that "tool" here is compiled from framework/python_op_gen_main.cc

造成这种复杂情况的原因是 TensorFlow 被设计为与语言无关.因此,在理想情况下,您将在 ops 中描述每个操作.pbtxt,然后每个操作将使用 REGISTER_KERNEL_BUILDER 对每种硬件类型有一个实现,因此所有实现都将在 C++/CUDA/Assembly 中完成,并自动可用于所有语言前端.对于每种语言,都会有一个类似python_op_gen_main"的等效翻译器操作,并且所有客户端库代码都将自动生成.但是,由于 Python 如此占主导地位,因此在 Python 方面添加功能存在压力.所以现在有两种操作——在 gen_nn_ops.py 等文件中看到的纯 TensorFlow 操作,以及在 nn_ops.py 等文件中看到的纯 Python 操作,它们通常包装操作自动生成文件 gen_nn_ops.py 但添加额外的功能/语法糖.此外,最初所有名称都是驼峰式的,但决定面向公众的版本应该是 PEP 兼容更常见的 Python 语法,所以这是相同操作的 C++/Python 接口之间驼峰式/蛇式不匹配的原因

The reason for this complication is that TensorFlow was designed to be language agnostic. So in ideal world you would have each op described in ops.pbtxt, and then each op would have one implementation per hardware type using REGISTER_KERNEL_BUILDER, so all implementations would be done in C++/CUDA/Assembly and become automatically available to all language front-ends. There would be an equivalent translator op like "python_op_gen_main" for every language and all client library code would be automatically generated. However, because Python is so dominant, there was pressure to add features on the Python side. So now there are two kinds of ops -- pure TensorFlow ops seen in files like gen_nn_ops.py, and Python-only ops in files like nn_ops.py which typically wrap ops automatically generated files gen_nn_ops.py but add extra features/syntax sugar. Also, originally all names were camel-case, but it was decided that public facing release should be PEP compliant with more common Python syntax, so this is a reason for camel-case/snake-case mismatch between C++/Python interfaces of same op

这篇关于在 tensorflow 中寻找来自 gen_nn_ops 的源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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