在张量流中从gen_nn_ops寻找源代码 [英] looking for source code of from gen_nn_ops in tensorflow

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

问题描述

我对用于深度学习的tensorflow并不陌生,并且对tensorflow中的反卷积(卷积转置)操作感兴趣.我需要看一下操作反卷积的源代码.我猜该函数是nn_ops中的 conv2d_transpose(). 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包装器,因此它基本上由一串1行函数组成.查找此类生成的Python op的基础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类型的目标,该目标已定义

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语法,因此这是相同op的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

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

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