Path: blob/master/site/en-snapshot/community/contribute/code_style.md
25118 views
TensorFlow code style guide
Python style
Follow the PEP 8 Python style guide, except TensorFlow uses 2 spaces instead of 4. Please conform to the Google Python Style Guide, and use pylint to check your Python changes.
pylint
To install pylint
:
To check a file with pylint
from the TensorFlow source code root directory:
Supported Python versions
For supported Python versions, see the TensorFlow installation guide.
See the TensorFlow continuous build status for official and community supported builds.
C++ coding style
Changes to TensorFlow C++ code should conform to the Google C++ Style Guide and TensorFlow specific style details. Use clang-format
to check your C/C++ changes.
To install on Ubuntu 16+, do:
You can check the format of a C/C++ file with the following:
Other languages
TensorFlow conventions and special uses
Python operations
A TensorFlow operation is a function that, given input tensors returns output tensors (or adds an op to a graph when building graphs).
The first argument should be tensors, followed by basic Python parameters. The last argument is
name
with a default value ofNone
.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
.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 aTensor
object of a specific dtype in the documentation.Each Python operation should have a
name_scope
. As seen below, pass the name of the op as a string.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.
For increased usability, include an example of usage with inputs / outputs of the op in Example section.
Avoid making explicit use of
tf.Tensor.eval
ortf.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()
).
Example:
Usage: