<commit_before># Copyright 2015 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Common util functions used by layers.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from collections import namedtuple
from collections import OrderedDict
from tensorflow.python.framework import ops
from tensorflow.python.framework import tensor_shape
from tensorflow.python.framework import tensor_util
from tensorflow.python.ops import control_flow_ops
from tensorflow.python.ops import variables

__all__ = ['collect_named_outputs',
           'constant_value',
           'static_cond',
           'smart_cond',
           'get_variable_collections',
           'two_element_tuple',
           'last_dimension',
           'first_dimension']

NamedOutputs = namedtuple('NamedOutputs', ['name', 'outputs'])


def collect_named_outputs(collections, alias, outputs):
  """Add `Tensor` outputs tagged with alias to collections.

  It is useful to collect end-points or tags for summaries. Example of usage:

  logits = collect_named_outputs('end_points', 'inception_v3/logits', logits)
  assert logits.alias == 'inception_v3/logits'

  Args:
    collections: A collection or list of collections. If None skip collection.
    alias: String, alias to name the outputs, ex. 'inception_v3/conv1'
    outputs: Tensor, an output tensor to collect

  Returns:
    The outputs Tensor to allow inline call.
  """
  # Remove ending '/' if present.
  if alias[-1] == '/':
    alias = alias[:-1]
  outputs.alias = alias
  if collections:
    ops.add_to_collections(collections, outputs)
  return outputs


def gather_tensors_alias(tensors):
  """Given a list of tensors, gather their aliases.

  If the tensor does not have an alias it would default to its name.

  Args:
    tensors: A list of `Tensors`.

  Returns:
    A list of strings with the alias of each tensor.
  """
  return [get_tensor_alias(tensor) for tensor in tensors]


def get_tensor_alias(tensor):
  """Given a tensor gather its alias, its op.name or its name.

  If the tensor does not have an alias it would default to its name.

  Args:
    tensor: A `Tensor`.

  Returns:
    A string with the alias of the tensor.
  """
  if hasattr(tensor, 'alias'):
    alias = tensor.alias
  else:
    if tensor.name[-2:] == ':0':
      # Use op.name for tensor ending in :0
      alias = tensor.op.name
    else:
      alias = tensor.name
  return alias


def convert_collection_to_dict(collection):
  """Returns a dict of Tensors using get_tensor_alias as key.

  Args:
    collection: A collection.

  Returns:
    A dictionary of {get_tensor_alias(tensor): tensor}
  """
  return OrderedDict((get_tensor_alias(t), t) for t in ops.get_collection(collection))


def constant_value(value_or_tensor_or_var, dtype=None):<commit_msg>updated docstring<commit_after># Copyright 2015 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Common util functions used by layers.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from collections import namedtuple
from collections import OrderedDict
from tensorflow.python.framework import ops
from tensorflow.python.framework import tensor_shape
from tensorflow.python.framework import tensor_util
from tensorflow.python.ops import control_flow_ops
from tensorflow.python.ops import variables

__all__ = ['collect_named_outputs',
           'constant_value',
           'static_cond',
           'smart_cond',
           'get_variable_collections',
           'two_element_tuple',
           'last_dimension',
           'first_dimension']

NamedOutputs = namedtuple('NamedOutputs', ['name', 'outputs'])


def collect_named_outputs(collections, alias, outputs):
  """Add `Tensor` outputs tagged with alias to collections.

  It is useful to collect end-points or tags for summaries. Example of usage:

  logits = collect_named_outputs('end_points', 'inception_v3/logits', logits)
  assert logits.alias == 'inception_v3/logits'

  Args:
    collections: A collection or list of collections. If None skip collection.
    alias: String, alias to name the outputs, ex. 'inception_v3/conv1'
    outputs: Tensor, an output tensor to collect

  Returns:
    The outputs Tensor to allow inline call.
  """
  # Remove ending '/' if present.
  if alias[-1] == '/':
    alias = alias[:-1]
  outputs.alias = alias
  if collections:
    ops.add_to_collections(collections, outputs)
  return outputs


def gather_tensors_alias(tensors):
  """Given a list of tensors, gather their aliases.

  If the tensor does not have an alias it would default to its name.

  Args:
    tensors: A list of `Tensors`.

  Returns:
    A list of strings with the alias of each tensor.
  """
  return [get_tensor_alias(tensor) for tensor in tensors]


def get_tensor_alias(tensor):
  """Given a tensor gather its alias, its op.name or its name.

  If the tensor does not have an alias it would default to its name.

  Args:
    tensor: A `Tensor`.

  Returns:
    A string with the alias of the tensor.
  """
  if hasattr(tensor, 'alias'):
    alias = tensor.alias
  else:
    if tensor.name[-2:] == ':0':
      # Use op.name for tensor ending in :0
      alias = tensor.op.name
    else:
      alias = tensor.name
  return alias


def convert_collection_to_dict(collection):
  """Returns an OrderedDict of Tensors using get_tensor_alias as key.

  Args:
    collection: A collection.

  Returns:
    An OrderedDict of {get_tensor_alias(tensor): tensor}
  """
  return OrderedDict((get_tensor_alias(t), t) for t in ops.get_collection(collection))


def constant_value(value_or_tensor_or_var, dtype=None):
{'fraction_non_alphanumeric': 0.10314937879225658, 'fraction_numerical': 0.004911875180583647, 'mean_word_length': 3.8548387096774195, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4733053', 'n_tokens_mistral': 1953, 'n_tokens_neox': 1884, 'n_words': 742}