site/en/community/contribute/code_style.md
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.
To install pylint:
$ pip install pylint
To check a file with pylint from the TensorFlow source code root directory:
$ pylint --rcfile=tensorflow/tools/ci_build/pylintrc tensorflow/python/keras/losses.py
For supported Python versions, see the TensorFlow installation guide.
See the TensorFlow continuous build status for official and community supported builds.
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:
$ apt-get install -y clang-format
You can check the format of a C/C++ file with the following:
$ clang-format <my_cc_file> --style=google > /tmp/my_cc_file.cc
$ diff <my_cc_file> /tmp/my_cc_file.cc
A TensorFlow operation is a function that, given input tensors returns output tensors (or adds an op to a graph when building graphs).
name with a default value of None.assert_proper_iterable.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.name_scope. As seen below, pass the name
of the op as a string.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()).Example:
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
Usage:
output = my_op(t1, t2, my_param=0.5, other_param=0.6,
output_collections=['MY_OPS'], name='add_t1t2')