Skip to content

Instantly share code, notes, and snippets.

@4SkyNet
Last active August 31, 2020 00:30
Show Gist options
  • Select an option

  • Save 4SkyNet/6b4280ddd7b127369d523a7350004550 to your computer and use it in GitHub Desktop.

Select an option

Save 4SkyNet/6b4280ddd7b127369d523a7350004550 to your computer and use it in GitHub Desktop.
TensorFlow Code Style

1. Python style

Generally follow PEP8 Python style guide

But! Try to use Tensorflow wherever it useful (or possible...)

2. Tensors

  • Operations that deal with batches may assume that the first dimension of a Tensor is the batch dimension.
# For example, if you want to pass a batch of states as input:
state_size = [64, 48]
input_state = tf.placeholder('float', [None] + state_size)
  • Try to assign appropriate names for your tensors and ops. It could be useful to control the flow and debugging. In addition, it is good practice to use the scopes to divide graph on more consistent parts.

9. Use annotations for your classes and functions

Try to write some annotations to your functional blocks, which should includes:

  • short description (purpose) of the block
  • list of passing arguments starts with Args:
  • list of return values starts with Returns:

For example, let's define a function:

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.op_scope([tensor_in, other_tensor_in], name, "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_collections(output_collections, result)
  return result



10. Python 2 and 3 compatible

  • All code needs to be compatible with Python 2 and 3.

  • Next lines should be present in all Python files:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

Use six and future.utils to write compatible code, for example:

from __future__ import division
from six import moves
from future.utils import raise_with_traceback

for i in moves.xrange(n_iter):
  if i > 10**10/3:
    raise_with_traceback(ValueError("too big value"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment