For a tensorflow public function, is there a way to link the script that it is in and the function name?

Hi, new Tensorflow user here. I am studying Tensorflow source code and have a very specific question. For every public function, I want to get the source code equivalent of it. I tried using this but in the source code, public functions are named differently compared to the API symbols.

This is in the python file tensor_util. tensorflow/ at master · tensorflow/tensorflow · GitHub

Is it possible to form the connection between source code and API symbols? For example, with the picture above, I want to connect tensor_util.constant_value to tf.get_static_value and retrieve all the source code equivalent of the public API symbols.

You can try using some of the helpers in

from tensorflow.python.util.tf_export import get_symbol_from_name



<function tensorflow.python.framework.tensor_util.constant_value>

You can then use the functions __code__ property to extract whatever information you’re looking for. Here’s a function that prints out an API function’s name, module name, file name, and definition line number:

from tensorflow.python.util.tf_export import get_symbol_from_name

def print_api_info(api_name):
  func = get_symbol_from_name(api_name)
  if func is not None:
    module = func.__module__
    code_object = func.__code__
    fname = code_object.co_filename
    line_no = code_object.co_firstlineno
    name = code_object.co_name
    print(f'Name: {name}\nModule: {module}\nFile: {fname}\nLineNo: {line_no}\n')

Trying with your example:



Name: constant_value
Module: tensorflow.python.framework.tensor_util
File: /usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/
LineNo: 809

EDIT: api_name should contain the full namespace within TensorFlow. e.g. for tf.autograph.set_verbosity, you would do:


I’m not certain if this will be useful for every function in the API, but it might be a start in navigating around the source.