looking for source code of from gen_nn_ops in tensorflow

tensorflow gen_nn_ops conv2d
tf nn conv2d source code
github tensorflow source code
github tensorflow python ops
tensorflow/python ops gen_dataset_ops
tensorflow convolution
tensorflow nhwc
tensorflow/tensorflow/python/ops math_grad 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.

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.

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'

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

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,

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

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

looking for source code of from gen_nn_ops in tensorflow, 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  Pre-trained models and datasets built by Google and the community

Unfortunately, TensorFlow code is not easy to read :(

To make things fast, the python code has to interleave C++ code, which also uses indirect dependencies.

gen_X functions are generated from their C++ code; to find it, you need to search for Conv2dBackpropInput.

You can find the registration of the kernel op in ops/nn_ops.cc and concrete implementation in kernels/conv_grad_input_ops.cc.

tensorflow/nn_ops.py at master · tensorflow/tensorflow · GitHub, import gen_math_ops. from tensorflow.python.ops import gen_nn_ops Note that this code does not implement the same error checks as the. # pre-existing C++ Source: [Understanding and Improving Convolutional Neural Networks via For example, let's look at the height dimension, and the constraints. on the list of  Thank you for submitting a TensorFlow documentation issue. Per our GitHub policy, we only address code/doc bugs, performance issues, feature requests, and build/installation issues on GitHub. The TensorFlow docs are open source! To get i

This is generated file when you build Tensorflow. After you build Tensorflow source, you should see a symbolic link file named "bazel-genfiles" at tensorflow root directory, and go to the location it points, and then you can find it at tensorflow/python/ops/gen_nn_ops.py

Make documentation link to C++ code · Issue #27029 · tensorflow , System information TensorFlow version: All Doc Link: All the Python API /​questions/41147734/looking-for-source-code-of-from-gen-nn-ops-in-tensorflow But then I had to go back to github to search for its C++ source code  Yaroslav has the right answer, but the link he provided is now broken. Here's a correct link to tensorflow.bzl. In my case, I was interested in the internal workings of gen_array_ops._concat . It turns out that the gen*.py are automatically generated wrappers for the C++ kernels located here.

gen_nn_ops - tensorflow - Python documentation, tensorflow.contrib.quantization.gen_nn_ops - Python wrappers around TensorFlow ops. Original C++ source file: nn_ops.cc. and IntelliJ that uses machine learning to provide you with code completions in real time sorted by relevance. python - source - Tensorflow: Where is tf.nn.conv2d Actually Executed? tf.nn.conv2d source code (5) TL;DR: The implementation of tf.nn.conv2d() is written in C++, which invokes optimized code using either Eigen (on CPU) or the cuDNN library (on GPU).

Contribute to the TensorFlow documentation, Some TensorFlow projects keep documentation source files near the code in a separate repository, usually in a docs/ directory. See the project's CONTRIBUTING. Apply tf1-tf2 renames to tensorflow/python/ops docstrings and comments. Move python batch op from contrib to core. #N#batch_ops_test.py. Move python batch op from contrib to core. #N#bitwise_ops.py. Removing @@ comments from core TensorFlow. They are no longer needed … #N#bitwise_ops_test.py. [TF:XLA] Enable some tests that were mistakenly

tensorflow: tensorflow/python/ops/nn_ops.py, Member "tensorflow-2.1.0/tensorflow/python/ops/nn_ops.py" (7 Jan source page into HTML format using (guessed) Python source code 57 local_response_normalization = gen_nn_ops.lrn 58 59 # pylint: Number of top elements to look for along the last 4427 dimension (along each row for matrices). Contribute to tensorflow/tfjs-models development by creating an account on GitHub. You can also take a look at the source code of the demo app. Note.

Comments
  • Hi, when I typed inspect.getsourcefile('gen_nn_ops.conv2d_backprop_input'), I got TypeError: 'gen_nn_ops.conv2d_backprop_input' is not a module, class, method, function, traceback, frame, or code object
  • My tf version is '1.2.1'. How should I go around this?
  • I guess you are missing an import