Python tensorflow.python.ops.math_ops._as_indexed_slices_list() Examples

The following are 6 code examples of tensorflow.python.ops.math_ops._as_indexed_slices_list(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tensorflow.python.ops.math_ops , or try the search function .
Example #1
Source File: variable_mgr_util.py    From benchmarks with Apache License 2.0 5 votes vote down vote up
def aggregate_indexed_slices_gradients(grads):
  """Aggregates gradients containing `IndexedSlices`s."""
  if len(grads) < 1:
    return None
  elif len(grads) == 1:
    return grads[0]
  else:
    grads = [g for g in grads if g is not None]
    # If any gradient is a `Tensor`, sum them up and return a dense tensor
    # object.
    if any(isinstance(g, ops.Tensor) for g in grads):
      return math_ops.add_n(grads)

    # The following `_as_indexed_slices_list` casts ids of IndexedSlices into
    # int64. It is to make sure the inputs of `concat` all have same the data
    # type.
    grads = math_ops._as_indexed_slices_list(grads)  # pylint: disable=protected-access

    grads = [flatten_nested_indexed_slices(x) for x in grads]
    # Form IndexedSlices out of the concatenated values and indices.
    concat_grad = ops.IndexedSlices(
        array_ops.concat([x.values for x in grads], axis=0),
        array_ops.concat([x.indices for x in grads], axis=0),
        grads[0].dense_shape)

    return concat_grad 
Example #2
Source File: control_flow_ops.py    From lambda-packs with MIT License 4 votes vote down vote up
def merge(inputs, name=None):
  """Returns the value of an available element of `inputs`.

  This op tests each of the tensors in `inputs` in turn to determine if any of
  them is available. If it finds an available tensor, it returns it and its
  index in `inputs`.

  It is an error if more than one tensor in `inputs` is available. If no tensor
  in `inputs` is available, the returned tensor and index are not set.

  This op handles both `Tensor`s and `IndexedSlices`. If inputs has a mix of
  `Tensor`s and `IndexedSlices`, all inputs are converted to IndexedSlices
  before merging.

  Args:
    inputs: The input tensors, at most one of which is available.
    name: A name for this operation (optional).

  Returns:
    A tuple containing the chosen input tensor and its index in `inputs`.

  Raises:
    ValueError: If any of the inputs is None, or inputs are IndexedSlices and
      some but not all have a dense_shape property.
  """
  if any([inp is None for inp in inputs]):
    raise ValueError("At least one of the merge inputs is None: %s" % inputs)
  with ops.name_scope(name, "Merge", inputs) as name:
    inputs = [ops.internal_convert_to_tensor_or_indexed_slices(inp, as_ref=True)
              for inp in inputs]
    if all([isinstance(v, ops.Tensor) for v in inputs]):
      if all([v.dtype._is_ref_dtype for v in inputs]):  # pylint: disable=protected-access
        return gen_control_flow_ops._ref_merge(inputs, name)
      else:
        return gen_control_flow_ops._merge(inputs, name)
    elif all([isinstance(v, sparse_tensor.SparseTensor) for v in inputs]):
      # Only handle the case when all inputs are SparseTensor.
      values, _ = merge([inp.values for inp in inputs], name=name)
      indices, chosen_index = gen_control_flow_ops._merge(
          [inp.indices for inp in inputs], name="indices")
      dense_shape, _ = gen_control_flow_ops._merge(
          [inp.dense_shape for inp in inputs], name="dense_shape")
      return (sparse_tensor.SparseTensor(indices, values, dense_shape),
              chosen_index)
    else:
      # For now convert all the inputs as IndexedSlices.
      inputs = math_ops._as_indexed_slices_list(inputs, optimize=False)
      values, _ = merge([inp.values for inp in inputs], name=name)
      indices, chosen_index = gen_control_flow_ops._merge(
          [inp.indices for inp in inputs], name="indices")
      if any(inp.dense_shape is not None for inp in inputs):
        if any(inp.dense_shape is None for inp in inputs):
          raise ValueError("Either all merged IndexedSlices must have a "
                           "dense_shape, or none must have a dense_shape.")
        dense_shape, _ = gen_control_flow_ops._merge(
            [inp.dense_shape for inp in inputs], name="dense_shape")
      else:
        dense_shape = None
      return ops.IndexedSlices(values, indices, dense_shape), chosen_index
# pylint: enable=protected-access 
Example #3
Source File: control_flow_ops.py    From auto-alt-text-lambda-api with MIT License 4 votes vote down vote up
def merge(inputs, name=None):
  """Returns the value of an available element of `inputs`.

  This op tests each of the tensors in `inputs` in turn to determine if any of
  them is available. If it finds an available tensor, it returns it and its
  index in `inputs`.

  It is an error if more than one tensor in `inputs` is available. If no tensor
  in `inputs` is available, the returned tensor and index are not set.

  This op handles both `Tensor`s and `IndexedSlices`. If inputs has a mix of
  `Tensor`s and `IndexedSlices`, all inputs are converted to IndexedSlices
  before merging.

  Args:
    inputs: The input tensors, at most one of which is available.
    name: A name for this operation (optional).

  Returns:
    A tuple containing the chosen input tensor and its index in `inputs`.

  Raises:
    ValueError: If any of the inputs is None, or inputs are IndexedSlices and
      some but not all have a dense_shape property.
  """
  if any([inp is None for inp in inputs]):
    raise ValueError("At least one of the merge inputs is None: %s" % inputs)
  with ops.name_scope(name, "Merge", inputs) as name:
    inputs = [ops.internal_convert_to_tensor_or_indexed_slices(inp, as_ref=True)
              for inp in inputs]
    if all([isinstance(v, ops.Tensor) for v in inputs]):
      if all([v.dtype._is_ref_dtype for v in inputs]):  # pylint: disable=protected-access
        return gen_control_flow_ops._ref_merge(inputs, name)
      else:
        return gen_control_flow_ops._merge(inputs, name)
    elif all([isinstance(v, sparse_tensor.SparseTensor) for v in inputs]):
      # Only handle the case when all inputs are SparseTensor.
      values, _ = merge([inp.values for inp in inputs], name=name)
      indices, chosen_index = gen_control_flow_ops._merge(
          [inp.indices for inp in inputs], name="indices")
      dense_shape, _ = gen_control_flow_ops._merge(
          [inp.dense_shape for inp in inputs], name="dense_shape")
      return (sparse_tensor.SparseTensor(indices, values, dense_shape),
              chosen_index)
    else:
      # For now convert all the inputs as IndexedSlices.
      inputs = math_ops._as_indexed_slices_list(inputs, optimize=False)
      values, _ = merge([inp.values for inp in inputs], name=name)
      indices, chosen_index = gen_control_flow_ops._merge(
          [inp.indices for inp in inputs], name="indices")
      if any(inp.dense_shape is not None for inp in inputs):
        if any(inp.dense_shape is None for inp in inputs):
          raise ValueError("Either all merged IndexedSlices must have a "
                           "dense_shape, or none must have a dense_shape.")
        dense_shape, _ = gen_control_flow_ops._merge(
            [inp.dense_shape for inp in inputs], name="dense_shape")
      else:
        dense_shape = None
      return ops.IndexedSlices(values, indices, dense_shape), chosen_index
# pylint: enable=protected-access 
Example #4
Source File: control_flow_ops.py    From deep_image_model with Apache License 2.0 4 votes vote down vote up
def merge(inputs, name=None):
  """Returns the value of an available element of `inputs`.

  This op tests each of the tensors in `inputs` in turn to determine if any of
  them is available. If it finds an available tensor, it returns it and its
  index in `inputs`.

  It is an error if more than one tensor in `inputs` is available. If no tensor
  in `inputs` is available, the returned tensor and index are not set.

  This op handles both `Tensor`s and `IndexedSlices`. If inputs has a mix of
  `Tensor`s and `IndexedSlices`, all inputs are converted to IndexedSlices
  before merging.

  Args:
    inputs: The input tensors, at most one of which is available.
    name: A name for this operation (optional).

  Returns:
    A tuple containing the chosen input tensor and its index in `inputs`.

  Raises:
    ValueError: If any of the inputs is None, or inputs are IndexedSlices and
      some but not all have a dense_shape property.
  """
  if any([inp is None for inp in inputs]):
    raise ValueError("At least one of the merge inputs is None: %s" % inputs)
  with ops.name_scope(name, "Merge", inputs) as name:
    inputs = [ops.convert_to_tensor_or_indexed_slices(inp, as_ref=True)
              for inp in inputs]
    if all([isinstance(v, ops.Tensor) for v in inputs]):
      if all([v.dtype.is_ref_dtype for v in inputs]):
        return gen_control_flow_ops._ref_merge(inputs, name)
      else:
        return gen_control_flow_ops._merge(inputs, name)
    elif all([isinstance(v, sparse_tensor.SparseTensor) for v in inputs]):
      # Only handle the case when all inputs are SparseTensor.
      values, _ = merge([inp.values for inp in inputs], name=name)
      indices, chosen_index = gen_control_flow_ops._merge(
          [inp.indices for inp in inputs], name="indices")
      dense_shape, _ = gen_control_flow_ops._merge(
          [inp.shape for inp in inputs], name="dense_shape")
      return (sparse_tensor.SparseTensor(indices, values, dense_shape),
              chosen_index)
    else:
      # For now convert all the inputs as IndexedSlices.
      inputs = math_ops._as_indexed_slices_list(inputs, optimize=False)
      values, _ = merge([inp.values for inp in inputs], name=name)
      indices, chosen_index = gen_control_flow_ops._merge(
          [inp.indices for inp in inputs], name="indices")
      if any(inp.dense_shape is not None for inp in inputs):
        if any(inp.dense_shape is None for inp in inputs):
          raise ValueError("Either all merged IndexedSlices must have a "
                           "dense_shape, or none must have a dense_shape.")
        dense_shape, _ = gen_control_flow_ops._merge(
            [inp.dense_shape for inp in inputs], name="dense_shape")
      else:
        dense_shape = None
      return ops.IndexedSlices(values, indices, dense_shape), chosen_index
# pylint: enable=protected-access 
Example #5
Source File: control_flow_ops.py    From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License 4 votes vote down vote up
def merge(inputs, name=None):
  """Returns the value of an available element of `inputs`.

  This op tests each of the tensors in `inputs` in turn to determine if any of
  them is available. If it finds an available tensor, it returns it and its
  index in `inputs`.

  It is an error if more than one tensor in `inputs` is available. If no tensor
  in `inputs` is available, the returned tensor and index are not set.

  This op handles both `Tensor`s and `IndexedSlices`. If inputs has a mix of
  `Tensor`s and `IndexedSlices`, all inputs are converted to IndexedSlices
  before merging.

  Args:
    inputs: The input tensors, at most one of which is available.
    name: A name for this operation (optional).

  Returns:
    A tuple containing the chosen input tensor and its index in `inputs`.

  Raises:
    ValueError: If any of the inputs is None, or inputs are IndexedSlices and
      some but not all have a dense_shape property.
  """
  if any([inp is None for inp in inputs]):
    raise ValueError("At least one of the merge inputs is None: %s" % inputs)
  with ops.name_scope(name, "Merge", inputs) as name:
    inputs = [ops.internal_convert_to_tensor_or_indexed_slices(inp, as_ref=True)
              for inp in inputs]
    if all([isinstance(v, ops.Tensor) for v in inputs]):
      if all([v.dtype._is_ref_dtype for v in inputs]):  # pylint: disable=protected-access
        return gen_control_flow_ops._ref_merge(inputs, name)
      else:
        return gen_control_flow_ops._merge(inputs, name)
    elif all([isinstance(v, sparse_tensor.SparseTensor) for v in inputs]):
      # Only handle the case when all inputs are SparseTensor.
      values, _ = merge([inp.values for inp in inputs], name=name)
      indices, chosen_index = gen_control_flow_ops._merge(
          [inp.indices for inp in inputs], name="indices")
      dense_shape, _ = gen_control_flow_ops._merge(
          [inp.dense_shape for inp in inputs], name="dense_shape")
      return (sparse_tensor.SparseTensor(indices, values, dense_shape),
              chosen_index)
    else:
      # For now convert all the inputs as IndexedSlices.
      inputs = math_ops._as_indexed_slices_list(inputs, optimize=False)
      values, _ = merge([inp.values for inp in inputs], name=name)
      indices, chosen_index = gen_control_flow_ops._merge(
          [inp.indices for inp in inputs], name="indices")
      if any(inp.dense_shape is not None for inp in inputs):
        if any(inp.dense_shape is None for inp in inputs):
          raise ValueError("Either all merged IndexedSlices must have a "
                           "dense_shape, or none must have a dense_shape.")
        dense_shape, _ = gen_control_flow_ops._merge(
            [inp.dense_shape for inp in inputs], name="dense_shape")
      else:
        dense_shape = None
      return ops.IndexedSlices(values, indices, dense_shape), chosen_index
# pylint: enable=protected-access 
Example #6
Source File: control_flow_ops.py    From keras-lambda with MIT License 4 votes vote down vote up
def merge(inputs, name=None):
  """Returns the value of an available element of `inputs`.

  This op tests each of the tensors in `inputs` in turn to determine if any of
  them is available. If it finds an available tensor, it returns it and its
  index in `inputs`.

  It is an error if more than one tensor in `inputs` is available. If no tensor
  in `inputs` is available, the returned tensor and index are not set.

  This op handles both `Tensor`s and `IndexedSlices`. If inputs has a mix of
  `Tensor`s and `IndexedSlices`, all inputs are converted to IndexedSlices
  before merging.

  Args:
    inputs: The input tensors, at most one of which is available.
    name: A name for this operation (optional).

  Returns:
    A tuple containing the chosen input tensor and its index in `inputs`.

  Raises:
    ValueError: If any of the inputs is None, or inputs are IndexedSlices and
      some but not all have a dense_shape property.
  """
  if any([inp is None for inp in inputs]):
    raise ValueError("At least one of the merge inputs is None: %s" % inputs)
  with ops.name_scope(name, "Merge", inputs) as name:
    inputs = [ops.internal_convert_to_tensor_or_indexed_slices(inp, as_ref=True)
              for inp in inputs]
    if all([isinstance(v, ops.Tensor) for v in inputs]):
      if all([v.dtype._is_ref_dtype for v in inputs]):  # pylint: disable=protected-access
        return gen_control_flow_ops._ref_merge(inputs, name)
      else:
        return gen_control_flow_ops._merge(inputs, name)
    elif all([isinstance(v, sparse_tensor.SparseTensor) for v in inputs]):
      # Only handle the case when all inputs are SparseTensor.
      values, _ = merge([inp.values for inp in inputs], name=name)
      indices, chosen_index = gen_control_flow_ops._merge(
          [inp.indices for inp in inputs], name="indices")
      dense_shape, _ = gen_control_flow_ops._merge(
          [inp.dense_shape for inp in inputs], name="dense_shape")
      return (sparse_tensor.SparseTensor(indices, values, dense_shape),
              chosen_index)
    else:
      # For now convert all the inputs as IndexedSlices.
      inputs = math_ops._as_indexed_slices_list(inputs, optimize=False)
      values, _ = merge([inp.values for inp in inputs], name=name)
      indices, chosen_index = gen_control_flow_ops._merge(
          [inp.indices for inp in inputs], name="indices")
      if any(inp.dense_shape is not None for inp in inputs):
        if any(inp.dense_shape is None for inp in inputs):
          raise ValueError("Either all merged IndexedSlices must have a "
                           "dense_shape, or none must have a dense_shape.")
        dense_shape, _ = gen_control_flow_ops._merge(
            [inp.dense_shape for inp in inputs], name="dense_shape")
      else:
        dense_shape = None
      return ops.IndexedSlices(values, indices, dense_shape), chosen_index
# pylint: enable=protected-access