TensorFlow 代码样式指南
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Python 样式
遵循 PEP 8 Python 样式指南,但 TensorFlow 使用 2 个空格而不是 4 个空格。请遵循 Google Python 样式指南,并使用 pylint 检查您的 Python 更改。
pylint
要安装 pylint
,请运行以下代码:
$ pip install pylint
要使用 TensorFlow 源代码根目录中的 pylint
检查文件,请运行以下代码:
$ pylint --rcfile=tensorflow/tools/ci_build/pylintrc tensorflow/python/keras/losses.py
支持的 Python 版本
有关支持的 Python 版本,请参阅 TensorFlow 安装指南。
有关官方和社区支持的构建,请参阅 TensorFlow 持续构建状态。
C++ 编码样式
对 TensorFlow C++ 代码的变更应符合 Google C++ 样式指南和 TensorFlow 特定样式详细信息。使用 clang-format
检查您的 C/C++ 变更。
要在 Ubuntu 16+ 上安装,请运行以下命令:
$ apt-get install -y clang-format
您可以使用以下命令检查 C/C++ 文件的格式:
$ clang-format <my_cc_file> --style=google > /tmp/my_cc_file.cc
$ diff <my_cc_file> /tmp/my_cc_file.cc
其他语言
TensorFlow 惯例和特殊用法
Python 运算
TensorFlow 运算是一种给定输入张量、返回输出张量(或在构建计算图时向计算图添加运算)的函数。
- 第一个参数应当是张量,然后是基本的 Python 参数。最后一个参数是默认值为
None
的 name
。
- 张量参数应当是单个张量或者多个张量的可迭代对象。也就是说,“张量或张量列表”过于宽泛。请参见
assert_proper_iterable
。
- 如果使用张量作为参数的运算正在使用 C++ 运算,则应调用
convert_to_tensor
将非张量输入转换为张量。请注意,参数在文档中仍被描述为特定 dtype 的 Tensor
对象。
- 每个 Python 运算都应具有一个
name_scope
。如下所示,以字符串形式传递运算的名称。
- 运算应包含带参数和返回声明的大量 Python 注释,这些注释说明了每个值的类型和含义。应在说明中指定可能的形状、dtype 或秩。请参阅文档详细信息。
- 为提高可用性,“示例”部分中包括带运算的输入/输出的用法示例。
- 避免显式使用
tf.Tensor.eval
或 tf.Session.run
。例如,要编写依赖于张量值的逻辑,请使用 TensorFlow 控制流。或者,将运算限制为仅在启用 Eager Execution 时 (tf.executing_eagerly()
) 才运行。
示例:
def my_op(tensor_in, other_tensor_in, my_param, other_param=0.5,
output_collections=(), name=None):
"""My operation that adds two tensors with given coefficients.
Args:
tensor_in: `Tensor`, input tensor.
other_tensor_in: `Tensor`, same shape as `tensor_in`, other input tensor.
my_param: `float`, coefficient for `tensor_in`.
other_param: `float`, coefficient for `other_tensor_in`.
output_collections: `tuple` of `string`s, name of the collection to
collect result of this op.
name: `string`, name of the operation.
Returns:
`Tensor` of same shape as `tensor_in`, sum of input values with coefficients.
Example:
>>> my_op([1., 2.], [3., 4.], my_param=0.5, other_param=0.6,
output_collections=['MY_OPS'], name='add_t1t2')
[2.3, 3.4]
"""
with tf.name_scope(name or "my_op"):
tensor_in = tf.convert_to_tensor(tensor_in)
other_tensor_in = tf.convert_to_tensor(other_tensor_in)
result = my_param * tensor_in + other_param * other_tensor_in
tf.add_to_collection(output_collections, result)
return result
用法:
output = my_op(t1, t2, my_param=0.5, other_param=0.6,
output_collections=['MY_OPS'], name='add_t1t2')
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2022-06-07。
[null,null,["最后更新时间 (UTC):2022-06-07。"],[],[],null,["# TensorFlow code style guide\n\nPython style\n------------\n\nFollow the [PEP 8 Python style\nguide](https://www.python.org/dev/peps/pep-0008/), except TensorFlow uses 2\nspaces instead of 4. Please conform to the\n[Google Python Style Guide](https://github.com/google/styleguide/blob/gh-pages/pyguide.md),\nand use [pylint](https://www.pylint.org/) to check your Python changes.\n\n### pylint\n\nTo install `pylint`: \n\n $ pip install pylint\n\nTo check a file with `pylint` from the TensorFlow source code root directory: \n\n $ pylint --rcfile=tensorflow/tools/ci_build/pylintrc tensorflow/python/keras/losses.py\n\n### Supported Python versions\n\nFor supported Python versions, see the TensorFlow\n[installation guide](https://www.tensorflow.org/install).\n\nSee the TensorFlow\n[continuous build status](https://github.com/tensorflow/tensorflow/blob/master/README.md#continuous-build-status)\nfor official and community supported builds.\n\nC++ coding style\n----------------\n\nChanges to TensorFlow C++ code should conform to the [Google C++ Style\nGuide](https://google.github.io/styleguide/cppguide.html) and [TensorFlow specific style details](https://github.com/tensorflow/community/blob/master/governance/cpp-style.md). Use `clang-format` to check your C/C++ changes.\n\nTo install on Ubuntu 16+, do: \n\n $ apt-get install -y clang-format\n\nYou can check the format of a C/C++ file with the following: \n\n $ clang-format \u003cmy_cc_file\u003e --style=google \u003e /tmp/my_cc_file.cc\n $ diff \u003cmy_cc_file\u003e /tmp/my_cc_file.cc\n\nOther languages\n---------------\n\n- [Google Java Style Guide](https://google.github.io/styleguide/javaguide.html)\n- [Google JavaScript Style Guide](https://google.github.io/styleguide/jsguide.html)\n- [Google Shell Style Guide](https://google.github.io/styleguide/shell.xml)\n- [Google Objective-C Style Guide](https://google.github.io/styleguide/objcguide.html)\n\nTensorFlow conventions and special uses\n---------------------------------------\n\n### Python operations\n\nA TensorFlow *operation* is a function that, given input tensors returns output\ntensors (or adds an op to a graph when building graphs).\n\n- The first argument should be tensors, followed by basic Python parameters. The last argument is `name` with a default value of `None`.\n- Tensor arguments should be either a single tensor or an iterable of tensors. That is, a \"Tensor or list of Tensors\" is too broad. See `assert_proper_iterable`.\n- Operations that take tensors as arguments should call `convert_to_tensor` to convert non-tensor inputs into tensors if they are using C++ operations. Note that the arguments are still described as a `Tensor` object of a specific dtype in the documentation.\n- Each Python operation should have a `name_scope`. As seen below, pass the name of the op as a string.\n- Operations should contain an extensive Python comment with Args and Returns declarations that explain both the type and meaning of each value. Possible shapes, dtypes, or ranks should be specified in the description. See documentation details.\n- For increased usability, include an example of usage with inputs / outputs of the op in Example section.\n- Avoid making explicit use of [`tf.Tensor.eval`](https://www.tensorflow.org/api_docs/python/tf/Tensor#eval) or `tf.Session.run`. For example, to write logic that depends on the Tensor value, use the TensorFlow control flow. Alternatively, restrict the operation to only run when eager execution is enabled ([`tf.executing_eagerly()`](https://www.tensorflow.org/api_docs/python/tf/executing_eagerly)).\n\nExample: \n\n def my_op(tensor_in, other_tensor_in, my_param, other_param=0.5,\n output_collections=(), name=None):\n \"\"\"My operation that adds two tensors with given coefficients.\n\n Args:\n tensor_in: `Tensor`, input tensor.\n other_tensor_in: `Tensor`, same shape as `tensor_in`, other input tensor.\n my_param: `float`, coefficient for `tensor_in`.\n other_param: `float`, coefficient for `other_tensor_in`.\n output_collections: `tuple` of `string`s, name of the collection to\n collect result of this op.\n name: `string`, name of the operation.\n\n Returns:\n `Tensor` of same shape as `tensor_in`, sum of input values with coefficients.\n\n Example:\n \u003e\u003e\u003e my_op([1., 2.], [3., 4.], my_param=0.5, other_param=0.6,\n output_collections=['MY_OPS'], name='add_t1t2')\n [2.3, 3.4]\n \"\"\"\n with tf.name_scope(name or \"my_op\"):\n tensor_in = tf.convert_to_tensor(tensor_in)\n other_tensor_in = tf.convert_to_tensor(other_tensor_in)\n result = my_param * tensor_in + other_param * other_tensor_in\n tf.add_to_collection(output_collections, result)\n return result\n\nUsage: \n\n output = my_op(t1, t2, my_param=0.5, other_param=0.6,\n output_collections=['MY_OPS'], name='add_t1t2')"]]