TensorFlow如何生成gen_数组_操作pyvia阵列_ops.cc?

2024-05-20 23:17:40 发布

您现在位置:Python中文网/ 问答频道 /正文

TensorFlow自动生成代码。我很好奇TF是如何通过^{}生成{}?在

生成的python文件位于python3.6/site-packages/tensorflow/python/ops/gen_array_ops.py

"""Python wrappers around TensorFlow ops.

This file is MACHINE GENERATED! Do not edit.
Original C++ source file: array_ops.cc
"""
...
...

Tags: 文件代码pypackagestftensorflowsitethis
1条回答
网友
1楼 · 发布于 2024-05-20 23:17:40

Python代码生成是在构建时通过Bazel完成的。{我可以在这里找到相关的^标题:

# Generates a Python library target wrapping the ops registered in "deps".
#
# Args:
#   name: used as the name of the generated target and as a name component of
#     the intermediate files.
#   out: name of the python file created by this rule. If None, then
#     "ops/gen_{name}.py" is used.
#   hidden: Optional list of ops names to make private in the Python module.
#     It is invalid to specify both "hidden" and "op_whitelist".
#   visibility: passed to py_library.
#   deps: list of dependencies for the intermediate tool used to generate the
#     python target. NOTE these `deps` are not applied to the final python
#     library target itself.
#   require_shape_functions: leave this as False.
#   hidden_file: optional file that contains a list of op names to make private
#     in the generated Python module. Each op name should be on a line by
#     itself. Lines that start with characters that are invalid op name
#     starting characters are treated as comments and ignored.
#   generated_target_name: name of the generated target (overrides the
#     "name" arg)
#   op_whitelist: if not empty, only op names in this list will be wrapped. It
#     is invalid to specify both "hidden" and "op_whitelist".
#   cc_linkopts: Optional linkopts to be added to tf_cc_binary that contains the
#     specified ops.

def tf_gen_op_wrapper_py(
        name,
        out = None,
        hidden = None,
        visibility = None,
        deps = [],
        require_shape_functions = False,
        hidden_file = None,
        generated_target_name = None,
        op_whitelist = [],
        cc_linkopts = [],
        api_def_srcs = []):
    # ...

这是通过tf_gen_op_wrapper_private_py间接调用的,您可以在^{}中找到它。对于array_ops的情况,可以在^{}中找到:

^{pr2}$

这条规则有什么作用?它调用一个可以在^{}找到的源程序(这是主入口点,它使用其他相邻的源文件)。本质上,它是一个程序,它通过REGISTER_OP宏(在^{}中定义)注册的操作,并相应地生成Python代码。我现在不能详细介绍,但是如果你想知道细节,你应该可以浏览代码。在

相关问题 更多 >