Пример #1
0
def sdca_optimizer_v2(sparse_example_indices, sparse_feature_indices, sparse_feature_values, dense_features, example_weights, example_labels, sparse_indices, sparse_weights, dense_weights, example_state_data, loss_type, l1, l2, num_loss_partitions, num_inner_iterations, adaptive=True, name=None):
  r"""Distributed version of Stochastic Dual Coordinate Ascent (SDCA) optimizer for

  linear models with L1 + L2 regularization. As global optimization objective is
  strongly-convex, the optimizer optimizes the dual objective at each step. The
  optimizer applies each update one example at a time. Examples are sampled
  uniformly, and the optimizer is learning rate free and enjoys linear convergence
  rate.

  [Proximal Stochastic Dual Coordinate Ascent](http://arxiv.org/pdf/1211.2717v1.pdf).<br>
  Shai Shalev-Shwartz, Tong Zhang. 2012

  $$Loss Objective = \sum f_{i} (wx_{i}) + (l2 / 2) * |w|^2 + l1 * |w|$$

  [Adding vs. Averaging in Distributed Primal-Dual Optimization](http://arxiv.org/abs/1502.03508).<br>
  Chenxin Ma, Virginia Smith, Martin Jaggi, Michael I. Jordan,
  Peter Richtarik, Martin Takac. 2015

  [Stochastic Dual Coordinate Ascent with Adaptive Probabilities](https://arxiv.org/abs/1502.08053).<br>
  Dominik Csiba, Zheng Qu, Peter Richtarik. 2015

  Args:
    sparse_example_indices: A list of `Tensor` objects with type `int64`.
      a list of vectors which contain example indices.
    sparse_feature_indices: A list with the same length as `sparse_example_indices` of `Tensor` objects with type `int64`.
      a list of vectors which contain feature indices.
    sparse_feature_values: A list of `Tensor` objects with type `float32`.
      a list of vectors which contains feature value
      associated with each feature group.
    dense_features: A list of `Tensor` objects with type `float32`.
      a list of matrices which contains the dense feature values.
    example_weights: A `Tensor` of type `float32`.
      a vector which contains the weight associated with each
      example.
    example_labels: A `Tensor` of type `float32`.
      a vector which contains the label/target associated with each
      example.
    sparse_indices: A list with the same length as `sparse_example_indices` of `Tensor` objects with type `int64`.
      a list of vectors where each value is the indices which has
      corresponding weights in sparse_weights. This field maybe omitted for the
      dense approach.
    sparse_weights: A list with the same length as `sparse_example_indices` of `Tensor` objects with type `float32`.
      a list of vectors where each value is the weight associated with
      a sparse feature group.
    dense_weights: A list with the same length as `dense_features` of `Tensor` objects with type `float32`.
      a list of vectors where the values are the weights associated
      with a dense feature group.
    example_state_data: A `Tensor` of type `float32`.
      a list of vectors containing the example state data.
    loss_type: A `string` from: `"logistic_loss", "squared_loss", "hinge_loss", "smooth_hinge_loss", "poisson_loss"`.
      Type of the primal loss. Currently SdcaSolver supports logistic,
      squared and hinge losses.
    l1: A `float`. Symmetric l1 regularization strength.
    l2: A `float`. Symmetric l2 regularization strength.
    num_loss_partitions: An `int` that is `>= 1`.
      Number of partitions of the global loss function.
    num_inner_iterations: An `int` that is `>= 1`.
      Number of iterations per mini-batch.
    adaptive: An optional `bool`. Defaults to `True`.
      Whether to use Adaptive SDCA for the inner loop.
    name: A name for the operation (optional).

  Returns:
    A tuple of `Tensor` objects (out_example_state_data, out_delta_sparse_weights, out_delta_dense_weights).

    out_example_state_data: A `Tensor` of type `float32`.
    out_delta_sparse_weights: A list with the same length as `sparse_example_indices` of `Tensor` objects with type `float32`.
    out_delta_dense_weights: A list with the same length as `dense_features` of `Tensor` objects with type `float32`.
  """
  _ctx = _context._context or _context.context()
  tld = _ctx._thread_local_data
  if tld.is_eager:
    try:
      _result = pywrap_tfe.TFE_Py_FastPathExecute(
        _ctx, "SdcaOptimizerV2", name, sparse_example_indices,
        sparse_feature_indices, sparse_feature_values, dense_features,
        example_weights, example_labels, sparse_indices, sparse_weights,
        dense_weights, example_state_data, "loss_type", loss_type, "adaptive",
        adaptive, "l1", l1, "l2", l2, "num_loss_partitions",
        num_loss_partitions, "num_inner_iterations", num_inner_iterations)
      _result = _SdcaOptimizerV2Output._make(_result)
      return _result
    except _core._NotOkStatusException as e:
      _ops.raise_from_not_ok_status(e, name)
    except _core._FallbackException:
      pass
    try:
      return sdca_optimizer_v2_eager_fallback(
          sparse_example_indices, sparse_feature_indices,
          sparse_feature_values, dense_features, example_weights,
          example_labels, sparse_indices, sparse_weights, dense_weights,
          example_state_data, loss_type=loss_type, adaptive=adaptive, l1=l1,
          l2=l2, num_loss_partitions=num_loss_partitions,
          num_inner_iterations=num_inner_iterations, name=name, ctx=_ctx)
    except _core._SymbolicException:
      pass  # Add nodes to the TensorFlow graph.
  # Add nodes to the TensorFlow graph.
  if not isinstance(sparse_example_indices, (list, tuple)):
    raise TypeError(
        "Expected list for 'sparse_example_indices' argument to "
        "'sdca_optimizer_v2' Op, not %r." % sparse_example_indices)
  _attr_num_sparse_features = len(sparse_example_indices)
  if not isinstance(sparse_feature_indices, (list, tuple)):
    raise TypeError(
        "Expected list for 'sparse_feature_indices' argument to "
        "'sdca_optimizer_v2' Op, not %r." % sparse_feature_indices)
  if len(sparse_feature_indices) != _attr_num_sparse_features:
    raise ValueError(
        "List argument 'sparse_feature_indices' to 'sdca_optimizer_v2' Op with length %d "
        "must match length %d of argument 'sparse_example_indices'." %
        (len(sparse_feature_indices), _attr_num_sparse_features))
  if not isinstance(sparse_indices, (list, tuple)):
    raise TypeError(
        "Expected list for 'sparse_indices' argument to "
        "'sdca_optimizer_v2' Op, not %r." % sparse_indices)
  if len(sparse_indices) != _attr_num_sparse_features:
    raise ValueError(
        "List argument 'sparse_indices' to 'sdca_optimizer_v2' Op with length %d "
        "must match length %d of argument 'sparse_example_indices'." %
        (len(sparse_indices), _attr_num_sparse_features))
  if not isinstance(sparse_weights, (list, tuple)):
    raise TypeError(
        "Expected list for 'sparse_weights' argument to "
        "'sdca_optimizer_v2' Op, not %r." % sparse_weights)
  if len(sparse_weights) != _attr_num_sparse_features:
    raise ValueError(
        "List argument 'sparse_weights' to 'sdca_optimizer_v2' Op with length %d "
        "must match length %d of argument 'sparse_example_indices'." %
        (len(sparse_weights), _attr_num_sparse_features))
  if not isinstance(sparse_feature_values, (list, tuple)):
    raise TypeError(
        "Expected list for 'sparse_feature_values' argument to "
        "'sdca_optimizer_v2' Op, not %r." % sparse_feature_values)
  _attr_num_sparse_features_with_values = len(sparse_feature_values)
  if not isinstance(dense_features, (list, tuple)):
    raise TypeError(
        "Expected list for 'dense_features' argument to "
        "'sdca_optimizer_v2' Op, not %r." % dense_features)
  _attr_num_dense_features = len(dense_features)
  if not isinstance(dense_weights, (list, tuple)):
    raise TypeError(
        "Expected list for 'dense_weights' argument to "
        "'sdca_optimizer_v2' Op, not %r." % dense_weights)
  if len(dense_weights) != _attr_num_dense_features:
    raise ValueError(
        "List argument 'dense_weights' to 'sdca_optimizer_v2' Op with length %d "
        "must match length %d of argument 'dense_features'." %
        (len(dense_weights), _attr_num_dense_features))
  loss_type = _execute.make_str(loss_type, "loss_type")
  l1 = _execute.make_float(l1, "l1")
  l2 = _execute.make_float(l2, "l2")
  num_loss_partitions = _execute.make_int(num_loss_partitions, "num_loss_partitions")
  num_inner_iterations = _execute.make_int(num_inner_iterations, "num_inner_iterations")
  if adaptive is None:
    adaptive = True
  adaptive = _execute.make_bool(adaptive, "adaptive")
  _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "SdcaOptimizerV2", sparse_example_indices=sparse_example_indices,
                           sparse_feature_indices=sparse_feature_indices,
                           sparse_feature_values=sparse_feature_values,
                           dense_features=dense_features,
                           example_weights=example_weights,
                           example_labels=example_labels,
                           sparse_indices=sparse_indices,
                           sparse_weights=sparse_weights,
                           dense_weights=dense_weights,
                           example_state_data=example_state_data,
                           loss_type=loss_type, l1=l1, l2=l2,
                           num_loss_partitions=num_loss_partitions,
                           num_inner_iterations=num_inner_iterations,
                           adaptive=adaptive, name=name)
  _result = _outputs[:]
  if _execute.must_record_gradient():
    _attrs = ("loss_type", _op.get_attr("loss_type"), "adaptive",
              _op._get_attr_bool("adaptive"), "num_sparse_features",
              _op._get_attr_int("num_sparse_features"),
              "num_sparse_features_with_values",
              _op._get_attr_int("num_sparse_features_with_values"),
              "num_dense_features", _op._get_attr_int("num_dense_features"),
              "l1", _op.get_attr("l1"), "l2", _op.get_attr("l2"),
              "num_loss_partitions", _op._get_attr_int("num_loss_partitions"),
              "num_inner_iterations",
              _op._get_attr_int("num_inner_iterations"))
    _inputs_flat = _op.inputs
    _execute.record_gradient(
        "SdcaOptimizerV2", _inputs_flat, _attrs, _result)
  _result = _result[:1] + [_result[1:1 + _attr_num_sparse_features]] + _result[1 + _attr_num_sparse_features:]
  _result = _result[:2] + [_result[2:]]
  _result = _SdcaOptimizerV2Output._make(_result)
  return _result
Пример #2
0
def trt_engine_op(in_tensor,
                  serialized_segment,
                  OutT,
                  workspace_size_bytes,
                  precision_mode,
                  segment_func="",
                  input_shapes=[],
                  max_cached_engines_count=1,
                  calibration_data="",
                  use_calibration=True,
                  segment_funcdef_name="",
                  cached_engine_batches=[],
                  fixed_input_size=True,
                  output_shapes=[],
                  static_engine=True,
                  name=None):
    r"""TODO: add doc.

  Args:
    in_tensor: A list of `Tensor` objects with types from: `int8`, `half`, `float32`, `int32`.
    serialized_segment: A `string`.
    OutT: A list of `tf.DTypes` from: `tf.int8, tf.half, tf.float32, tf.int32` that has length `>= 1`.
    workspace_size_bytes: An `int`.
    precision_mode: A `string` from: `"FP32", "FP16", "INT8"`.
    segment_func: An optional function decorated with @Defun. Defaults to `""`.
    input_shapes: An optional list of shapes (each a `tf.TensorShape` or list of `ints`). Defaults to `[]`.
    max_cached_engines_count: An optional `int`. Defaults to `1`.
    calibration_data: An optional `string`. Defaults to `""`.
    use_calibration: An optional `bool`. Defaults to `True`.
    segment_funcdef_name: An optional `string`. Defaults to `""`.
    cached_engine_batches: An optional list of `ints`. Defaults to `[]`.
    fixed_input_size: An optional `bool`. Defaults to `True`.
    output_shapes: An optional list of shapes (each a `tf.TensorShape` or list of `ints`). Defaults to `[]`.
    static_engine: An optional `bool`. Defaults to `True`.
    name: A name for the operation (optional).

  Returns:
    A list of `Tensor` objects of type `OutT`.
  """
    _ctx = _context._context or _context.context()
    tld = _ctx._thread_local_data
    if tld.is_eager:
        try:
            _result = pywrap_tfe.TFE_Py_FastPathExecute(
                _ctx._context_handle, tld.device_name, "TRTEngineOp", name,
                tld.op_callbacks, in_tensor, "serialized_segment",
                serialized_segment, "segment_func", segment_func, "OutT", OutT,
                "input_shapes", input_shapes, "max_cached_engines_count",
                max_cached_engines_count, "workspace_size_bytes",
                workspace_size_bytes, "precision_mode", precision_mode,
                "calibration_data", calibration_data, "use_calibration",
                use_calibration, "segment_funcdef_name", segment_funcdef_name,
                "cached_engine_batches", cached_engine_batches,
                "fixed_input_size", fixed_input_size, "output_shapes",
                output_shapes, "static_engine", static_engine)
            return _result
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
        except _core._FallbackException:
            pass
        try:
            return trt_engine_op_eager_fallback(
                in_tensor,
                serialized_segment=serialized_segment,
                segment_func=segment_func,
                OutT=OutT,
                input_shapes=input_shapes,
                max_cached_engines_count=max_cached_engines_count,
                workspace_size_bytes=workspace_size_bytes,
                precision_mode=precision_mode,
                calibration_data=calibration_data,
                use_calibration=use_calibration,
                segment_funcdef_name=segment_funcdef_name,
                cached_engine_batches=cached_engine_batches,
                fixed_input_size=fixed_input_size,
                output_shapes=output_shapes,
                static_engine=static_engine,
                name=name,
                ctx=_ctx)
        except _core._SymbolicException:
            pass  # Add nodes to the TensorFlow graph.
        except (TypeError, ValueError):
            result = _dispatch.dispatch(
                trt_engine_op, (),
                dict(in_tensor=in_tensor,
                     serialized_segment=serialized_segment,
                     OutT=OutT,
                     workspace_size_bytes=workspace_size_bytes,
                     precision_mode=precision_mode,
                     segment_func=segment_func,
                     input_shapes=input_shapes,
                     max_cached_engines_count=max_cached_engines_count,
                     calibration_data=calibration_data,
                     use_calibration=use_calibration,
                     segment_funcdef_name=segment_funcdef_name,
                     cached_engine_batches=cached_engine_batches,
                     fixed_input_size=fixed_input_size,
                     output_shapes=output_shapes,
                     static_engine=static_engine,
                     name=name))
            if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
                return result
            raise
    # Add nodes to the TensorFlow graph.
    serialized_segment = _execute.make_str(serialized_segment,
                                           "serialized_segment")
    if not isinstance(OutT, (list, tuple)):
        raise TypeError("Expected list for 'OutT' argument to "
                        "'trt_engine_op' Op, not %r." % OutT)
    OutT = [_execute.make_type(_t, "OutT") for _t in OutT]
    workspace_size_bytes = _execute.make_int(workspace_size_bytes,
                                             "workspace_size_bytes")
    precision_mode = _execute.make_str(precision_mode, "precision_mode")
    if segment_func is None:
        segment_func = ""
    if input_shapes is None:
        input_shapes = []
    if not isinstance(input_shapes, (list, tuple)):
        raise TypeError("Expected list for 'input_shapes' argument to "
                        "'trt_engine_op' Op, not %r." % input_shapes)
    input_shapes = [
        _execute.make_shape(_s, "input_shapes") for _s in input_shapes
    ]
    if max_cached_engines_count is None:
        max_cached_engines_count = 1
    max_cached_engines_count = _execute.make_int(max_cached_engines_count,
                                                 "max_cached_engines_count")
    if calibration_data is None:
        calibration_data = ""
    calibration_data = _execute.make_str(calibration_data, "calibration_data")
    if use_calibration is None:
        use_calibration = True
    use_calibration = _execute.make_bool(use_calibration, "use_calibration")
    if segment_funcdef_name is None:
        segment_funcdef_name = ""
    segment_funcdef_name = _execute.make_str(segment_funcdef_name,
                                             "segment_funcdef_name")
    if cached_engine_batches is None:
        cached_engine_batches = []
    if not isinstance(cached_engine_batches, (list, tuple)):
        raise TypeError(
            "Expected list for 'cached_engine_batches' argument to "
            "'trt_engine_op' Op, not %r." % cached_engine_batches)
    cached_engine_batches = [
        _execute.make_int(_i, "cached_engine_batches")
        for _i in cached_engine_batches
    ]
    if fixed_input_size is None:
        fixed_input_size = True
    fixed_input_size = _execute.make_bool(fixed_input_size, "fixed_input_size")
    if output_shapes is None:
        output_shapes = []
    if not isinstance(output_shapes, (list, tuple)):
        raise TypeError("Expected list for 'output_shapes' argument to "
                        "'trt_engine_op' Op, not %r." % output_shapes)
    output_shapes = [
        _execute.make_shape(_s, "output_shapes") for _s in output_shapes
    ]
    if static_engine is None:
        static_engine = True
    static_engine = _execute.make_bool(static_engine, "static_engine")
    try:
        _, _, _op, _outputs = _op_def_library._apply_op_helper(
            "TRTEngineOp",
            in_tensor=in_tensor,
            serialized_segment=serialized_segment,
            OutT=OutT,
            workspace_size_bytes=workspace_size_bytes,
            precision_mode=precision_mode,
            segment_func=segment_func,
            input_shapes=input_shapes,
            max_cached_engines_count=max_cached_engines_count,
            calibration_data=calibration_data,
            use_calibration=use_calibration,
            segment_funcdef_name=segment_funcdef_name,
            cached_engine_batches=cached_engine_batches,
            fixed_input_size=fixed_input_size,
            output_shapes=output_shapes,
            static_engine=static_engine,
            name=name)
    except (TypeError, ValueError):
        result = _dispatch.dispatch(
            trt_engine_op, (),
            dict(in_tensor=in_tensor,
                 serialized_segment=serialized_segment,
                 OutT=OutT,
                 workspace_size_bytes=workspace_size_bytes,
                 precision_mode=precision_mode,
                 segment_func=segment_func,
                 input_shapes=input_shapes,
                 max_cached_engines_count=max_cached_engines_count,
                 calibration_data=calibration_data,
                 use_calibration=use_calibration,
                 segment_funcdef_name=segment_funcdef_name,
                 cached_engine_batches=cached_engine_batches,
                 fixed_input_size=fixed_input_size,
                 output_shapes=output_shapes,
                 static_engine=static_engine,
                 name=name))
        if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
            return result
        raise
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("serialized_segment", _op.get_attr("serialized_segment"),
                  "segment_func", _op.get_attr("segment_func"), "InT",
                  _op.get_attr("InT"), "OutT",
                  _op.get_attr("OutT"), "input_shapes",
                  _op.get_attr("input_shapes"), "max_cached_engines_count",
                  _op._get_attr_int("max_cached_engines_count"),
                  "workspace_size_bytes",
                  _op._get_attr_int("workspace_size_bytes"), "precision_mode",
                  _op.get_attr("precision_mode"), "calibration_data",
                  _op.get_attr("calibration_data"), "use_calibration",
                  _op._get_attr_bool("use_calibration"),
                  "segment_funcdef_name", _op.get_attr("segment_funcdef_name"),
                  "cached_engine_batches",
                  _op.get_attr("cached_engine_batches"), "fixed_input_size",
                  _op._get_attr_bool("fixed_input_size"), "output_shapes",
                  _op.get_attr("output_shapes"), "static_engine",
                  _op._get_attr_bool("static_engine"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("TRTEngineOp", _inputs_flat, _attrs, _result)
    return _result
Пример #3
0
def copy(input, tensor_name="", debug_ops_spec=[], name=None):
    r"""Copy a tensor from CPU-to-CPU or GPU-to-GPU.

  Performs CPU-to-CPU or GPU-to-GPU deep-copying of tensor, depending on the
  device on which the tensor is allocated.
  N.B.: If the all downstream attached debug ops are disabled given the current
  gRPC gating status, the output will simply forward the input tensor without
  deep-copying. See the documentation of Debug* ops for more details.

  Unlike the CopyHost Op, this op does not have HostMemory constraint on its
  input or output.

  Args:
    input: A `Tensor`. Input tensor.
    tensor_name: An optional `string`. Defaults to `""`.
      The name of the input tensor.
    debug_ops_spec: An optional list of `strings`. Defaults to `[]`.
      A list of debug op spec (op, url, gated_grpc) for attached debug
      ops. Each element of the list has the format
      <debug_op>;<grpc_url>;<gated_grpc>, wherein gated_grpc is boolean represented
      as 0/1. E.g., "DebugIdentity;grpc://foo:3333;1",
      "DebugIdentity;file:///tmp/tfdbg_1;0".
    name: A name for the operation (optional).

  Returns:
    A `Tensor`. Has the same type as `input`.
  """
    _ctx = _context._context or _context.context()
    tld = _ctx._thread_local_data
    if tld.is_eager:
        try:
            _result = pywrap_tfe.TFE_Py_FastPathExecute(
                _ctx, "Copy", name, input, "tensor_name", tensor_name,
                "debug_ops_spec", debug_ops_spec)
            return _result
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
        except _core._FallbackException:
            pass
        try:
            return copy_eager_fallback(input,
                                       tensor_name=tensor_name,
                                       debug_ops_spec=debug_ops_spec,
                                       name=name,
                                       ctx=_ctx)
        except _core._SymbolicException:
            pass  # Add nodes to the TensorFlow graph.
    # Add nodes to the TensorFlow graph.
    if tensor_name is None:
        tensor_name = ""
    tensor_name = _execute.make_str(tensor_name, "tensor_name")
    if debug_ops_spec is None:
        debug_ops_spec = []
    if not isinstance(debug_ops_spec, (list, tuple)):
        raise TypeError("Expected list for 'debug_ops_spec' argument to "
                        "'copy' Op, not %r." % debug_ops_spec)
    debug_ops_spec = [
        _execute.make_str(_s, "debug_ops_spec") for _s in debug_ops_spec
    ]
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "Copy",
        input=input,
        tensor_name=tensor_name,
        debug_ops_spec=debug_ops_spec,
        name=name)
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("T", _op._get_attr_type("T"), "tensor_name",
                  _op.get_attr("tensor_name"), "debug_ops_spec",
                  _op.get_attr("debug_ops_spec"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("Copy", _inputs_flat, _attrs, _result)
    _result, = _result
    return _result
Пример #4
0
def ragged_tensor_to_variant(rt_nested_splits,
                             rt_dense_values,
                             batched_input,
                             name=None):
    r"""Encodes a `RaggedTensor` into a `variant` Tensor.

  
  Encodes the given `RaggedTensor` and returns a `variant` Tensor. If
  `batched_input` is True, then input `RaggedTensor` is unbatched along the
  zero-th dimension, each component `RaggedTensor` is encoded into a scalar
  `variant` Tensor, and these are stacked to return a 1-D `variant` Tensor.
  If `batched_input` is False, then the input `RaggedTensor` is encoded as is and
  a scalar `variant` Tensor is returned. A `RaggedTensor` is encoded by first
  creating a 1-D `variant` Tensor with `ragged_rank + 1` elements, containing the
  splits and values Tensors of the `RaggedTensor`. Then the 1-D `variant` Tensor
  is wrapped in a scalar `variant` Tensor. See `RaggedTensorFromVariant` for the
  corresponding decoding logic.

  Args:
    rt_nested_splits: A list of `Tensor` objects with the same type in: `int32`, `int64`.
      A list of one or more Tensors representing the splits of the input
      `RaggedTensor`.
    rt_dense_values: A `Tensor`.
      A Tensor representing the values of the input `RaggedTensor`.
    batched_input: A `bool`.
      A `bool` denoting whether the input is a batched `RaggedTensor`.
    name: A name for the operation (optional).

  Returns:
    A `Tensor` of type `variant`.
  """
    _ctx = _context._context or _context.context()
    tld = _ctx._thread_local_data
    if tld.is_eager:
        try:
            _result = pywrap_tfe.TFE_Py_FastPathExecute(
                _ctx._context_handle, tld.device_name, "RaggedTensorToVariant",
                name, tld.op_callbacks, rt_nested_splits, rt_dense_values,
                "batched_input", batched_input)
            return _result
        except _core._FallbackException:
            try:
                return ragged_tensor_to_variant_eager_fallback(
                    rt_nested_splits,
                    rt_dense_values,
                    batched_input=batched_input,
                    name=name,
                    ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
    # Add nodes to the TensorFlow graph.
    if not isinstance(rt_nested_splits, (list, tuple)):
        raise TypeError("Expected list for 'rt_nested_splits' argument to "
                        "'ragged_tensor_to_variant' Op, not %r." %
                        rt_nested_splits)
    _attr_RAGGED_RANK = len(rt_nested_splits)
    batched_input = _execute.make_bool(batched_input, "batched_input")
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "RaggedTensorToVariant",
        rt_nested_splits=rt_nested_splits,
        rt_dense_values=rt_dense_values,
        batched_input=batched_input,
        name=name)
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("RAGGED_RANK", _op._get_attr_int("RAGGED_RANK"), "Tvalues",
                  _op._get_attr_type("Tvalues"), "Tsplits",
                  _op._get_attr_type("Tsplits"), "batched_input",
                  _op._get_attr_bool("batched_input"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("RaggedTensorToVariant", _inputs_flat, _attrs,
                                 _result)
    _result, = _result
    return _result
Пример #5
0
def nccl_all_reduce(input, reduction, num_devices, shared_name, name=None):
  r"""Outputs a tensor containing the reduction across all input tensors.

  Outputs a tensor containing the reduction across all input tensors passed to ops
  within the same `shared_name.

  The graph should be constructed so if one op runs with shared_name value `c`,
  then `num_devices` ops will run with shared_name value `c`.  Failure to do so
  will cause the graph execution to fail to complete.

  input: the input to the reduction
  data: the value of the reduction across all `num_devices` devices.
  reduction: the reduction operation to perform.
  num_devices: The number of devices participating in this reduction.
  shared_name: Identifier that shared between ops of the same reduction.

  Args:
    input: A `Tensor`. Must be one of the following types: `half`, `float32`, `float64`, `int32`, `int64`.
    reduction: A `string` from: `"min", "max", "prod", "sum"`.
    num_devices: An `int`.
    shared_name: A `string`.
    name: A name for the operation (optional).

  Returns:
    A `Tensor`. Has the same type as `input`.
  """
  _ctx = _context._context or _context.context()
  tld = _ctx._thread_local_data
  if tld.is_eager:
    try:
      _result = pywrap_tfe.TFE_Py_FastPathExecute(
        _ctx, "NcclAllReduce", name, input, "reduction", reduction,
        "num_devices", num_devices, "shared_name", shared_name)
      return _result
    except _core._NotOkStatusException as e:
      _ops.raise_from_not_ok_status(e, name)
    except _core._FallbackException:
      pass
    try:
      return nccl_all_reduce_eager_fallback(
          input, reduction=reduction, num_devices=num_devices,
          shared_name=shared_name, name=name, ctx=_ctx)
    except _core._SymbolicException:
      pass  # Add nodes to the TensorFlow graph.
  # Add nodes to the TensorFlow graph.
  reduction = _execute.make_str(reduction, "reduction")
  num_devices = _execute.make_int(num_devices, "num_devices")
  shared_name = _execute.make_str(shared_name, "shared_name")
  _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "NcclAllReduce", input=input, reduction=reduction,
                         num_devices=num_devices, shared_name=shared_name,
                         name=name)
  _result = _outputs[:]
  if _execute.must_record_gradient():
    _attrs = ("reduction", _op.get_attr("reduction"), "T",
              _op._get_attr_type("T"), "num_devices",
              _op._get_attr_int("num_devices"), "shared_name",
              _op.get_attr("shared_name"))
    _inputs_flat = _op.inputs
    _execute.record_gradient(
        "NcclAllReduce", _inputs_flat, _attrs, _result)
  _result, = _result
  return _result
Пример #6
0
def encode_wav(audio, sample_rate, name=None):
    r"""Encode audio data using the WAV file format.

  This operation will generate a string suitable to be saved out to create a .wav
  audio file. It will be encoded in the 16-bit PCM format. It takes in float
  values in the range -1.0f to 1.0f, and any outside that value will be clamped to
  that range.

  `audio` is a 2-D float Tensor of shape `[length, channels]`.
  `sample_rate` is a scalar Tensor holding the rate to use (e.g. 44100).

  Args:
    audio: A `Tensor` of type `float32`. 2-D with shape `[length, channels]`.
    sample_rate: A `Tensor` of type `int32`.
      Scalar containing the sample frequency.
    name: A name for the operation (optional).

  Returns:
    A `Tensor` of type `string`.
  """
    _ctx = _context._context or _context.context()
    tld = _ctx._thread_local_data
    if tld.is_eager:
        try:
            _result = pywrap_tfe.TFE_Py_FastPathExecute(
                _ctx, "EncodeWav", name, audio, sample_rate)
            return _result
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
        except _core._FallbackException:
            pass
        try:
            return encode_wav_eager_fallback(audio,
                                             sample_rate,
                                             name=name,
                                             ctx=_ctx)
        except _core._SymbolicException:
            pass  # Add nodes to the TensorFlow graph.
        except (TypeError, ValueError):
            result = _dispatch.dispatch(
                encode_wav, (),
                dict(audio=audio, sample_rate=sample_rate, name=name))
            if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
                return result
            raise
    # Add nodes to the TensorFlow graph.
    try:
        _, _, _op, _outputs = _op_def_library._apply_op_helper(
            "EncodeWav", audio=audio, sample_rate=sample_rate, name=name)
    except (TypeError, ValueError):
        result = _dispatch.dispatch(
            encode_wav, (),
            dict(audio=audio, sample_rate=sample_rate, name=name))
        if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
            return result
        raise
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ()
        _inputs_flat = _op.inputs
        _execute.record_gradient("EncodeWav", _inputs_flat, _attrs, _result)
    _result, = _result
    return _result
Пример #7
0
def ragged_tensor_from_variant(encoded_ragged,
                               input_ragged_rank,
                               output_ragged_rank,
                               Tvalues,
                               Tsplits=_dtypes.int64,
                               name=None):
    r"""Decodes a `variant` Tensor into a `RaggedTensor`.

  Decodes the given `variant` Tensor and returns a `RaggedTensor`. The input
  could be a scalar, meaning it encodes a single `RaggedTensor` with ragged_rank
  `output_ragged_rank`. It could also have an arbitrary rank, in which case each
  element is decoded into a `RaggedTensor` with ragged_rank `input_ragged_rank`
  and these are then stacked according to the input shape to output a single
  `RaggedTensor` with ragged_rank `output_ragged_rank`. Each `variant` element in
  the input Tensor is decoded by retrieving from the element a 1-D `variant`
  Tensor with `input_ragged_rank + 1` Tensors, corresponding to the splits and
  values of the decoded `RaggedTensor`. If `input_ragged_rank` is -1, then it is
  inferred as `output_ragged_rank` - `rank(encoded_ragged)`. See
  `RaggedTensorToVariant` for the corresponding encoding logic.

  Args:
    encoded_ragged: A `Tensor` of type `variant`.
      A `variant` Tensor containing encoded `RaggedTensor`s.
    input_ragged_rank: An `int` that is `>= -1`.
      The ragged rank of each encoded `RaggedTensor` component in the input. If set to
      -1, this is inferred as `output_ragged_rank` - `rank(encoded_ragged)`
    output_ragged_rank: An `int` that is `>= 0`.
      The expected ragged rank of the output `RaggedTensor`. The following must hold:
      `output_ragged_rank = rank(encoded_ragged) + input_ragged_rank`.
    Tvalues: A `tf.DType`.
    Tsplits: An optional `tf.DType` from: `tf.int32, tf.int64`. Defaults to `tf.int64`.
    name: A name for the operation (optional).

  Returns:
    A tuple of `Tensor` objects (output_nested_splits, output_dense_values).

    output_nested_splits: A list of `output_ragged_rank` `Tensor` objects with type `Tsplits`.
    output_dense_values: A `Tensor` of type `Tvalues`.
  """
    _ctx = _context._context or _context.context()
    tld = _ctx._thread_local_data
    if tld.is_eager:
        try:
            _result = pywrap_tfe.TFE_Py_FastPathExecute(
                _ctx._context_handle, tld.device_name,
                "RaggedTensorFromVariant", name, tld.op_callbacks,
                encoded_ragged, "input_ragged_rank", input_ragged_rank,
                "output_ragged_rank", output_ragged_rank, "Tvalues", Tvalues,
                "Tsplits", Tsplits)
            _result = _RaggedTensorFromVariantOutput._make(_result)
            return _result
        except _core._FallbackException:
            try:
                return ragged_tensor_from_variant_eager_fallback(
                    encoded_ragged,
                    input_ragged_rank=input_ragged_rank,
                    output_ragged_rank=output_ragged_rank,
                    Tvalues=Tvalues,
                    Tsplits=Tsplits,
                    name=name,
                    ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
    # Add nodes to the TensorFlow graph.
    input_ragged_rank = _execute.make_int(input_ragged_rank,
                                          "input_ragged_rank")
    output_ragged_rank = _execute.make_int(output_ragged_rank,
                                           "output_ragged_rank")
    Tvalues = _execute.make_type(Tvalues, "Tvalues")
    if Tsplits is None:
        Tsplits = _dtypes.int64
    Tsplits = _execute.make_type(Tsplits, "Tsplits")
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "RaggedTensorFromVariant",
        encoded_ragged=encoded_ragged,
        input_ragged_rank=input_ragged_rank,
        output_ragged_rank=output_ragged_rank,
        Tvalues=Tvalues,
        Tsplits=Tsplits,
        name=name)
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("input_ragged_rank",
                  _op._get_attr_int("input_ragged_rank"), "output_ragged_rank",
                  _op._get_attr_int("output_ragged_rank"), "Tvalues",
                  _op._get_attr_type("Tvalues"), "Tsplits",
                  _op._get_attr_type("Tsplits"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("RaggedTensorFromVariant", _inputs_flat,
                                 _attrs, _result)
    _result = [_result[:output_ragged_rank]] + _result[output_ragged_rank:]
    _result = _RaggedTensorFromVariantOutput._make(_result)
    return _result
def thread_unsafe_unigram_candidate_sampler(true_classes,
                                            num_true,
                                            num_sampled,
                                            unique,
                                            range_max,
                                            seed=0,
                                            seed2=0,
                                            name=None):
    r"""Generates labels for candidate sampling with a learned unigram distribution.

  See explanations of candidate sampling and the data formats at
  go/candidate-sampling.

  For each batch, this op picks a single set of sampled candidate labels.

  The advantages of sampling candidates per-batch are simplicity and the
  possibility of efficient dense matrix multiplication. The disadvantage is that
  the sampled candidates must be chosen independently of the context and of the
  true labels.

  Args:
    true_classes: A `Tensor` of type `int64`.
      A batch_size * num_true matrix, in which each row contains the
      IDs of the num_true target_classes in the corresponding original label.
    num_true: An `int` that is `>= 1`. Number of true labels per context.
    num_sampled: An `int` that is `>= 1`.
      Number of candidates to randomly sample.
    unique: A `bool`.
      If unique is true, we sample with rejection, so that all sampled
      candidates in a batch are unique. This requires some approximation to
      estimate the post-rejection sampling probabilities.
    range_max: An `int` that is `>= 1`.
      The sampler will sample integers from the interval [0, range_max).
    seed: An optional `int`. Defaults to `0`.
      If either seed or seed2 are set to be non-zero, the random number
      generator is seeded by the given seed.  Otherwise, it is seeded by a
      random seed.
    seed2: An optional `int`. Defaults to `0`.
      An second seed to avoid seed collision.
    name: A name for the operation (optional).

  Returns:
    A tuple of `Tensor` objects (sampled_candidates, true_expected_count, sampled_expected_count).

    sampled_candidates: A `Tensor` of type `int64`.
    true_expected_count: A `Tensor` of type `float32`.
    sampled_expected_count: A `Tensor` of type `float32`.
  """
    _ctx = _context._context or _context.context()
    tld = _ctx._thread_local_data
    if tld.is_eager:
        try:
            _result = pywrap_tfe.TFE_Py_FastPathExecute(
                _ctx._context_handle, tld.device_name,
                "ThreadUnsafeUnigramCandidateSampler", name, tld.op_callbacks,
                true_classes, "num_true", num_true, "num_sampled", num_sampled,
                "unique", unique, "range_max", range_max, "seed", seed,
                "seed2", seed2)
            _result = _ThreadUnsafeUnigramCandidateSamplerOutput._make(_result)
            return _result
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
        except _core._FallbackException:
            pass
        try:
            return thread_unsafe_unigram_candidate_sampler_eager_fallback(
                true_classes,
                num_true=num_true,
                num_sampled=num_sampled,
                unique=unique,
                range_max=range_max,
                seed=seed,
                seed2=seed2,
                name=name,
                ctx=_ctx)
        except _core._SymbolicException:
            pass  # Add nodes to the TensorFlow graph.
    # Add nodes to the TensorFlow graph.
    num_true = _execute.make_int(num_true, "num_true")
    num_sampled = _execute.make_int(num_sampled, "num_sampled")
    unique = _execute.make_bool(unique, "unique")
    range_max = _execute.make_int(range_max, "range_max")
    if seed is None:
        seed = 0
    seed = _execute.make_int(seed, "seed")
    if seed2 is None:
        seed2 = 0
    seed2 = _execute.make_int(seed2, "seed2")
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "ThreadUnsafeUnigramCandidateSampler",
        true_classes=true_classes,
        num_true=num_true,
        num_sampled=num_sampled,
        unique=unique,
        range_max=range_max,
        seed=seed,
        seed2=seed2,
        name=name)
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("num_true", _op._get_attr_int("num_true"), "num_sampled",
                  _op._get_attr_int("num_sampled"), "unique",
                  _op._get_attr_bool("unique"), "range_max",
                  _op._get_attr_int("range_max"), "seed",
                  _op._get_attr_int("seed"), "seed2",
                  _op._get_attr_int("seed2"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("ThreadUnsafeUnigramCandidateSampler",
                                 _inputs_flat, _attrs, _result)
    _result = _ThreadUnsafeUnigramCandidateSamplerOutput._make(_result)
    return _result
Пример #9
0
def set_size(set_indices,
             set_values,
             set_shape,
             validate_indices=True,
             name=None):
    r"""Number of unique elements along last dimension of input `set`.

  Input `set` is a `SparseTensor` represented by `set_indices`, `set_values`,
  and `set_shape`. The last dimension contains values in a set, duplicates are
  allowed but ignored.

  If `validate_indices` is `True`, this op validates the order and range of `set`
  indices.

  Args:
    set_indices: A `Tensor` of type `int64`.
      2D `Tensor`, indices of a `SparseTensor`.
    set_values: A `Tensor`. Must be one of the following types: `int8`, `int16`, `int32`, `int64`, `uint8`, `uint16`, `string`.
      1D `Tensor`, values of a `SparseTensor`.
    set_shape: A `Tensor` of type `int64`.
      1D `Tensor`, shape of a `SparseTensor`.
    validate_indices: An optional `bool`. Defaults to `True`.
    name: A name for the operation (optional).

  Returns:
    A `Tensor` of type `int32`.
  """
    _ctx = _context._context or _context.context()
    tld = _ctx._thread_local_data
    if tld.is_eager:
        try:
            _result = pywrap_tfe.TFE_Py_FastPathExecute(
                _ctx._context_handle, tld.device_name, "SetSize", name,
                tld.op_callbacks, set_indices, set_values, set_shape,
                "validate_indices", validate_indices)
            return _result
        except _core._FallbackException:
            try:
                return set_size_eager_fallback(
                    set_indices,
                    set_values,
                    set_shape,
                    validate_indices=validate_indices,
                    name=name,
                    ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
    # Add nodes to the TensorFlow graph.
    if validate_indices is None:
        validate_indices = True
    validate_indices = _execute.make_bool(validate_indices, "validate_indices")
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "SetSize",
        set_indices=set_indices,
        set_values=set_values,
        set_shape=set_shape,
        validate_indices=validate_indices,
        name=name)
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("validate_indices", _op._get_attr_bool("validate_indices"),
                  "T", _op._get_attr_type("T"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("SetSize", _inputs_flat, _attrs, _result)
    _result, = _result
    return _result
def compute_accidental_hits(true_classes,
                            sampled_candidates,
                            num_true,
                            seed=0,
                            seed2=0,
                            name=None):
    r"""Computes the ids of the positions in sampled_candidates that match true_labels.

  When doing log-odds NCE, the result of this op should be passed through a
  SparseToDense op, then added to the logits of the sampled candidates. This has
  the effect of 'removing' the sampled labels that match the true labels by
  making the classifier sure that they are sampled labels.

  Args:
    true_classes: A `Tensor` of type `int64`.
      The true_classes output of UnpackSparseLabels.
    sampled_candidates: A `Tensor` of type `int64`.
      The sampled_candidates output of CandidateSampler.
    num_true: An `int`. Number of true labels per context.
    seed: An optional `int`. Defaults to `0`.
      If either seed or seed2 are set to be non-zero, the random number
      generator is seeded by the given seed.  Otherwise, it is seeded by a
      random seed.
    seed2: An optional `int`. Defaults to `0`.
      An second seed to avoid seed collision.
    name: A name for the operation (optional).

  Returns:
    A tuple of `Tensor` objects (indices, ids, weights).

    indices: A `Tensor` of type `int32`.
    ids: A `Tensor` of type `int64`.
    weights: A `Tensor` of type `float32`.
  """
    _ctx = _context._context or _context.context()
    tld = _ctx._thread_local_data
    if tld.is_eager:
        try:
            _result = pywrap_tfe.TFE_Py_FastPathExecute(
                _ctx._context_handle, tld.device_name, "ComputeAccidentalHits",
                name, tld.op_callbacks, true_classes, sampled_candidates,
                "num_true", num_true, "seed", seed, "seed2", seed2)
            _result = _ComputeAccidentalHitsOutput._make(_result)
            return _result
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
        except _core._FallbackException:
            pass
        try:
            return compute_accidental_hits_eager_fallback(true_classes,
                                                          sampled_candidates,
                                                          num_true=num_true,
                                                          seed=seed,
                                                          seed2=seed2,
                                                          name=name,
                                                          ctx=_ctx)
        except _core._SymbolicException:
            pass  # Add nodes to the TensorFlow graph.
    # Add nodes to the TensorFlow graph.
    num_true = _execute.make_int(num_true, "num_true")
    if seed is None:
        seed = 0
    seed = _execute.make_int(seed, "seed")
    if seed2 is None:
        seed2 = 0
    seed2 = _execute.make_int(seed2, "seed2")
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "ComputeAccidentalHits",
        true_classes=true_classes,
        sampled_candidates=sampled_candidates,
        num_true=num_true,
        seed=seed,
        seed2=seed2,
        name=name)
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("num_true", _op._get_attr_int("num_true"), "seed",
                  _op._get_attr_int("seed"), "seed2",
                  _op._get_attr_int("seed2"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("ComputeAccidentalHits", _inputs_flat, _attrs,
                                 _result)
    _result = _ComputeAccidentalHitsOutput._make(_result)
    return _result
def fixed_unigram_candidate_sampler(true_classes,
                                    num_true,
                                    num_sampled,
                                    unique,
                                    range_max,
                                    vocab_file="",
                                    distortion=1,
                                    num_reserved_ids=0,
                                    num_shards=1,
                                    shard=0,
                                    unigrams=[],
                                    seed=0,
                                    seed2=0,
                                    name=None):
    r"""Generates labels for candidate sampling with a learned unigram distribution.

  A unigram sampler could use a fixed unigram distribution read from a
  file or passed in as an in-memory array instead of building up the distribution
  from data on the fly. There is also an option to skew the distribution by
  applying a distortion power to the weights.

  The vocabulary file should be in CSV-like format, with the last field
  being the weight associated with the word.

  For each batch, this op picks a single set of sampled candidate labels.

  The advantages of sampling candidates per-batch are simplicity and the
  possibility of efficient dense matrix multiplication. The disadvantage is that
  the sampled candidates must be chosen independently of the context and of the
  true labels.

  Args:
    true_classes: A `Tensor` of type `int64`.
      A batch_size * num_true matrix, in which each row contains the
      IDs of the num_true target_classes in the corresponding original label.
    num_true: An `int` that is `>= 1`. Number of true labels per context.
    num_sampled: An `int` that is `>= 1`.
      Number of candidates to randomly sample.
    unique: A `bool`.
      If unique is true, we sample with rejection, so that all sampled
      candidates in a batch are unique. This requires some approximation to
      estimate the post-rejection sampling probabilities.
    range_max: An `int` that is `>= 1`.
      The sampler will sample integers from the interval [0, range_max).
    vocab_file: An optional `string`. Defaults to `""`.
      Each valid line in this file (which should have a CSV-like format)
      corresponds to a valid word ID. IDs are in sequential order, starting from
      num_reserved_ids. The last entry in each line is expected to be a value
      corresponding to the count or relative probability. Exactly one of vocab_file
      and unigrams needs to be passed to this op.
    distortion: An optional `float`. Defaults to `1`.
      The distortion is used to skew the unigram probability distribution.
      Each weight is first raised to the distortion's power before adding to the
      internal unigram distribution. As a result, distortion = 1.0 gives regular
      unigram sampling (as defined by the vocab file), and distortion = 0.0 gives
      a uniform distribution.
    num_reserved_ids: An optional `int`. Defaults to `0`.
      Optionally some reserved IDs can be added in the range [0,
      ..., num_reserved_ids) by the users. One use case is that a special unknown
      word token is used as ID 0. These IDs will have a sampling probability of 0.
    num_shards: An optional `int` that is `>= 1`. Defaults to `1`.
      A sampler can be used to sample from a subset of the original range
      in order to speed up the whole computation through parallelism. This parameter
      (together with 'shard') indicates the number of partitions that are being
      used in the overall computation.
    shard: An optional `int` that is `>= 0`. Defaults to `0`.
      A sampler can be used to sample from a subset of the original range
      in order to speed up the whole computation through parallelism. This parameter
      (together with 'num_shards') indicates the particular partition number of a
      sampler op, when partitioning is being used.
    unigrams: An optional list of `floats`. Defaults to `[]`.
      A list of unigram counts or probabilities, one per ID in sequential
      order. Exactly one of vocab_file and unigrams should be passed to this op.
    seed: An optional `int`. Defaults to `0`.
      If either seed or seed2 are set to be non-zero, the random number
      generator is seeded by the given seed.  Otherwise, it is seeded by a
      random seed.
    seed2: An optional `int`. Defaults to `0`.
      An second seed to avoid seed collision.
    name: A name for the operation (optional).

  Returns:
    A tuple of `Tensor` objects (sampled_candidates, true_expected_count, sampled_expected_count).

    sampled_candidates: A `Tensor` of type `int64`.
    true_expected_count: A `Tensor` of type `float32`.
    sampled_expected_count: A `Tensor` of type `float32`.
  """
    _ctx = _context._context or _context.context()
    tld = _ctx._thread_local_data
    if tld.is_eager:
        try:
            _result = pywrap_tfe.TFE_Py_FastPathExecute(
                _ctx._context_handle, tld.device_name,
                "FixedUnigramCandidateSampler", name, tld.op_callbacks,
                true_classes, "num_true", num_true, "num_sampled", num_sampled,
                "unique", unique, "range_max", range_max, "vocab_file",
                vocab_file, "distortion", distortion, "num_reserved_ids",
                num_reserved_ids, "num_shards", num_shards, "shard", shard,
                "unigrams", unigrams, "seed", seed, "seed2", seed2)
            _result = _FixedUnigramCandidateSamplerOutput._make(_result)
            return _result
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
        except _core._FallbackException:
            pass
        try:
            return fixed_unigram_candidate_sampler_eager_fallback(
                true_classes,
                num_true=num_true,
                num_sampled=num_sampled,
                unique=unique,
                range_max=range_max,
                vocab_file=vocab_file,
                distortion=distortion,
                num_reserved_ids=num_reserved_ids,
                num_shards=num_shards,
                shard=shard,
                unigrams=unigrams,
                seed=seed,
                seed2=seed2,
                name=name,
                ctx=_ctx)
        except _core._SymbolicException:
            pass  # Add nodes to the TensorFlow graph.
    # Add nodes to the TensorFlow graph.
    num_true = _execute.make_int(num_true, "num_true")
    num_sampled = _execute.make_int(num_sampled, "num_sampled")
    unique = _execute.make_bool(unique, "unique")
    range_max = _execute.make_int(range_max, "range_max")
    if vocab_file is None:
        vocab_file = ""
    vocab_file = _execute.make_str(vocab_file, "vocab_file")
    if distortion is None:
        distortion = 1
    distortion = _execute.make_float(distortion, "distortion")
    if num_reserved_ids is None:
        num_reserved_ids = 0
    num_reserved_ids = _execute.make_int(num_reserved_ids, "num_reserved_ids")
    if num_shards is None:
        num_shards = 1
    num_shards = _execute.make_int(num_shards, "num_shards")
    if shard is None:
        shard = 0
    shard = _execute.make_int(shard, "shard")
    if unigrams is None:
        unigrams = []
    if not isinstance(unigrams, (list, tuple)):
        raise TypeError("Expected list for 'unigrams' argument to "
                        "'fixed_unigram_candidate_sampler' Op, not %r." %
                        unigrams)
    unigrams = [_execute.make_float(_f, "unigrams") for _f in unigrams]
    if seed is None:
        seed = 0
    seed = _execute.make_int(seed, "seed")
    if seed2 is None:
        seed2 = 0
    seed2 = _execute.make_int(seed2, "seed2")
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "FixedUnigramCandidateSampler",
        true_classes=true_classes,
        num_true=num_true,
        num_sampled=num_sampled,
        unique=unique,
        range_max=range_max,
        vocab_file=vocab_file,
        distortion=distortion,
        num_reserved_ids=num_reserved_ids,
        num_shards=num_shards,
        shard=shard,
        unigrams=unigrams,
        seed=seed,
        seed2=seed2,
        name=name)
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("num_true", _op._get_attr_int("num_true"), "num_sampled",
                  _op._get_attr_int("num_sampled"), "unique",
                  _op._get_attr_bool("unique"), "range_max",
                  _op._get_attr_int("range_max"), "vocab_file",
                  _op.get_attr("vocab_file"), "distortion",
                  _op.get_attr("distortion"), "num_reserved_ids",
                  _op._get_attr_int("num_reserved_ids"), "num_shards",
                  _op._get_attr_int("num_shards"), "shard",
                  _op._get_attr_int("shard"), "unigrams",
                  _op.get_attr("unigrams"), "seed", _op._get_attr_int("seed"),
                  "seed2", _op._get_attr_int("seed2"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("FixedUnigramCandidateSampler", _inputs_flat,
                                 _attrs, _result)
    _result = _FixedUnigramCandidateSamplerOutput._make(_result)
    return _result
def generate_vocab_remapping(new_vocab_file,
                             old_vocab_file,
                             new_vocab_offset,
                             num_new_vocab,
                             old_vocab_size=-1,
                             name=None):
    r"""Given a path to new and old vocabulary files, returns a remapping Tensor of

  length `num_new_vocab`, where `remapping[i]` contains the row number in the old
  vocabulary that corresponds to row `i` in the new vocabulary (starting at line
  `new_vocab_offset` and up to `num_new_vocab` entities), or `-1` if entry `i`
  in the new vocabulary is not in the old vocabulary.  The old vocabulary is
  constrained to the first `old_vocab_size` entries if `old_vocab_size` is not the
  default value of -1.

  `num_vocab_offset` enables
  use in the partitioned variable case, and should generally be set through
  examining partitioning info.  The format of the files should be a text file,
  with each line containing a single entity within the vocabulary.

  For example, with `new_vocab_file` a text file containing each of the following
  elements on a single line: `[f0, f1, f2, f3]`, old_vocab_file = [f1, f0, f3],
  `num_new_vocab = 3, new_vocab_offset = 1`, the returned remapping would be
  `[0, -1, 2]`.

  The op also returns a count of how many entries in the new vocabulary
  were present in the old vocabulary, which is used to calculate the number of
  values to initialize in a weight matrix remapping

  This functionality can be used to remap both row vocabularies (typically,
  features) and column vocabularies (typically, classes) from TensorFlow
  checkpoints.  Note that the partitioning logic relies on contiguous vocabularies
  corresponding to div-partitioned variables.  Moreover, the underlying remapping
  uses an IndexTable (as opposed to an inexact CuckooTable), so client code should
  use the corresponding index_table_from_file() as the FeatureColumn framework
  does (as opposed to tf.feature_to_id(), which uses a CuckooTable).

  Args:
    new_vocab_file: A `Tensor` of type `string`. Path to the new vocab file.
    old_vocab_file: A `Tensor` of type `string`. Path to the old vocab file.
    new_vocab_offset: An `int` that is `>= 0`.
      How many entries into the new vocab file to start reading.
    num_new_vocab: An `int` that is `>= 0`.
      Number of entries in the new vocab file to remap.
    old_vocab_size: An optional `int` that is `>= -1`. Defaults to `-1`.
      Number of entries in the old vocab file to consider.  If -1,
      use the entire old vocabulary.
    name: A name for the operation (optional).

  Returns:
    A tuple of `Tensor` objects (remapping, num_present).

    remapping: A `Tensor` of type `int64`.
    num_present: A `Tensor` of type `int32`.
  """
    _ctx = _context._context or _context.context()
    tld = _ctx._thread_local_data
    if tld.is_eager:
        try:
            _result = pywrap_tfe.TFE_Py_FastPathExecute(
                _ctx._context_handle, tld.device_name,
                "GenerateVocabRemapping", name, tld.op_callbacks,
                new_vocab_file, old_vocab_file, "new_vocab_offset",
                new_vocab_offset, "num_new_vocab", num_new_vocab,
                "old_vocab_size", old_vocab_size)
            _result = _GenerateVocabRemappingOutput._make(_result)
            return _result
        except _core._FallbackException:
            try:
                return generate_vocab_remapping_eager_fallback(
                    new_vocab_file,
                    old_vocab_file,
                    new_vocab_offset=new_vocab_offset,
                    num_new_vocab=num_new_vocab,
                    old_vocab_size=old_vocab_size,
                    name=name,
                    ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
    # Add nodes to the TensorFlow graph.
    new_vocab_offset = _execute.make_int(new_vocab_offset, "new_vocab_offset")
    num_new_vocab = _execute.make_int(num_new_vocab, "num_new_vocab")
    if old_vocab_size is None:
        old_vocab_size = -1
    old_vocab_size = _execute.make_int(old_vocab_size, "old_vocab_size")
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "GenerateVocabRemapping",
        new_vocab_file=new_vocab_file,
        old_vocab_file=old_vocab_file,
        new_vocab_offset=new_vocab_offset,
        num_new_vocab=num_new_vocab,
        old_vocab_size=old_vocab_size,
        name=name)
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("new_vocab_offset", _op._get_attr_int("new_vocab_offset"),
                  "num_new_vocab", _op._get_attr_int("num_new_vocab"),
                  "old_vocab_size", _op._get_attr_int("old_vocab_size"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("GenerateVocabRemapping", _inputs_flat,
                                 _attrs, _result)
    _result = _GenerateVocabRemappingOutput._make(_result)
    return _result
def load_and_remap_matrix(ckpt_path,
                          old_tensor_name,
                          row_remapping,
                          col_remapping,
                          initializing_values,
                          num_rows,
                          num_cols,
                          max_rows_in_memory=-1,
                          name=None):
    r"""Loads a 2-D (matrix) `Tensor` with name `old_tensor_name` from the checkpoint

  at `ckpt_path` and potentially reorders its rows and columns using the
  specified remappings.

  Most users should use one of the wrapper initializers (such as
  `tf.contrib.framework.load_and_remap_matrix_initializer`) instead of this
  function directly.

  The remappings are 1-D tensors with the following properties:

  * `row_remapping` must have exactly `num_rows` entries. Row `i` of the output
    matrix will be initialized from the row corresponding to index
    `row_remapping[i]` in the old `Tensor` from the checkpoint.
  * `col_remapping` must have either 0 entries (indicating that no column
    reordering is needed) or `num_cols` entries. If specified, column `j` of the
    output matrix will be initialized from the column corresponding to index
    `col_remapping[j]` in the old `Tensor` from the checkpoint.
  * A value of -1 in either of the remappings signifies a "missing" entry. In that
    case, values from the `initializing_values` tensor will be used to fill that
    missing row or column. If `row_remapping` has `r` missing entries and
    `col_remapping` has `c` missing entries, then the following condition must be
    true:

  `(r * num_cols) + (c * num_rows) - (r * c) == len(initializing_values)`

  The remapping tensors can be generated using the GenerateVocabRemapping op.

  As an example, with row_remapping = [1, 0, -1], col_remapping = [0, 2, -1],
  initializing_values = [0.5, -0.5, 0.25, -0.25, 42], and w(i, j) representing
  the value from row i, column j of the old tensor in the checkpoint, the output
  matrix will look like the following:

  [[w(1, 0),  w(1, 2),  0.5],
   [w(0, 0),  w(0, 2), -0.5],
   [0.25,    -0.25,      42]]

  Args:
    ckpt_path: A `Tensor` of type `string`.
      Path to the TensorFlow checkpoint (version 2, `TensorBundle`) from
      which the old matrix `Tensor` will be loaded.
    old_tensor_name: A `Tensor` of type `string`.
      Name of the 2-D `Tensor` to load from checkpoint.
    row_remapping: A `Tensor` of type `int64`.
      An int `Tensor` of row remappings (generally created by
      `generate_vocab_remapping`).  Even if no row remapping is needed, this must
      still be an index-valued Tensor (e.g. [0, 1, 2, ...]), or a shifted
      index-valued `Tensor` (e.g. [8, 9, 10, ...], for partitioned `Variables`).
    col_remapping: A `Tensor` of type `int64`.
      An int `Tensor` of column remappings (generally created by
      `generate_vocab_remapping`).  May be a size-0 `Tensor` if only row remapping
      is to be done (e.g. column ordering is the same).
    initializing_values: A `Tensor` of type `float32`.
      A float `Tensor` containing  values to fill in for cells
      in the output matrix that are not loaded from the checkpoint. Length must be
      exactly the same as the number of missing / new cells.
    num_rows: An `int` that is `>= 0`.
      Number of rows (length of the 1st dimension) in the output matrix.
    num_cols: An `int` that is `>= 1`.
      Number of columns (length of the 2nd dimension) in the output matrix.
    max_rows_in_memory: An optional `int`. Defaults to `-1`.
      The maximum number of rows to load from the checkpoint at
      once. If less than or equal to 0, the entire matrix will be loaded into
      memory. Setting this arg trades increased disk reads for lower memory usage.
    name: A name for the operation (optional).

  Returns:
    A `Tensor` of type `float32`.
  """
    _ctx = _context._context or _context.context()
    tld = _ctx._thread_local_data
    if tld.is_eager:
        try:
            _result = pywrap_tfe.TFE_Py_FastPathExecute(
                _ctx._context_handle, tld.device_name, "LoadAndRemapMatrix",
                name, tld.op_callbacks, ckpt_path, old_tensor_name,
                row_remapping, col_remapping, initializing_values, "num_rows",
                num_rows, "num_cols", num_cols, "max_rows_in_memory",
                max_rows_in_memory)
            return _result
        except _core._FallbackException:
            try:
                return load_and_remap_matrix_eager_fallback(
                    ckpt_path,
                    old_tensor_name,
                    row_remapping,
                    col_remapping,
                    initializing_values,
                    num_rows=num_rows,
                    num_cols=num_cols,
                    max_rows_in_memory=max_rows_in_memory,
                    name=name,
                    ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
    # Add nodes to the TensorFlow graph.
    num_rows = _execute.make_int(num_rows, "num_rows")
    num_cols = _execute.make_int(num_cols, "num_cols")
    if max_rows_in_memory is None:
        max_rows_in_memory = -1
    max_rows_in_memory = _execute.make_int(max_rows_in_memory,
                                           "max_rows_in_memory")
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "LoadAndRemapMatrix",
        ckpt_path=ckpt_path,
        old_tensor_name=old_tensor_name,
        row_remapping=row_remapping,
        col_remapping=col_remapping,
        initializing_values=initializing_values,
        num_rows=num_rows,
        num_cols=num_cols,
        max_rows_in_memory=max_rows_in_memory,
        name=name)
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("num_rows", _op._get_attr_int("num_rows"), "num_cols",
                  _op._get_attr_int("num_cols"), "max_rows_in_memory",
                  _op._get_attr_int("max_rows_in_memory"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("LoadAndRemapMatrix", _inputs_flat, _attrs,
                                 _result)
    _result, = _result
    return _result
Пример #14
0
def enter(data,
          frame_name,
          is_constant=False,
          parallel_iterations=10,
          name=None):
    r"""Creates or finds a child frame, and makes `data` available to the child frame.

  This op is used together with `Exit` to create loops in the graph.
  The unique `frame_name` is used by the `Executor` to identify frames. If
  `is_constant` is true, `output` is a constant in the child frame; otherwise
  it may be changed in the child frame. At most `parallel_iterations` iterations
  are run in parallel in the child frame.

  Args:
    data: A `Tensor`. The tensor to be made available to the child frame.
    frame_name: A `string`. The name of the child frame.
    is_constant: An optional `bool`. Defaults to `False`.
      If true, the output is constant within the child frame.
    parallel_iterations: An optional `int`. Defaults to `10`.
      The number of iterations allowed to run in parallel.
    name: A name for the operation (optional).

  Returns:
    A `Tensor`. Has the same type as `data`.
  """
    _ctx = _context._context or _context.context()
    tld = _ctx._thread_local_data
    if tld.is_eager:
        try:
            _result = pywrap_tfe.TFE_Py_FastPathExecute(
                _ctx, "Enter", name, data, "frame_name", frame_name,
                "is_constant", is_constant, "parallel_iterations",
                parallel_iterations)
            return _result
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
        except _core._FallbackException:
            pass
        try:
            return enter_eager_fallback(
                data,
                frame_name=frame_name,
                is_constant=is_constant,
                parallel_iterations=parallel_iterations,
                name=name,
                ctx=_ctx)
        except _core._SymbolicException:
            pass  # Add nodes to the TensorFlow graph.
    # Add nodes to the TensorFlow graph.
    frame_name = _execute.make_str(frame_name, "frame_name")
    if is_constant is None:
        is_constant = False
    is_constant = _execute.make_bool(is_constant, "is_constant")
    if parallel_iterations is None:
        parallel_iterations = 10
    parallel_iterations = _execute.make_int(parallel_iterations,
                                            "parallel_iterations")
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "Enter",
        data=data,
        frame_name=frame_name,
        is_constant=is_constant,
        parallel_iterations=parallel_iterations,
        name=name)
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("T", _op._get_attr_type("T"), "frame_name",
                  _op.get_attr("frame_name"), "is_constant",
                  _op._get_attr_bool("is_constant"), "parallel_iterations",
                  _op._get_attr_int("parallel_iterations"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("Enter", _inputs_flat, _attrs, _result)
    _result, = _result
    return _result
Пример #15
0
def decode_wav(contents, desired_channels=-1, desired_samples=-1, name=None):
    r"""Decode a 16-bit PCM WAV file to a float tensor.

  The -32768 to 32767 signed 16-bit values will be scaled to -1.0 to 1.0 in float.

  When desired_channels is set, if the input contains fewer channels than this
  then the last channel will be duplicated to give the requested number, else if
  the input has more channels than requested then the additional channels will be
  ignored.

  If desired_samples is set, then the audio will be cropped or padded with zeroes
  to the requested length.

  The first output contains a Tensor with the content of the audio samples. The
  lowest dimension will be the number of channels, and the second will be the
  number of samples. For example, a ten-sample-long stereo WAV file should give an
  output shape of [10, 2].

  Args:
    contents: A `Tensor` of type `string`.
      The WAV-encoded audio, usually from a file.
    desired_channels: An optional `int`. Defaults to `-1`.
      Number of sample channels wanted.
    desired_samples: An optional `int`. Defaults to `-1`.
      Length of audio requested.
    name: A name for the operation (optional).

  Returns:
    A tuple of `Tensor` objects (audio, sample_rate).

    audio: A `Tensor` of type `float32`.
    sample_rate: A `Tensor` of type `int32`.
  """
    _ctx = _context._context or _context.context()
    tld = _ctx._thread_local_data
    if tld.is_eager:
        try:
            _result = pywrap_tfe.TFE_Py_FastPathExecute(
                _ctx, "DecodeWav", name, contents, "desired_channels",
                desired_channels, "desired_samples", desired_samples)
            _result = _DecodeWavOutput._make(_result)
            return _result
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
        except _core._FallbackException:
            pass
        try:
            return decode_wav_eager_fallback(contents,
                                             desired_channels=desired_channels,
                                             desired_samples=desired_samples,
                                             name=name,
                                             ctx=_ctx)
        except _core._SymbolicException:
            pass  # Add nodes to the TensorFlow graph.
        except (TypeError, ValueError):
            result = _dispatch.dispatch(
                decode_wav, (),
                dict(contents=contents,
                     desired_channels=desired_channels,
                     desired_samples=desired_samples,
                     name=name))
            if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
                return result
            raise
    # Add nodes to the TensorFlow graph.
    if desired_channels is None:
        desired_channels = -1
    desired_channels = _execute.make_int(desired_channels, "desired_channels")
    if desired_samples is None:
        desired_samples = -1
    desired_samples = _execute.make_int(desired_samples, "desired_samples")
    try:
        _, _, _op, _outputs = _op_def_library._apply_op_helper(
            "DecodeWav",
            contents=contents,
            desired_channels=desired_channels,
            desired_samples=desired_samples,
            name=name)
    except (TypeError, ValueError):
        result = _dispatch.dispatch(
            decode_wav, (),
            dict(contents=contents,
                 desired_channels=desired_channels,
                 desired_samples=desired_samples,
                 name=name))
        if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
            return result
        raise
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("desired_channels", _op._get_attr_int("desired_channels"),
                  "desired_samples", _op._get_attr_int("desired_samples"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("DecodeWav", _inputs_flat, _attrs, _result)
    _result = _DecodeWavOutput._make(_result)
    return _result
Пример #16
0
def sparse_to_sparse_set_operation(set1_indices,
                                   set1_values,
                                   set1_shape,
                                   set2_indices,
                                   set2_values,
                                   set2_shape,
                                   set_operation,
                                   validate_indices=True,
                                   name=None):
    r"""Applies set operation along last dimension of 2 `SparseTensor` inputs.

  See SetOperationOp::SetOperationFromContext for values of `set_operation`.

  If `validate_indices` is `True`, `SparseToSparseSetOperation` validates the
  order and range of `set1` and `set2` indices.

  Input `set1` is a `SparseTensor` represented by `set1_indices`, `set1_values`,
  and `set1_shape`. For `set1` ranked `n`, 1st `n-1` dimensions must be the same
  as `set2`. Dimension `n` contains values in a set, duplicates are allowed but
  ignored.

  Input `set2` is a `SparseTensor` represented by `set2_indices`, `set2_values`,
  and `set2_shape`. For `set2` ranked `n`, 1st `n-1` dimensions must be the same
  as `set1`. Dimension `n` contains values in a set, duplicates are allowed but
  ignored.

  If `validate_indices` is `True`, this op validates the order and range of `set1`
  and `set2` indices.

  Output `result` is a `SparseTensor` represented by `result_indices`,
  `result_values`, and `result_shape`. For `set1` and `set2` ranked `n`, this
  has rank `n` and the same 1st `n-1` dimensions as `set1` and `set2`. The `nth`
  dimension contains the result of `set_operation` applied to the corresponding
  `[0...n-1]` dimension of `set`.

  Args:
    set1_indices: A `Tensor` of type `int64`.
      2D `Tensor`, indices of a `SparseTensor`. Must be in row-major
      order.
    set1_values: A `Tensor`. Must be one of the following types: `int8`, `int16`, `int32`, `int64`, `uint8`, `uint16`, `string`.
      1D `Tensor`, values of a `SparseTensor`. Must be in row-major
      order.
    set1_shape: A `Tensor` of type `int64`.
      1D `Tensor`, shape of a `SparseTensor`. `set1_shape[0...n-1]` must
      be the same as `set2_shape[0...n-1]`, `set1_shape[n]` is the
      max set size across `0...n-1` dimensions.
    set2_indices: A `Tensor` of type `int64`.
      2D `Tensor`, indices of a `SparseTensor`. Must be in row-major
      order.
    set2_values: A `Tensor`. Must have the same type as `set1_values`.
      1D `Tensor`, values of a `SparseTensor`. Must be in row-major
      order.
    set2_shape: A `Tensor` of type `int64`.
      1D `Tensor`, shape of a `SparseTensor`. `set2_shape[0...n-1]` must
      be the same as `set1_shape[0...n-1]`, `set2_shape[n]` is the
      max set size across `0...n-1` dimensions.
    set_operation: A `string`.
    validate_indices: An optional `bool`. Defaults to `True`.
    name: A name for the operation (optional).

  Returns:
    A tuple of `Tensor` objects (result_indices, result_values, result_shape).

    result_indices: A `Tensor` of type `int64`.
    result_values: A `Tensor`. Has the same type as `set1_values`.
    result_shape: A `Tensor` of type `int64`.
  """
    _ctx = _context._context or _context.context()
    tld = _ctx._thread_local_data
    if tld.is_eager:
        try:
            _result = pywrap_tfe.TFE_Py_FastPathExecute(
                _ctx._context_handle, tld.device_name,
                "SparseToSparseSetOperation", name, tld.op_callbacks,
                set1_indices, set1_values, set1_shape, set2_indices,
                set2_values, set2_shape, "set_operation", set_operation,
                "validate_indices", validate_indices)
            _result = _SparseToSparseSetOperationOutput._make(_result)
            return _result
        except _core._FallbackException:
            try:
                return sparse_to_sparse_set_operation_eager_fallback(
                    set1_indices,
                    set1_values,
                    set1_shape,
                    set2_indices,
                    set2_values,
                    set2_shape,
                    set_operation=set_operation,
                    validate_indices=validate_indices,
                    name=name,
                    ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
    # Add nodes to the TensorFlow graph.
    set_operation = _execute.make_str(set_operation, "set_operation")
    if validate_indices is None:
        validate_indices = True
    validate_indices = _execute.make_bool(validate_indices, "validate_indices")
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "SparseToSparseSetOperation",
        set1_indices=set1_indices,
        set1_values=set1_values,
        set1_shape=set1_shape,
        set2_indices=set2_indices,
        set2_values=set2_values,
        set2_shape=set2_shape,
        set_operation=set_operation,
        validate_indices=validate_indices,
        name=name)
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("set_operation", _op.get_attr("set_operation"),
                  "validate_indices", _op._get_attr_bool("validate_indices"),
                  "T", _op._get_attr_type("T"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("SparseToSparseSetOperation", _inputs_flat,
                                 _attrs, _result)
    _result = _SparseToSparseSetOperationOutput._make(_result)
    return _result
Пример #17
0
def audio_spectrogram(input,
                      window_size,
                      stride,
                      magnitude_squared=False,
                      name=None):
    r"""Produces a visualization of audio data over time.

  Spectrograms are a standard way of representing audio information as a series of
  slices of frequency information, one slice for each window of time. By joining
  these together into a sequence, they form a distinctive fingerprint of the sound
  over time.

  This op expects to receive audio data as an input, stored as floats in the range
  -1 to 1, together with a window width in samples, and a stride specifying how
  far to move the window between slices. From this it generates a three
  dimensional output. The first dimension is for the channels in the input, so a
  stereo audio input would have two here for example. The second dimension is time,
  with successive frequency slices. The third dimension has an amplitude value for
  each frequency during that time slice.

  This means the layout when converted and saved as an image is rotated 90 degrees
  clockwise from a typical spectrogram. Time is descending down the Y axis, and
  the frequency decreases from left to right.

  Each value in the result represents the square root of the sum of the real and
  imaginary parts of an FFT on the current window of samples. In this way, the
  lowest dimension represents the power of each frequency in the current window,
  and adjacent windows are concatenated in the next dimension.

  To get a more intuitive and visual look at what this operation does, you can run
  tensorflow/examples/wav_to_spectrogram to read in an audio file and save out the
  resulting spectrogram as a PNG image.

  Args:
    input: A `Tensor` of type `float32`. Float representation of audio data.
    window_size: An `int`.
      How wide the input window is in samples. For the highest efficiency
      this should be a power of two, but other values are accepted.
    stride: An `int`.
      How widely apart the center of adjacent sample windows should be.
    magnitude_squared: An optional `bool`. Defaults to `False`.
      Whether to return the squared magnitude or just the
      magnitude. Using squared magnitude can avoid extra calculations.
    name: A name for the operation (optional).

  Returns:
    A `Tensor` of type `float32`.
  """
    _ctx = _context._context or _context.context()
    tld = _ctx._thread_local_data
    if tld.is_eager:
        try:
            _result = pywrap_tfe.TFE_Py_FastPathExecute(
                _ctx, "AudioSpectrogram", name, input, "window_size",
                window_size, "stride", stride, "magnitude_squared",
                magnitude_squared)
            return _result
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
        except _core._FallbackException:
            pass
        try:
            return audio_spectrogram_eager_fallback(
                input,
                window_size=window_size,
                stride=stride,
                magnitude_squared=magnitude_squared,
                name=name,
                ctx=_ctx)
        except _core._SymbolicException:
            pass  # Add nodes to the TensorFlow graph.
    # Add nodes to the TensorFlow graph.
    window_size = _execute.make_int(window_size, "window_size")
    stride = _execute.make_int(stride, "stride")
    if magnitude_squared is None:
        magnitude_squared = False
    magnitude_squared = _execute.make_bool(magnitude_squared,
                                           "magnitude_squared")
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "AudioSpectrogram",
        input=input,
        window_size=window_size,
        stride=stride,
        magnitude_squared=magnitude_squared,
        name=name)
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("window_size", _op._get_attr_int("window_size"), "stride",
                  _op._get_attr_int("stride"), "magnitude_squared",
                  _op._get_attr_bool("magnitude_squared"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("AudioSpectrogram", _inputs_flat, _attrs,
                                 _result)
    _result, = _result
    return _result
Пример #18
0
def sparse_count_sparse_output(indices,
                               values,
                               dense_shape,
                               weights,
                               binary_output,
                               minlength=-1,
                               maxlength=-1,
                               name=None):
    r"""Performs sparse-output bin counting for a sparse tensor input.

    Counts the number of times each value occurs in the input.

  Args:
    indices: A `Tensor` of type `int64`.
      Tensor containing the indices of the sparse tensor to count.
    values: A `Tensor`. Must be one of the following types: `int32`, `int64`.
      Tensor containing values of the sparse tensor to count.
    dense_shape: A `Tensor` of type `int64`.
      Tensor containing the dense shape of the sparse tensor to count.
    weights: A `Tensor`. Must be one of the following types: `int32`, `int64`, `float32`, `float64`.
      A Tensor of the same shape as indices containing per-index weight values.
      May also be the empty tensor if no weights are used.
    binary_output: A `bool`.
      Whether to output the number of occurrences of each value or 1.
    minlength: An optional `int` that is `>= -1`. Defaults to `-1`.
      Minimum value to count. Can be set to -1 for no minimum.
    maxlength: An optional `int` that is `>= -1`. Defaults to `-1`.
      Maximum value to count. Can be set to -1 for no maximum.
    name: A name for the operation (optional).

  Returns:
    A tuple of `Tensor` objects (output_indices, output_values, output_dense_shape).

    output_indices: A `Tensor` of type `int64`.
    output_values: A `Tensor`. Has the same type as `weights`.
    output_dense_shape: A `Tensor` of type `int64`.
  """
    _ctx = _context._context or _context.context()
    tld = _ctx._thread_local_data
    if tld.is_eager:
        try:
            _result = pywrap_tfe.TFE_Py_FastPathExecute(
                _ctx, "SparseCountSparseOutput", name, indices, values,
                dense_shape, weights, "minlength", minlength, "maxlength",
                maxlength, "binary_output", binary_output)
            _result = _SparseCountSparseOutputOutput._make(_result)
            return _result
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
        except _core._FallbackException:
            pass
        try:
            return sparse_count_sparse_output_eager_fallback(
                indices,
                values,
                dense_shape,
                weights,
                minlength=minlength,
                maxlength=maxlength,
                binary_output=binary_output,
                name=name,
                ctx=_ctx)
        except _core._SymbolicException:
            pass  # Add nodes to the TensorFlow graph.
    # Add nodes to the TensorFlow graph.
    binary_output = _execute.make_bool(binary_output, "binary_output")
    if minlength is None:
        minlength = -1
    minlength = _execute.make_int(minlength, "minlength")
    if maxlength is None:
        maxlength = -1
    maxlength = _execute.make_int(maxlength, "maxlength")
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "SparseCountSparseOutput",
        indices=indices,
        values=values,
        dense_shape=dense_shape,
        weights=weights,
        binary_output=binary_output,
        minlength=minlength,
        maxlength=maxlength,
        name=name)
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("T", _op._get_attr_type("T"), "minlength",
                  _op._get_attr_int("minlength"), "maxlength",
                  _op._get_attr_int("maxlength"), "binary_output",
                  _op._get_attr_bool("binary_output"), "output_type",
                  _op._get_attr_type("output_type"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("SparseCountSparseOutput", _inputs_flat,
                                 _attrs, _result)
    _result = _SparseCountSparseOutputOutput._make(_result)
    return _result
Пример #19
0
def mfcc(spectrogram,
         sample_rate,
         upper_frequency_limit=4000,
         lower_frequency_limit=20,
         filterbank_channel_count=40,
         dct_coefficient_count=13,
         name=None):
    r"""Transforms a spectrogram into a form that's useful for speech recognition.

  Mel Frequency Cepstral Coefficients are a way of representing audio data that's
  been effective as an input feature for machine learning. They are created by
  taking the spectrum of a spectrogram (a 'cepstrum'), and discarding some of the
  higher frequencies that are less significant to the human ear. They have a long
  history in the speech recognition world, and https://en.wikipedia.org/wiki/Mel-frequency_cepstrum
  is a good resource to learn more.

  Args:
    spectrogram: A `Tensor` of type `float32`.
      Typically produced by the Spectrogram op, with magnitude_squared
      set to true.
    sample_rate: A `Tensor` of type `int32`.
      How many samples per second the source audio used.
    upper_frequency_limit: An optional `float`. Defaults to `4000`.
      The highest frequency to use when calculating the
      ceptstrum.
    lower_frequency_limit: An optional `float`. Defaults to `20`.
      The lowest frequency to use when calculating the
      ceptstrum.
    filterbank_channel_count: An optional `int`. Defaults to `40`.
      Resolution of the Mel bank used internally.
    dct_coefficient_count: An optional `int`. Defaults to `13`.
      How many output channels to produce per time slice.
    name: A name for the operation (optional).

  Returns:
    A `Tensor` of type `float32`.
  """
    _ctx = _context._context or _context.context()
    tld = _ctx._thread_local_data
    if tld.is_eager:
        try:
            _result = pywrap_tfe.TFE_Py_FastPathExecute(
                _ctx, "Mfcc", name, spectrogram, sample_rate,
                "upper_frequency_limit", upper_frequency_limit,
                "lower_frequency_limit", lower_frequency_limit,
                "filterbank_channel_count", filterbank_channel_count,
                "dct_coefficient_count", dct_coefficient_count)
            return _result
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
        except _core._FallbackException:
            pass
        try:
            return mfcc_eager_fallback(
                spectrogram,
                sample_rate,
                upper_frequency_limit=upper_frequency_limit,
                lower_frequency_limit=lower_frequency_limit,
                filterbank_channel_count=filterbank_channel_count,
                dct_coefficient_count=dct_coefficient_count,
                name=name,
                ctx=_ctx)
        except _core._SymbolicException:
            pass  # Add nodes to the TensorFlow graph.
    # Add nodes to the TensorFlow graph.
    if upper_frequency_limit is None:
        upper_frequency_limit = 4000
    upper_frequency_limit = _execute.make_float(upper_frequency_limit,
                                                "upper_frequency_limit")
    if lower_frequency_limit is None:
        lower_frequency_limit = 20
    lower_frequency_limit = _execute.make_float(lower_frequency_limit,
                                                "lower_frequency_limit")
    if filterbank_channel_count is None:
        filterbank_channel_count = 40
    filterbank_channel_count = _execute.make_int(filterbank_channel_count,
                                                 "filterbank_channel_count")
    if dct_coefficient_count is None:
        dct_coefficient_count = 13
    dct_coefficient_count = _execute.make_int(dct_coefficient_count,
                                              "dct_coefficient_count")
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "Mfcc",
        spectrogram=spectrogram,
        sample_rate=sample_rate,
        upper_frequency_limit=upper_frequency_limit,
        lower_frequency_limit=lower_frequency_limit,
        filterbank_channel_count=filterbank_channel_count,
        dct_coefficient_count=dct_coefficient_count,
        name=name)
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("upper_frequency_limit",
                  _op.get_attr("upper_frequency_limit"),
                  "lower_frequency_limit",
                  _op.get_attr("lower_frequency_limit"),
                  "filterbank_channel_count",
                  _op._get_attr_int("filterbank_channel_count"),
                  "dct_coefficient_count",
                  _op._get_attr_int("dct_coefficient_count"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("Mfcc", _inputs_flat, _attrs, _result)
    _result, = _result
    return _result
Пример #20
0
def mlir_passthrough_op(inputs, mlir_module, Toutputs, name=None):
    r"""TODO: add doc.

  Args:
    inputs: A list of `Tensor` objects.
    mlir_module: A `string`.
    Toutputs: A list of `tf.DTypes`.
    name: A name for the operation (optional).

  Returns:
    A list of `Tensor` objects of type `Toutputs`.
  """
    _ctx = _context._context or _context.context()
    tld = _ctx._thread_local_data
    if tld.is_eager:
        try:
            _result = pywrap_tfe.TFE_Py_FastPathExecute(
                _ctx._context_handle, tld.device_name, "MlirPassthroughOp",
                name, tld.op_callbacks, inputs, "mlir_module", mlir_module,
                "Toutputs", Toutputs)
            return _result
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
        except _core._FallbackException:
            pass
        try:
            return mlir_passthrough_op_eager_fallback(inputs,
                                                      mlir_module=mlir_module,
                                                      Toutputs=Toutputs,
                                                      name=name,
                                                      ctx=_ctx)
        except _core._SymbolicException:
            pass  # Add nodes to the TensorFlow graph.
        except (TypeError, ValueError):
            result = _dispatch.dispatch(
                mlir_passthrough_op, (),
                dict(inputs=inputs,
                     mlir_module=mlir_module,
                     Toutputs=Toutputs,
                     name=name))
            if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
                return result
            raise
    # Add nodes to the TensorFlow graph.
    mlir_module = _execute.make_str(mlir_module, "mlir_module")
    if not isinstance(Toutputs, (list, tuple)):
        raise TypeError("Expected list for 'Toutputs' argument to "
                        "'mlir_passthrough_op' Op, not %r." % Toutputs)
    Toutputs = [_execute.make_type(_t, "Toutputs") for _t in Toutputs]
    try:
        _, _, _op, _outputs = _op_def_library._apply_op_helper(
            "MlirPassthroughOp",
            inputs=inputs,
            mlir_module=mlir_module,
            Toutputs=Toutputs,
            name=name)
    except (TypeError, ValueError):
        result = _dispatch.dispatch(
            mlir_passthrough_op, (),
            dict(inputs=inputs,
                 mlir_module=mlir_module,
                 Toutputs=Toutputs,
                 name=name))
        if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
            return result
        raise
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("mlir_module", _op.get_attr("mlir_module"), "Tinputs",
                  _op.get_attr("Tinputs"), "Toutputs",
                  _op.get_attr("Toutputs"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("MlirPassthroughOp", _inputs_flat, _attrs,
                                 _result)
    return _result
Пример #21
0
def ragged_tensor_to_tensor(shape,
                            values,
                            default_value,
                            row_partition_tensors,
                            row_partition_types,
                            name=None):
    r"""Create a dense tensor from a ragged tensor, possibly altering its shape.

  The `ragged_to_dense` op creates a dense tensor from a list of row partition
  tensors, a value vector, and default values. If the shape is unspecified, the
  minimal shape required to contain all the elements in the ragged tensor (the
  natural shape) will be used. If some dimensions are left unspecified, then the
  size of the natural shape is used in that dimension.

  The default_value will be broadcast to the output shape. After that, the values
  from the ragged tensor overwrite the default values. Note that the default_value
  must have less dimensions than the value.

  The row partition tensors are in the order of the dimensions.
  At present, the types can be:
  * "ROW_SPLITS": the row_splits tensor from the ragged tensor.
  * "VALUE_ROWIDS": the value_rowids tensor from the ragged tensor.
  * "FIRST_DIM_SIZE": if value_rowids is used for the first dimension, then it
    is preceded by "FIRST_DIM_SIZE".

  Args:
    shape: A `Tensor`. Must be one of the following types: `int64`, `int32`.
      The desired shape of the the output tensor. If left unspecified (empty),
      the minimal shape required to contain all the elements in the ragged tensor
      (the natural shape) will be used. If some dimensions are left unspecified, then
      the size of the natural shape is used in that dimension.

      Note that dense dimensions cannot be modified by the shape argument. Trying to
      change the size of a dense dimension will cause the op to fail.
      Examples:
      natural shape: [4, 5, 6]
      shape: -1
      output shape: [4, 5, 6]

      natural shape: [4, 5, 6]
      shape: [3, -1, 2]
      output shape: [3, 5, 2]

      natural shape: [4, 5, 6]
      shape: [3, 7, 2]
      output shape: [3, 7, 2]
    values: A `Tensor`.
      A 1D tensor representing the values of the ragged tensor.
    default_value: A `Tensor`. Must have the same type as `values`.
      The default_value when the shape is larger than the ragged tensor. The
      default_value is broadcast until it is the shape of the output tensor, and
      then overwritten by values in the ragged tensor. The default value must be
      compatible with this broadcast operation, and must have fewer dimensions than
      the value tensor.
    row_partition_tensors: A list of at least 1 `Tensor` objects with the same type in: `int64`, `int32`.
    row_partition_types: A list of `strings`.
      The types of the row partition tensors. At present, these can be:
      * "ROW_SPLITS": the row_splits tensor from the ragged tensor.
      * "VALUE_ROWIDS": the value_rowids tensor from the ragged tensor.
      * "FIRST_DIM_SIZE": if value_rowids is used for the first dimension, then it
        is preceeded by "FIRST_DIM_SIZE".
      The tensors are in the order of the dimensions.
    name: A name for the operation (optional).

  Returns:
    A `Tensor`. Has the same type as `values`.
  """
    _ctx = _context._context or _context.context()
    tld = _ctx._thread_local_data
    if tld.is_eager:
        try:
            _result = pywrap_tfe.TFE_Py_FastPathExecute(
                _ctx._context_handle, tld.device_name, "RaggedTensorToTensor",
                name, tld.op_callbacks, shape, values, default_value,
                row_partition_tensors, "row_partition_types",
                row_partition_types)
            return _result
        except _core._FallbackException:
            try:
                return ragged_tensor_to_tensor_eager_fallback(
                    shape,
                    values,
                    default_value,
                    row_partition_tensors,
                    row_partition_types=row_partition_types,
                    name=name,
                    ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
    # Add nodes to the TensorFlow graph.
    if not isinstance(row_partition_tensors, (list, tuple)):
        raise TypeError(
            "Expected list for 'row_partition_tensors' argument to "
            "'ragged_tensor_to_tensor' Op, not %r." % row_partition_tensors)
    _attr_num_row_partition_tensors = len(row_partition_tensors)
    if not isinstance(row_partition_types, (list, tuple)):
        raise TypeError("Expected list for 'row_partition_types' argument to "
                        "'ragged_tensor_to_tensor' Op, not %r." %
                        row_partition_types)
    row_partition_types = [
        _execute.make_str(_s, "row_partition_types")
        for _s in row_partition_types
    ]
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "RaggedTensorToTensor",
        shape=shape,
        values=values,
        default_value=default_value,
        row_partition_tensors=row_partition_tensors,
        row_partition_types=row_partition_types,
        name=name)
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("T", _op._get_attr_type("T"), "Tindex",
                  _op._get_attr_type("Tindex"), "Tshape",
                  _op._get_attr_type("Tshape"), "num_row_partition_tensors",
                  _op._get_attr_int("num_row_partition_tensors"),
                  "row_partition_types", _op.get_attr("row_partition_types"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("RaggedTensorToTensor", _inputs_flat, _attrs,
                                 _result)
    _result, = _result
    return _result
Пример #22
0
def send(tensor,
         tensor_name,
         send_device,
         send_device_incarnation,
         recv_device,
         client_terminated=False,
         name=None):
    r"""Sends the named tensor from send_device to recv_device.

  Args:
    tensor: A `Tensor`. The tensor to send.
    tensor_name: A `string`. The name of the tensor to send.
    send_device: A `string`. The name of the device sending the tensor.
    send_device_incarnation: An `int`. The current incarnation of send_device.
    recv_device: A `string`. The name of the device receiving the tensor.
    client_terminated: An optional `bool`. Defaults to `False`.
      If set to true, this indicates that the node was added
      to the graph as a result of a client-side feed or fetch of Tensor data,
      in which case the corresponding send or recv is expected to be managed
      locally by the caller.
    name: A name for the operation (optional).

  Returns:
    The created Operation.
  """
    _ctx = _context._context or _context.context()
    tld = _ctx._thread_local_data
    if tld.is_eager:
        try:
            _result = pywrap_tfe.TFE_Py_FastPathExecute(
                _ctx, "Send", name, tensor, "tensor_name", tensor_name,
                "send_device", send_device, "send_device_incarnation",
                send_device_incarnation, "recv_device", recv_device,
                "client_terminated", client_terminated)
            return _result
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
        except _core._FallbackException:
            pass
        try:
            return send_eager_fallback(
                tensor,
                tensor_name=tensor_name,
                send_device=send_device,
                send_device_incarnation=send_device_incarnation,
                recv_device=recv_device,
                client_terminated=client_terminated,
                name=name,
                ctx=_ctx)
        except _core._SymbolicException:
            pass  # Add nodes to the TensorFlow graph.
    # Add nodes to the TensorFlow graph.
    tensor_name = _execute.make_str(tensor_name, "tensor_name")
    send_device = _execute.make_str(send_device, "send_device")
    send_device_incarnation = _execute.make_int(send_device_incarnation,
                                                "send_device_incarnation")
    recv_device = _execute.make_str(recv_device, "recv_device")
    if client_terminated is None:
        client_terminated = False
    client_terminated = _execute.make_bool(client_terminated,
                                           "client_terminated")
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "Send",
        tensor=tensor,
        tensor_name=tensor_name,
        send_device=send_device,
        send_device_incarnation=send_device_incarnation,
        recv_device=recv_device,
        client_terminated=client_terminated,
        name=name)
    return _op
Пример #23
0
def recv(tensor_type,
         tensor_name,
         send_device,
         send_device_incarnation,
         recv_device,
         client_terminated=False,
         name=None):
    r"""Receives the named tensor from send_device on recv_device.

  Args:
    tensor_type: A `tf.DType`.
    tensor_name: A `string`. The name of the tensor to receive.
    send_device: A `string`. The name of the device sending the tensor.
    send_device_incarnation: An `int`. The current incarnation of send_device.
    recv_device: A `string`. The name of the device receiving the tensor.
    client_terminated: An optional `bool`. Defaults to `False`.
      If set to true, this indicates that the node was added
      to the graph as a result of a client-side feed or fetch of Tensor data,
      in which case the corresponding send or recv is expected to be managed
      locally by the caller.
    name: A name for the operation (optional).

  Returns:
    A `Tensor` of type `tensor_type`.
  """
    _ctx = _context._context or _context.context()
    tld = _ctx._thread_local_data
    if tld.is_eager:
        try:
            _result = pywrap_tfe.TFE_Py_FastPathExecute(
                _ctx._context_handle, tld.device_name, "Recv", name,
                tld.op_callbacks, "tensor_type", tensor_type, "tensor_name",
                tensor_name, "send_device", send_device,
                "send_device_incarnation", send_device_incarnation,
                "recv_device", recv_device, "client_terminated",
                client_terminated)
            return _result
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
        except _core._FallbackException:
            pass
        try:
            return recv_eager_fallback(
                tensor_type=tensor_type,
                tensor_name=tensor_name,
                send_device=send_device,
                send_device_incarnation=send_device_incarnation,
                recv_device=recv_device,
                client_terminated=client_terminated,
                name=name,
                ctx=_ctx)
        except _core._SymbolicException:
            pass  # Add nodes to the TensorFlow graph.
    # Add nodes to the TensorFlow graph.
    tensor_type = _execute.make_type(tensor_type, "tensor_type")
    tensor_name = _execute.make_str(tensor_name, "tensor_name")
    send_device = _execute.make_str(send_device, "send_device")
    send_device_incarnation = _execute.make_int(send_device_incarnation,
                                                "send_device_incarnation")
    recv_device = _execute.make_str(recv_device, "recv_device")
    if client_terminated is None:
        client_terminated = False
    client_terminated = _execute.make_bool(client_terminated,
                                           "client_terminated")
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "Recv",
        tensor_type=tensor_type,
        tensor_name=tensor_name,
        send_device=send_device,
        send_device_incarnation=send_device_incarnation,
        recv_device=recv_device,
        client_terminated=client_terminated,
        name=name)
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("tensor_type", _op._get_attr_type("tensor_type"),
                  "tensor_name", _op.get_attr("tensor_name"), "send_device",
                  _op.get_attr("send_device"), "send_device_incarnation",
                  _op._get_attr_int("send_device_incarnation"), "recv_device",
                  _op.get_attr("recv_device"), "client_terminated",
                  _op._get_attr_bool("client_terminated"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("Recv", _inputs_flat, _attrs, _result)
    _result, = _result
    return _result
Пример #24
0
def ctc_greedy_decoder(inputs,
                       sequence_length,
                       merge_repeated=False,
                       name=None):
    r"""Performs greedy decoding on the logits given in inputs.

  A note about the attribute merge_repeated: if enabled, when
  consecutive logits' maximum indices are the same, only the first of
  these is emitted.  Labeling the blank '*', the sequence "A B B * B B"
  becomes "A B B" if merge_repeated = True and "A B B B B" if
  merge_repeated = False.

  Regardless of the value of merge_repeated, if the maximum index of a given
  time and batch corresponds to the blank, index `(num_classes - 1)`, no new
  element is emitted.

  Args:
    inputs: A `Tensor`. Must be one of the following types: `float32`, `float64`.
      3-D, shape: `(max_time x batch_size x num_classes)`, the logits.
    sequence_length: A `Tensor` of type `int32`.
      A vector containing sequence lengths, size `(batch_size)`.
    merge_repeated: An optional `bool`. Defaults to `False`.
      If True, merge repeated classes in output.
    name: A name for the operation (optional).

  Returns:
    A tuple of `Tensor` objects (decoded_indices, decoded_values, decoded_shape, log_probability).

    decoded_indices: A `Tensor` of type `int64`.
    decoded_values: A `Tensor` of type `int64`.
    decoded_shape: A `Tensor` of type `int64`.
    log_probability: A `Tensor`. Has the same type as `inputs`.
  """
    _ctx = _context._context or _context.context()
    tld = _ctx._thread_local_data
    if tld.is_eager:
        try:
            _result = pywrap_tfe.TFE_Py_FastPathExecute(
                _ctx._context_handle, tld.device_name, "CTCGreedyDecoder",
                name, tld.op_callbacks, inputs, sequence_length,
                "merge_repeated", merge_repeated)
            _result = _CTCGreedyDecoderOutput._make(_result)
            return _result
        except _core._FallbackException:
            try:
                return ctc_greedy_decoder_eager_fallback(
                    inputs,
                    sequence_length,
                    merge_repeated=merge_repeated,
                    name=name,
                    ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
    # Add nodes to the TensorFlow graph.
    if merge_repeated is None:
        merge_repeated = False
    merge_repeated = _execute.make_bool(merge_repeated, "merge_repeated")
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "CTCGreedyDecoder",
        inputs=inputs,
        sequence_length=sequence_length,
        merge_repeated=merge_repeated,
        name=name)
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("merge_repeated", _op._get_attr_bool("merge_repeated"), "T",
                  _op._get_attr_type("T"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("CTCGreedyDecoder", _inputs_flat, _attrs,
                                 _result)
    _result = _CTCGreedyDecoderOutput._make(_result)
    return _result
Пример #25
0
def serialize_trt_resource(resource_name,
                           filename,
                           delete_resource=False,
                           name=None):
    r"""TODO: add doc.

  Args:
    resource_name: A `Tensor` of type `string`.
    filename: A `Tensor` of type `string`.
    delete_resource: An optional `bool`. Defaults to `False`.
    name: A name for the operation (optional).

  Returns:
    The created Operation.
  """
    _ctx = _context._context or _context.context()
    tld = _ctx._thread_local_data
    if tld.is_eager:
        try:
            _result = pywrap_tfe.TFE_Py_FastPathExecute(
                _ctx._context_handle, tld.device_name, "SerializeTRTResource",
                name, tld.op_callbacks, resource_name, filename,
                "delete_resource", delete_resource)
            return _result
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
        except _core._FallbackException:
            pass
        try:
            return serialize_trt_resource_eager_fallback(
                resource_name,
                filename,
                delete_resource=delete_resource,
                name=name,
                ctx=_ctx)
        except _core._SymbolicException:
            pass  # Add nodes to the TensorFlow graph.
        except (TypeError, ValueError):
            result = _dispatch.dispatch(
                serialize_trt_resource, (),
                dict(resource_name=resource_name,
                     filename=filename,
                     delete_resource=delete_resource,
                     name=name))
            if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
                return result
            raise
    # Add nodes to the TensorFlow graph.
    if delete_resource is None:
        delete_resource = False
    delete_resource = _execute.make_bool(delete_resource, "delete_resource")
    try:
        _, _, _op, _outputs = _op_def_library._apply_op_helper(
            "SerializeTRTResource",
            resource_name=resource_name,
            filename=filename,
            delete_resource=delete_resource,
            name=name)
    except (TypeError, ValueError):
        result = _dispatch.dispatch(
            serialize_trt_resource, (),
            dict(resource_name=resource_name,
                 filename=filename,
                 delete_resource=delete_resource,
                 name=name))
        if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
            return result
        raise
    return _op
Пример #26
0
def ctc_beam_search_decoder(inputs,
                            sequence_length,
                            beam_width,
                            top_paths,
                            merge_repeated=True,
                            name=None):
    r"""Performs beam search decoding on the logits given in input.

  A note about the attribute merge_repeated: For the beam search decoder,
  this means that if consecutive entries in a beam are the same, only
  the first of these is emitted.  That is, when the top path is "A B B B B",
  "A B" is returned if merge_repeated = True but "A B B B B" is
  returned if merge_repeated = False.

  Args:
    inputs: A `Tensor`. Must be one of the following types: `float32`, `float64`.
      3-D, shape: `(max_time x batch_size x num_classes)`, the logits.
    sequence_length: A `Tensor` of type `int32`.
      A vector containing sequence lengths, size `(batch)`.
    beam_width: An `int` that is `>= 1`.
      A scalar >= 0 (beam search beam width).
    top_paths: An `int` that is `>= 1`.
      A scalar >= 0, <= beam_width (controls output size).
    merge_repeated: An optional `bool`. Defaults to `True`.
      If true, merge repeated classes in output.
    name: A name for the operation (optional).

  Returns:
    A tuple of `Tensor` objects (decoded_indices, decoded_values, decoded_shape, log_probability).

    decoded_indices: A list of `top_paths` `Tensor` objects with type `int64`.
    decoded_values: A list of `top_paths` `Tensor` objects with type `int64`.
    decoded_shape: A list of `top_paths` `Tensor` objects with type `int64`.
    log_probability: A `Tensor`. Has the same type as `inputs`.
  """
    _ctx = _context._context or _context.context()
    tld = _ctx._thread_local_data
    if tld.is_eager:
        try:
            _result = pywrap_tfe.TFE_Py_FastPathExecute(
                _ctx._context_handle, tld.device_name, "CTCBeamSearchDecoder",
                name, tld.op_callbacks, inputs, sequence_length, "beam_width",
                beam_width, "top_paths", top_paths, "merge_repeated",
                merge_repeated)
            _result = _CTCBeamSearchDecoderOutput._make(_result)
            return _result
        except _core._FallbackException:
            try:
                return ctc_beam_search_decoder_eager_fallback(
                    inputs,
                    sequence_length,
                    beam_width=beam_width,
                    top_paths=top_paths,
                    merge_repeated=merge_repeated,
                    name=name,
                    ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
    # Add nodes to the TensorFlow graph.
    beam_width = _execute.make_int(beam_width, "beam_width")
    top_paths = _execute.make_int(top_paths, "top_paths")
    if merge_repeated is None:
        merge_repeated = True
    merge_repeated = _execute.make_bool(merge_repeated, "merge_repeated")
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "CTCBeamSearchDecoder",
        inputs=inputs,
        sequence_length=sequence_length,
        beam_width=beam_width,
        top_paths=top_paths,
        merge_repeated=merge_repeated,
        name=name)
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("beam_width", _op._get_attr_int("beam_width"), "top_paths",
                  _op._get_attr_int("top_paths"), "merge_repeated",
                  _op._get_attr_bool("merge_repeated"), "T",
                  _op._get_attr_type("T"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("CTCBeamSearchDecoder", _inputs_flat, _attrs,
                                 _result)
    _result = [_result[:top_paths]] + _result[top_paths:]
    _result = _result[:1] + [_result[1:1 + top_paths]
                             ] + _result[1 + top_paths:]
    _result = _result[:2] + [_result[2:2 + top_paths]
                             ] + _result[2 + top_paths:]
    _result = _CTCBeamSearchDecoderOutput._make(_result)
    return _result
Пример #27
0
def debug_identity(input,
                   device_name="",
                   tensor_name="",
                   debug_urls=[],
                   gated_grpc=False,
                   name=None):
    r"""Provides an identity mapping of the non-Ref type input tensor for debugging.

  Provides an identity mapping of the non-Ref type input tensor for debugging.

  Args:
    input: A `Tensor`. Input tensor, non-Reference type
    device_name: An optional `string`. Defaults to `""`.
      Name of the device on which the tensor resides.
    tensor_name: An optional `string`. Defaults to `""`.
      Name of the input tensor.
    debug_urls: An optional list of `strings`. Defaults to `[]`.
      List of URLs to debug targets, e.g.,
        file:///foo/tfdbg_dump, grpc:://localhost:11011
    gated_grpc: An optional `bool`. Defaults to `False`.
      Whether this op will be gated. If any of the debug_urls of this
        debug node is of the grpc:// scheme, when the value of this attribute is set
        to True, the data will not actually be sent via the grpc stream unless this
        debug op has been enabled at the debug_url. If all of the debug_urls of this
        debug node are of the grpc:// scheme and the debug op is enabled at none of
        them, the output will be an empty Tensor.
    name: A name for the operation (optional).

  Returns:
    A `Tensor`. Has the same type as `input`.
  """
    _ctx = _context._context or _context.context()
    tld = _ctx._thread_local_data
    if tld.is_eager:
        try:
            _result = pywrap_tfe.TFE_Py_FastPathExecute(
                _ctx, "DebugIdentity", name, input, "device_name", device_name,
                "tensor_name", tensor_name, "debug_urls", debug_urls,
                "gated_grpc", gated_grpc)
            return _result
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
        except _core._FallbackException:
            pass
        try:
            return debug_identity_eager_fallback(input,
                                                 device_name=device_name,
                                                 tensor_name=tensor_name,
                                                 debug_urls=debug_urls,
                                                 gated_grpc=gated_grpc,
                                                 name=name,
                                                 ctx=_ctx)
        except _core._SymbolicException:
            pass  # Add nodes to the TensorFlow graph.
    # Add nodes to the TensorFlow graph.
    if device_name is None:
        device_name = ""
    device_name = _execute.make_str(device_name, "device_name")
    if tensor_name is None:
        tensor_name = ""
    tensor_name = _execute.make_str(tensor_name, "tensor_name")
    if debug_urls is None:
        debug_urls = []
    if not isinstance(debug_urls, (list, tuple)):
        raise TypeError("Expected list for 'debug_urls' argument to "
                        "'debug_identity' Op, not %r." % debug_urls)
    debug_urls = [_execute.make_str(_s, "debug_urls") for _s in debug_urls]
    if gated_grpc is None:
        gated_grpc = False
    gated_grpc = _execute.make_bool(gated_grpc, "gated_grpc")
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "DebugIdentity",
        input=input,
        device_name=device_name,
        tensor_name=tensor_name,
        debug_urls=debug_urls,
        gated_grpc=gated_grpc,
        name=name)
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("T", _op._get_attr_type("T"), "device_name",
                  _op.get_attr("device_name"), "tensor_name",
                  _op.get_attr("tensor_name"), "debug_urls",
                  _op.get_attr("debug_urls"), "gated_grpc",
                  _op._get_attr_bool("gated_grpc"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("DebugIdentity", _inputs_flat, _attrs,
                                 _result)
    _result, = _result
    return _result
Пример #28
0
def ctc_loss_v2(inputs,
                labels_indices,
                labels_values,
                sequence_length,
                preprocess_collapse_repeated=False,
                ctc_merge_repeated=True,
                ignore_longer_outputs_than_inputs=False,
                name=None):
    r"""Calculates the CTC Loss (log probability) for each batch entry.  Also calculates

  the gradient.  This class performs the softmax operation for you, so inputs
  should be e.g. linear projections of outputs by an LSTM.

  Args:
    inputs: A `Tensor` of type `float32`.
      3-D, shape: `(max_time x batch_size x num_classes)`, the logits. Default blank
      label is 0 rather num_classes - 1.
    labels_indices: A `Tensor` of type `int64`.
      The indices of a `SparseTensor<int32, 2>`.
      `labels_indices(i, :) == [b, t]` means `labels_values(i)` stores the id for
      `(batch b, time t)`.
    labels_values: A `Tensor` of type `int32`.
      The values (labels) associated with the given batch and time.
    sequence_length: A `Tensor` of type `int32`.
      A vector containing sequence lengths (batch).
    preprocess_collapse_repeated: An optional `bool`. Defaults to `False`.
      Scalar, if true then repeated labels are
      collapsed prior to the CTC calculation.
    ctc_merge_repeated: An optional `bool`. Defaults to `True`.
      Scalar.  If set to false, *during* CTC calculation
      repeated non-blank labels will not be merged and are interpreted as
      individual labels.  This is a simplified version of CTC.
    ignore_longer_outputs_than_inputs: An optional `bool`. Defaults to `False`.
      Scalar. If set to true, during CTC
      calculation, items that have longer output sequences than input sequences
      are skipped: they don't contribute to the loss term and have zero-gradient.
    name: A name for the operation (optional).

  Returns:
    A tuple of `Tensor` objects (loss, gradient).

    loss: A `Tensor` of type `float32`.
    gradient: A `Tensor` of type `float32`.
  """
    _ctx = _context._context or _context.context()
    tld = _ctx._thread_local_data
    if tld.is_eager:
        try:
            _result = pywrap_tfe.TFE_Py_FastPathExecute(
                _ctx._context_handle, tld.device_name, "CTCLossV2", name,
                tld.op_callbacks, inputs, labels_indices, labels_values,
                sequence_length, "preprocess_collapse_repeated",
                preprocess_collapse_repeated, "ctc_merge_repeated",
                ctc_merge_repeated, "ignore_longer_outputs_than_inputs",
                ignore_longer_outputs_than_inputs)
            _result = _CTCLossV2Output._make(_result)
            return _result
        except _core._FallbackException:
            try:
                return ctc_loss_v2_eager_fallback(
                    inputs,
                    labels_indices,
                    labels_values,
                    sequence_length,
                    preprocess_collapse_repeated=preprocess_collapse_repeated,
                    ctc_merge_repeated=ctc_merge_repeated,
                    ignore_longer_outputs_than_inputs=
                    ignore_longer_outputs_than_inputs,
                    name=name,
                    ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
    # Add nodes to the TensorFlow graph.
    if preprocess_collapse_repeated is None:
        preprocess_collapse_repeated = False
    preprocess_collapse_repeated = _execute.make_bool(
        preprocess_collapse_repeated, "preprocess_collapse_repeated")
    if ctc_merge_repeated is None:
        ctc_merge_repeated = True
    ctc_merge_repeated = _execute.make_bool(ctc_merge_repeated,
                                            "ctc_merge_repeated")
    if ignore_longer_outputs_than_inputs is None:
        ignore_longer_outputs_than_inputs = False
    ignore_longer_outputs_than_inputs = _execute.make_bool(
        ignore_longer_outputs_than_inputs, "ignore_longer_outputs_than_inputs")
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "CTCLossV2",
        inputs=inputs,
        labels_indices=labels_indices,
        labels_values=labels_values,
        sequence_length=sequence_length,
        preprocess_collapse_repeated=preprocess_collapse_repeated,
        ctc_merge_repeated=ctc_merge_repeated,
        ignore_longer_outputs_than_inputs=ignore_longer_outputs_than_inputs,
        name=name)
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("preprocess_collapse_repeated",
                  _op._get_attr_bool("preprocess_collapse_repeated"),
                  "ctc_merge_repeated",
                  _op._get_attr_bool("ctc_merge_repeated"),
                  "ignore_longer_outputs_than_inputs",
                  _op._get_attr_bool("ignore_longer_outputs_than_inputs"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("CTCLossV2", _inputs_flat, _attrs, _result)
    _result = _CTCLossV2Output._make(_result)
    return _result
Пример #29
0
def debug_identity_v2(input,
                      tfdbg_context_id="",
                      op_name="",
                      output_slot=-1,
                      tensor_debug_mode=-1,
                      debug_urls=[],
                      circular_buffer_size=1000,
                      tfdbg_run_id="",
                      name=None):
    r"""Debug Identity V2 Op.

  Provides an identity mapping from input to output, while writing the content of
  the input tensor by calling DebugEventsWriter.

  The semantics of the input tensor depends on tensor_debug_mode. In typical
  usage, the input tensor comes directly from the user computation only when
  graph_debug_mode is FULL_TENSOR (see protobuf/debug_event.proto for a
  list of all the possible values of graph_debug_mode). For the other debug modes,
  the input tensor should be produced by an additional op or subgraph that
  computes summary information about one or more tensors.

  Args:
    input: A `Tensor`. Input tensor, non-Reference type
    tfdbg_context_id: An optional `string`. Defaults to `""`.
      A tfdbg-generated ID for the context that the op belongs to,
        e.g., a concrete compiled tf.function.
    op_name: An optional `string`. Defaults to `""`.
      Optional. Name of the op that the debug op is concerned with.
        Used only for single-tensor trace.
    output_slot: An optional `int`. Defaults to `-1`.
      Optional. Output slot index of the tensor that the debug op
        is concerned with. Used only for single-tensor trace.
    tensor_debug_mode: An optional `int`. Defaults to `-1`.
      TensorDebugMode enum value. See debug_event.proto for details.
    debug_urls: An optional list of `strings`. Defaults to `[]`.
      List of URLs to debug targets, e.g., file:///foo/tfdbg_dump.
    circular_buffer_size: An optional `int`. Defaults to `1000`.
    tfdbg_run_id: An optional `string`. Defaults to `""`.
    name: A name for the operation (optional).

  Returns:
    A `Tensor`. Has the same type as `input`.
  """
    _ctx = _context._context or _context.context()
    tld = _ctx._thread_local_data
    if tld.is_eager:
        try:
            _result = pywrap_tfe.TFE_Py_FastPathExecute(
                _ctx, "DebugIdentityV2", name, input, "tfdbg_context_id",
                tfdbg_context_id, "op_name", op_name, "output_slot",
                output_slot, "tensor_debug_mode", tensor_debug_mode,
                "debug_urls", debug_urls, "circular_buffer_size",
                circular_buffer_size, "tfdbg_run_id", tfdbg_run_id)
            return _result
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
        except _core._FallbackException:
            pass
        try:
            return debug_identity_v2_eager_fallback(
                input,
                tfdbg_context_id=tfdbg_context_id,
                op_name=op_name,
                output_slot=output_slot,
                tensor_debug_mode=tensor_debug_mode,
                debug_urls=debug_urls,
                circular_buffer_size=circular_buffer_size,
                tfdbg_run_id=tfdbg_run_id,
                name=name,
                ctx=_ctx)
        except _core._SymbolicException:
            pass  # Add nodes to the TensorFlow graph.
    # Add nodes to the TensorFlow graph.
    if tfdbg_context_id is None:
        tfdbg_context_id = ""
    tfdbg_context_id = _execute.make_str(tfdbg_context_id, "tfdbg_context_id")
    if op_name is None:
        op_name = ""
    op_name = _execute.make_str(op_name, "op_name")
    if output_slot is None:
        output_slot = -1
    output_slot = _execute.make_int(output_slot, "output_slot")
    if tensor_debug_mode is None:
        tensor_debug_mode = -1
    tensor_debug_mode = _execute.make_int(tensor_debug_mode,
                                          "tensor_debug_mode")
    if debug_urls is None:
        debug_urls = []
    if not isinstance(debug_urls, (list, tuple)):
        raise TypeError("Expected list for 'debug_urls' argument to "
                        "'debug_identity_v2' Op, not %r." % debug_urls)
    debug_urls = [_execute.make_str(_s, "debug_urls") for _s in debug_urls]
    if circular_buffer_size is None:
        circular_buffer_size = 1000
    circular_buffer_size = _execute.make_int(circular_buffer_size,
                                             "circular_buffer_size")
    if tfdbg_run_id is None:
        tfdbg_run_id = ""
    tfdbg_run_id = _execute.make_str(tfdbg_run_id, "tfdbg_run_id")
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "DebugIdentityV2",
        input=input,
        tfdbg_context_id=tfdbg_context_id,
        op_name=op_name,
        output_slot=output_slot,
        tensor_debug_mode=tensor_debug_mode,
        debug_urls=debug_urls,
        circular_buffer_size=circular_buffer_size,
        tfdbg_run_id=tfdbg_run_id,
        name=name)
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("T", _op._get_attr_type("T"), "tfdbg_context_id",
                  _op.get_attr("tfdbg_context_id"), "op_name",
                  _op.get_attr("op_name"), "output_slot",
                  _op._get_attr_int("output_slot"), "tensor_debug_mode",
                  _op._get_attr_int("tensor_debug_mode"), "debug_urls",
                  _op.get_attr("debug_urls"), "circular_buffer_size",
                  _op._get_attr_int("circular_buffer_size"), "tfdbg_run_id",
                  _op.get_attr("tfdbg_run_id"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("DebugIdentityV2", _inputs_flat, _attrs,
                                 _result)
    _result, = _result
    return _result
Пример #30
0
def encode_proto(sizes, values, field_names, message_type, descriptor_source="local://", name=None):
  r"""The op serializes protobuf messages provided in the input tensors.

  The types of the tensors in `values` must match the schema for the fields
  specified in `field_names`. All the tensors in `values` must have a common
  shape prefix, *batch_shape*.

  The `sizes` tensor specifies repeat counts for each field.  The repeat count
  (last dimension) of a each tensor in `values` must be greater than or equal
  to corresponding repeat count in `sizes`.

  A `message_type` name must be provided to give context for the field names.
  The actual message descriptor can be looked up either in the linked-in
  descriptor pool or a filename provided by the caller using the
  `descriptor_source` attribute.

  For the most part, the mapping between Proto field types and TensorFlow dtypes
  is straightforward. However, there are a few special cases:

  - A proto field that contains a submessage or group can only be converted
  to `DT_STRING` (the serialized submessage). This is to reduce the complexity
  of the API. The resulting string can be used as input to another instance of
  the decode_proto op.

  - TensorFlow lacks support for unsigned integers. The ops represent uint64
  types as a `DT_INT64` with the same twos-complement bit pattern (the obvious
  way). Unsigned int32 values can be represented exactly by specifying type
  `DT_INT64`, or using twos-complement if the caller specifies `DT_INT32` in
  the `output_types` attribute.

  The `descriptor_source` attribute selects the source of protocol
  descriptors to consult when looking up `message_type`. This may be:

  - An empty string  or "local://", in which case protocol descriptors are
  created for C++ (not Python) proto definitions linked to the binary.

  - A file, in which case protocol descriptors are created from the file,
  which is expected to contain a `FileDescriptorSet` serialized as a string.
  NOTE: You can build a `descriptor_source` file using the `--descriptor_set_out`
  and `--include_imports` options to the protocol compiler `protoc`.

  - A "bytes://<bytes>", in which protocol descriptors are created from `<bytes>`,
  which is expected to be a `FileDescriptorSet` serialized as a string.

  Args:
    sizes: A `Tensor` of type `int32`.
      Tensor of int32 with shape `[batch_shape, len(field_names)]`.
    values: A list of `Tensor` objects.
      List of tensors containing values for the corresponding field.
    field_names: A list of `strings`.
      List of strings containing proto field names.
    message_type: A `string`. Name of the proto message type to decode.
    descriptor_source: An optional `string`. Defaults to `"local://"`.
    name: A name for the operation (optional).

  Returns:
    A `Tensor` of type `string`.
  """
  _ctx = _context._context or _context.context()
  tld = _ctx._thread_local_data
  if tld.is_eager:
    try:
      _result = pywrap_tfe.TFE_Py_FastPathExecute(
        _ctx, "EncodeProto", name, sizes, values, "field_names", field_names,
        "message_type", message_type, "descriptor_source", descriptor_source)
      return _result
    except _core._NotOkStatusException as e:
      _ops.raise_from_not_ok_status(e, name)
    except _core._FallbackException:
      pass
    try:
      return encode_proto_eager_fallback(
          sizes, values, field_names=field_names, message_type=message_type,
          descriptor_source=descriptor_source, name=name, ctx=_ctx)
    except _core._SymbolicException:
      pass  # Add nodes to the TensorFlow graph.
    except (TypeError, ValueError):
      result = _dispatch.dispatch(
            encode_proto, (), dict(sizes=sizes, values=values,
                                   field_names=field_names,
                                   message_type=message_type,
                                   descriptor_source=descriptor_source,
                                   name=name)
          )
      if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
        return result
      raise
  # Add nodes to the TensorFlow graph.
  if not isinstance(field_names, (list, tuple)):
    raise TypeError(
        "Expected list for 'field_names' argument to "
        "'encode_proto' Op, not %r." % field_names)
  field_names = [_execute.make_str(_s, "field_names") for _s in field_names]
  message_type = _execute.make_str(message_type, "message_type")
  if descriptor_source is None:
    descriptor_source = "local://"
  descriptor_source = _execute.make_str(descriptor_source, "descriptor_source")
  try:
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "EncodeProto", sizes=sizes, values=values, field_names=field_names,
                       message_type=message_type,
                       descriptor_source=descriptor_source, name=name)
  except (TypeError, ValueError):
    result = _dispatch.dispatch(
          encode_proto, (), dict(sizes=sizes, values=values,
                                 field_names=field_names,
                                 message_type=message_type,
                                 descriptor_source=descriptor_source,
                                 name=name)
        )
    if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
      return result
    raise
  _result = _outputs[:]
  if _execute.must_record_gradient():
    _attrs = ("field_names", _op.get_attr("field_names"), "message_type",
              _op.get_attr("message_type"), "descriptor_source",
              _op.get_attr("descriptor_source"), "Tinput_types",
              _op.get_attr("Tinput_types"))
    _inputs_flat = _op.inputs
    _execute.record_gradient(
        "EncodeProto", _inputs_flat, _attrs, _result)
  _result, = _result
  return _result