示例#1
0
    def stringify_and_join(path_elements):
        return separator.join(
            str(path_element) for path_element in path_elements)

    flat_string_paths = [stringify_and_join(path) for path in flat_paths]
    return list(zip(flat_string_paths, flatten(structure)))


def flatten_with_tuple_paths(structure):
    """Returns a list of `(tuple_path, leaf_element)` tuples.

  The order of pairs produced matches that of `nest.flatten`. This allows you
  to flatten a nested structure while keeping information about where in the
  structure each data element was located. See `nest.yield_flat_paths`
  for more information about tuple paths.

  Args:
    structure: the nested structure to flatten.

  Returns:
    A list of `(tuple_path, leaf_element)` tuples. Each `tuple_path` is a tuple
    of indices and/or dictionary keys that uniquely specify the path to
    `leaf_element` within `structure`.
  """
    return list(zip(yield_flat_paths(structure), flatten(structure)))


_pywrap_tensorflow.RegisterType("Mapping", _collections.Mapping)
_pywrap_tensorflow.RegisterType("Sequence", _collections.Sequence)
示例#2
0
        """Returns a list of `Operation`s that consume this `CompositeTensor`.

    Returns:
      A list of `Operation`s.

    Raises:
      RuntimeError: If this method is called while executing eagerly.
    """
        consumers = nest.flatten([
            component.consumers() for component in self._to_components()
            if getattr(component, "graph", None) is not None
        ])
        return list(set(consumers))


pywrap_tensorflow.RegisterType("CompositeTensor", CompositeTensor)


def replace_composites_with_components(structure):
    """Recursively replaces CompositeTensors with their components.

  Args:
    structure: A `nest`-compatible structure, possibly containing composite
      tensors.

  Returns:
    A copy of `structure`, where each composite tensor has been replaced by
    its components.  The result will contain no composite tensors.
    Note that `nest.flatten(replace_composites_with_components(structure))`
    returns the same value as `nest.flatten(structure)`.
  """
示例#3
0
        (allow_subclass and isinstance(value, type_object))):
      return converter_fn(value)

  return None

_TYPE_CONVERSION_FUNCTION_REGISTRY = []


def register_type_spec_from_value_converter(type_object, converter_fn,
                                            allow_subclass=False):
  """Registers a function for converting values with a given type to TypeSpecs.

  If multiple registered `type_object`s match a value, then the most recent
  registration takes precedence.  Custom converters should not be defined for
  `CompositeTensor`s; use `CompositeTensor._type_spec` instead.

  Args:
    type_object: A Python `type` object representing the type of values
      accepted by `converter_fn`.
    converter_fn: A function that takes one argument (an instance of the
      type represented by `type_object`) and returns a `TypeSpec`.
    allow_subclass: If true, then use `isinstance(value, type_object)` to
      check for matches.  If false, then use `type(value) is type_object`.
  """
  _, type_object = tf_decorator.unwrap(type_object)
  _TYPE_CONVERSION_FUNCTION_REGISTRY.append(
      (type_object, converter_fn, allow_subclass))


pywrap_tensorflow.RegisterType("TypeSpec", TypeSpec)
示例#4
0
        rank = tensor_shape.dimension_value(shape[0])
        return [
            tensor_shape.TensorShape([None, rank]),  # indices
            tensor_shape.TensorShape([None]),  # values
            tensor_shape.TensorShape([rank])
        ]  # dense_shape

    @property
    def _is_graph_tensor(self):
        return hasattr(self._values, 'graph')


SparseTensorValue = collections.namedtuple(
    "SparseTensorValue", ["indices", "values", "dense_shape"])
tf_export(v1=["SparseTensorValue"])(SparseTensorValue)
pywrap_tensorflow.RegisterType("SparseTensorValue", SparseTensorValue)


@tf_export(v1=["convert_to_tensor_or_sparse_tensor"])
def convert_to_tensor_or_sparse_tensor(value, dtype=None, name=None):
    """Converts value to a `SparseTensor` or `Tensor`.

  Args:
    value: A `SparseTensor`, `SparseTensorValue`, or an object whose type has a
      registered `Tensor` conversion function.
    dtype: Optional element type for the returned tensor. If missing, the
      type is inferred from the type of `value`.
    name: Optional name to use if a new `Tensor` is created.

  Returns:
    A `SparseTensor` or `Tensor` based on `value`.