示例#1
0
def grow_tree_v4(tree_handle, stats_handle, finished_nodes, params, name=None):
    r"""Grows the tree for finished nodes and allocates waiting nodes.

  Args:
    tree_handle: A `Tensor` of type `resource`. The handle to the tree.
    stats_handle: A `Tensor` of type `resource`. The handle to the stats.
    finished_nodes: A `Tensor` of type `int32`.
      A 1-d Tensor of finished node ids from ProcessInput.
    params: A `string`. A serialized TensorForestParams proto.
    name: A name for the operation (optional).

  Returns:
    The created Operation.
  """
    _ctx = _context._context or _context.context()
    if _ctx is not None and _ctx._thread_local_data.is_eager:
        try:
            _result = _pywrap_tensorflow.TFE_Py_FastPathExecute(
                _ctx._context_handle, _ctx._thread_local_data.device_name,
                "GrowTreeV4", name, _ctx.post_execution_callbacks, tree_handle,
                stats_handle, finished_nodes, "params", params)
            return _result
        except _core._FallbackException:
            try:
                return grow_tree_v4_eager_fallback(tree_handle,
                                                   stats_handle,
                                                   finished_nodes,
                                                   params=params,
                                                   name=name,
                                                   ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
            except (TypeError, ValueError):
                result = _dispatch.dispatch(grow_tree_v4,
                                            tree_handle=tree_handle,
                                            stats_handle=stats_handle,
                                            finished_nodes=finished_nodes,
                                            params=params,
                                            name=name)
                if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
                    return result
                raise
        except _core._NotOkStatusException as e:
            if name is not None:
                message = e.message + " name: " + name
            else:
                message = e.message
            _six.raise_from(_core._status_to_exception(e.code, message), None)
    # Add nodes to the TensorFlow graph.
    params = _execute.make_str(params, "params")
    try:
        _, _, _op = _op_def_lib._apply_op_helper("GrowTreeV4",
                                                 tree_handle=tree_handle,
                                                 stats_handle=stats_handle,
                                                 finished_nodes=finished_nodes,
                                                 params=params,
                                                 name=name)
    except (TypeError, ValueError):
        result = _dispatch.dispatch(grow_tree_v4,
                                    tree_handle=tree_handle,
                                    stats_handle=stats_handle,
                                    finished_nodes=finished_nodes,
                                    params=params,
                                    name=name)
        if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
            return result
        raise
    return _op
    _result = None
    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._FallbackException:
            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,
                    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
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
    # 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,
            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
def big_query_reader(project_id,
                     dataset_id,
                     table_id,
                     columns,
                     timestamp_millis,
                     container="",
                     shared_name="",
                     test_end_point="",
                     name=None):
    r"""A Reader that outputs rows from a BigQuery table as tensorflow Examples.

  Args:
    project_id: A `string`. GCP project ID.
    dataset_id: A `string`. BigQuery Dataset ID.
    table_id: A `string`. Table to read.
    columns: A list of `strings`.
      List of columns to read. Leave empty to read all columns.
    timestamp_millis: An `int`.
      Table snapshot timestamp in millis since epoch. Relative
      (negative or zero) snapshot times are not allowed. For more details, see
      'Table Decorators' in BigQuery docs.
    container: An optional `string`. Defaults to `""`.
      If non-empty, this reader is placed in the given container.
      Otherwise, a default container is used.
    shared_name: An optional `string`. Defaults to `""`.
      If non-empty, this reader is named in the given bucket
      with this shared_name. Otherwise, the node name is used instead.
    test_end_point: An optional `string`. Defaults to `""`.
      Do not use. For testing purposes only.
    name: A name for the operation (optional).

  Returns:
    A `Tensor` of type mutable `string`. The handle to reference the Reader.
  """
    _ctx = _context._context or _context.context()
    if _ctx is not None and _ctx._thread_local_data.is_eager:
        raise RuntimeError(
            "big_query_reader op does not support eager execution. Arg 'reader_handle' is a ref."
        )
    # Add nodes to the TensorFlow graph.
    project_id = _execute.make_str(project_id, "project_id")
    dataset_id = _execute.make_str(dataset_id, "dataset_id")
    table_id = _execute.make_str(table_id, "table_id")
    if not isinstance(columns, (list, tuple)):
        raise TypeError("Expected list for 'columns' argument to "
                        "'big_query_reader' Op, not %r." % columns)
    columns = [_execute.make_str(_s, "columns") for _s in columns]
    timestamp_millis = _execute.make_int(timestamp_millis, "timestamp_millis")
    if container is None:
        container = ""
    container = _execute.make_str(container, "container")
    if shared_name is None:
        shared_name = ""
    shared_name = _execute.make_str(shared_name, "shared_name")
    if test_end_point is None:
        test_end_point = ""
    test_end_point = _execute.make_str(test_end_point, "test_end_point")
    try:
        _, _, _op = _op_def_lib._apply_op_helper(
            "BigQueryReader",
            project_id=project_id,
            dataset_id=dataset_id,
            table_id=table_id,
            columns=columns,
            timestamp_millis=timestamp_millis,
            container=container,
            shared_name=shared_name,
            test_end_point=test_end_point,
            name=name)
    except (TypeError, ValueError):
        result = _dispatch.dispatch(big_query_reader,
                                    project_id=project_id,
                                    dataset_id=dataset_id,
                                    table_id=table_id,
                                    columns=columns,
                                    timestamp_millis=timestamp_millis,
                                    container=container,
                                    shared_name=shared_name,
                                    test_end_point=test_end_point,
                                    name=name)
        if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
            return result
        raise
    _result = _op.outputs[:]
    _inputs_flat = _op.inputs
    _attrs = ("container", _op.get_attr("container"), "shared_name",
              _op.get_attr("shared_name"), "project_id",
              _op.get_attr("project_id"), "dataset_id",
              _op.get_attr("dataset_id"), "table_id", _op.get_attr("table_id"),
              "columns", _op.get_attr("columns"), "timestamp_millis",
              _op.get_attr("timestamp_millis"), "test_end_point",
              _op.get_attr("test_end_point"))
    _execute.record_gradient("BigQueryReader", _inputs_flat, _attrs, _result,
                             name)
    _result, = _result
    return _result
def bitwise_and(x, y, name=None):
    r"""Elementwise computes the bitwise AND of `x` and `y`.

  The result will have those bits set, that are set in both `x` and `y`. The
  computation is performed on the underlying representations of `x` and `y`.

  For example:

  ```python
  import tensorflow as tf
  from tensorflow.python.ops import bitwise_ops
  dtype_list = [tf.int8, tf.int16, tf.int32, tf.int64,
                tf.uint8, tf.uint16, tf.uint32, tf.uint64]

  for dtype in dtype_list:
    lhs = tf.constant([0, 5, 3, 14], dtype=dtype)
    rhs = tf.constant([5, 0, 7, 11], dtype=dtype)
    exp = tf.constant([0, 0, 3, 10], dtype=tf.float32)

    res = bitwise_ops.bitwise_and(lhs, rhs)
    tf.assert_equal(tf.cast(res, tf.float32), exp) # TRUE
  ```

  Args:
    x: A `Tensor`. Must be one of the following types: `int8`, `int16`, `int32`, `int64`, `uint8`, `uint16`, `uint32`, `uint64`.
    y: A `Tensor`. Must have the same type as `x`.
    name: A name for the operation (optional).

  Returns:
    A `Tensor`. Has the same type as `x`.
  """
    _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, "BitwiseAnd", name,
                tld.op_callbacks, x, y)
            return _result
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
        except _core._FallbackException:
            pass
        try:
            return bitwise_and_eager_fallback(x, y, name=name, ctx=_ctx)
        except _core._SymbolicException:
            pass  # Add nodes to the TensorFlow graph.
        except (TypeError, ValueError):
            result = _dispatch.dispatch(bitwise_and, (),
                                        dict(x=x, y=y, 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("BitwiseAnd",
                                                               x=x,
                                                               y=y,
                                                               name=name)
    except (TypeError, ValueError):
        result = _dispatch.dispatch(bitwise_and, (), dict(x=x, y=y, name=name))
        if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
            return result
        raise
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("T", _op._get_attr_type("T"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("BitwiseAnd", _inputs_flat, _attrs, _result)
    _result, = _result
    return _result
def right_shift(x, y, name=None):
    r"""Elementwise computes the bitwise right-shift of `x` and `y`.

  Performs a logical shift for unsigned integer types, and an arithmetic shift
  for signed integer types.

  If `y` is negative, or greater than or equal to than the width of `x` in bits
  the result is implementation defined.

  Example:

  ```python
  import tensorflow as tf
  from tensorflow.python.ops import bitwise_ops
  import numpy as np
  dtype_list = [tf.int8, tf.int16, tf.int32, tf.int64]

  for dtype in dtype_list:
    lhs = tf.constant([-1, -5, -3, -14], dtype=dtype)
    rhs = tf.constant([5, 0, 7, 11], dtype=dtype)

    right_shift_result = bitwise_ops.right_shift(lhs, rhs)

    print(right_shift_result)

  # This will print:
  # tf.Tensor([-1 -5 -1 -1], shape=(4,), dtype=int8)
  # tf.Tensor([-1 -5 -1 -1], shape=(4,), dtype=int16)
  # tf.Tensor([-1 -5 -1 -1], shape=(4,), dtype=int32)
  # tf.Tensor([-1 -5 -1 -1], shape=(4,), dtype=int64)

  lhs = np.array([-2, 64, 101, 32], dtype=np.int8)
  rhs = np.array([-1, -5, -3, -14], dtype=np.int8)
  bitwise_ops.right_shift(lhs, rhs)
  # <tf.Tensor: shape=(4,), dtype=int8, numpy=array([ -2,  64, 101,  32], dtype=int8)>
  ```

  Args:
    x: A `Tensor`. Must be one of the following types: `int8`, `int16`, `int32`, `int64`, `uint8`, `uint16`, `uint32`, `uint64`.
    y: A `Tensor`. Must have the same type as `x`.
    name: A name for the operation (optional).

  Returns:
    A `Tensor`. Has the same type as `x`.
  """
    _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, "RightShift", name,
                tld.op_callbacks, x, y)
            return _result
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
        except _core._FallbackException:
            pass
        try:
            return right_shift_eager_fallback(x, y, name=name, ctx=_ctx)
        except _core._SymbolicException:
            pass  # Add nodes to the TensorFlow graph.
        except (TypeError, ValueError):
            result = _dispatch.dispatch(right_shift, (),
                                        dict(x=x, y=y, 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("RightShift",
                                                               x=x,
                                                               y=y,
                                                               name=name)
    except (TypeError, ValueError):
        result = _dispatch.dispatch(right_shift, (), dict(x=x, y=y, name=name))
        if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
            return result
        raise
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("T", _op._get_attr_type("T"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("RightShift", _inputs_flat, _attrs, _result)
    _result, = _result
    return _result
示例#6
0
def sdca_optimizer(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,
                   adaptative=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.
    adaptative: 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._context_handle, tld.device_name, "SdcaOptimizer", name,
                tld.op_callbacks, 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, "adaptative", adaptative, "l1", l1, "l2", l2,
                "num_loss_partitions", num_loss_partitions,
                "num_inner_iterations", num_inner_iterations)
            _result = _SdcaOptimizerOutput._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_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,
                adaptative=adaptative,
                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.
        except (TypeError, ValueError):
            result = _dispatch.dispatch(
                sdca_optimizer, (),
                dict(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,
                     adaptative=adaptative,
                     name=name))
            if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
                return result
            raise
    # 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' 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' 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' 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' Op, not %r." % sparse_indices)
    if len(sparse_indices) != _attr_num_sparse_features:
        raise ValueError(
            "List argument 'sparse_indices' to 'sdca_optimizer' 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' Op, not %r." % sparse_weights)
    if len(sparse_weights) != _attr_num_sparse_features:
        raise ValueError(
            "List argument 'sparse_weights' to 'sdca_optimizer' 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' 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' 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' Op, not %r." % dense_weights)
    if len(dense_weights) != _attr_num_dense_features:
        raise ValueError(
            "List argument 'dense_weights' to 'sdca_optimizer' 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 adaptative is None:
        adaptative = True
    adaptative = _execute.make_bool(adaptative, "adaptative")
    try:
        _, _, _op, _outputs = _op_def_library._apply_op_helper(
            "SdcaOptimizer",
            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,
            adaptative=adaptative,
            name=name)
    except (TypeError, ValueError):
        result = _dispatch.dispatch(
            sdca_optimizer, (),
            dict(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,
                 adaptative=adaptative,
                 name=name))
        if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
            return result
        raise
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("loss_type", _op.get_attr("loss_type"), "adaptative",
                  _op._get_attr_bool("adaptative"), "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("SdcaOptimizer", _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 = _SdcaOptimizerOutput._make(_result)
    return _result
def adjust_hsv_in_yiq(images, delta_h, scale_s, scale_v, name=None):
    r"""Adjust the YIQ hue of one or more images.

  `images` is a tensor of at least 3 dimensions.  The last dimension is
  interpreted as channels, and must be three.

  We used linear transformation described in:
   beesbuzz.biz/code/hsv_color_transforms.php
  The input image is considered in the RGB colorspace. Conceptually, the RGB
  colors are first mapped into YIQ space, rotated around the Y channel by
  delta_h in radians, multiplying the chrominance channels (I, Q)  by scale_s,
  multiplying all channels (Y, I, Q)  by scale_v, and then remapped back to RGB
  colorspace. Each operation described above is a linear transformation.

  Args:
    images: A `Tensor`. Must be one of the following types: `uint8`, `int8`, `int16`, `int32`, `int64`, `half`, `float32`, `float64`.
      Images to adjust.  At least 3-D.
    delta_h: A `Tensor` of type `float32`.
      A float scale that represents the hue rotation amount, in radians.
      Although delta_h can be any float value.
    scale_s: A `Tensor` of type `float32`.
      A float scale that represents the factor to multiply the saturation by.
      scale_s needs to be non-negative.
    scale_v: A `Tensor` of type `float32`.
      A float scale that represents the factor to multiply the value by.
      scale_v needs to be non-negative.
    name: A name for the operation (optional).

  Returns:
    A `Tensor`. Has the same type as `images`.
    The hsv-adjusted image or images. No clipping will be done in this op.
    The client can clip them using additional ops in their graph.
  """
    _ctx = _context._context or _context.context()
    if _ctx is not None and _ctx._thread_local_data.is_eager:
        try:
            _result = _pywrap_tensorflow.TFE_Py_FastPathExecute(
                _ctx._context_handle, _ctx._thread_local_data.device_name,
                "AdjustHsvInYiq", name, _ctx.post_execution_callbacks, images,
                delta_h, scale_s, scale_v)
            return _result
        except _core._FallbackException:
            try:
                return adjust_hsv_in_yiq_eager_fallback(images,
                                                        delta_h,
                                                        scale_s,
                                                        scale_v,
                                                        name=name,
                                                        ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
            except (TypeError, ValueError):
                result = _dispatch.dispatch(adjust_hsv_in_yiq,
                                            images=images,
                                            delta_h=delta_h,
                                            scale_s=scale_s,
                                            scale_v=scale_v,
                                            name=name)
                if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
                    return result
                raise
        except _core._NotOkStatusException as e:
            if name is not None:
                message = e.message + " name: " + name
            else:
                message = e.message
            _six.raise_from(_core._status_to_exception(e.code, message), None)
    # Add nodes to the TensorFlow graph.
    try:
        _, _, _op = _op_def_lib._apply_op_helper("AdjustHsvInYiq",
                                                 images=images,
                                                 delta_h=delta_h,
                                                 scale_s=scale_s,
                                                 scale_v=scale_v,
                                                 name=name)
    except (TypeError, ValueError):
        result = _dispatch.dispatch(adjust_hsv_in_yiq,
                                    images=images,
                                    delta_h=delta_h,
                                    scale_s=scale_s,
                                    scale_v=scale_v,
                                    name=name)
        if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
            return result
        raise
    _result = _op.outputs[:]
    _inputs_flat = _op.inputs
    _attrs = ("T", _op._get_attr_type("T"))
    _execute.record_gradient("AdjustHsvInYiq", _inputs_flat, _attrs, _result,
                             name)
    _result, = _result
    return _result
示例#8
0
def sparse_table_push(values, grads, versions, table_handle, name=None):
    r"""push variable from parameter server

  Args:
    values: A list of at least 1 `Tensor` objects with type `int64`.
    grads: A list with the same length as `values` of `Tensor` objects with type `float32`.
    versions: A list with the same length as `values` of `Tensor` objects with type `int32`.
    table_handle: An `int`.
    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, "SparseTablePush", name,
                tld.op_callbacks, values, grads, versions, "table_handle",
                table_handle)
            return _result
        except _core._FallbackException:
            try:
                return sparse_table_push_eager_fallback(
                    values,
                    grads,
                    versions,
                    table_handle=table_handle,
                    name=name,
                    ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
            except (TypeError, ValueError):
                result = _dispatch.dispatch(sparse_table_push,
                                            values=values,
                                            grads=grads,
                                            versions=versions,
                                            table_handle=table_handle,
                                            name=name)
                if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
                    return result
                raise
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
    # Add nodes to the TensorFlow graph.
    if not isinstance(values, (list, tuple)):
        raise TypeError("Expected list for 'values' argument to "
                        "'sparse_table_push' Op, not %r." % values)
    _attr_N = len(values)
    if not isinstance(grads, (list, tuple)):
        raise TypeError("Expected list for 'grads' argument to "
                        "'sparse_table_push' Op, not %r." % grads)
    if len(grads) != _attr_N:
        raise ValueError(
            "List argument 'grads' to 'sparse_table_push' Op with length %d "
            "must match length %d of argument 'values'." %
            (len(grads), _attr_N))
    if not isinstance(versions, (list, tuple)):
        raise TypeError("Expected list for 'versions' argument to "
                        "'sparse_table_push' Op, not %r." % versions)
    if len(versions) != _attr_N:
        raise ValueError(
            "List argument 'versions' to 'sparse_table_push' Op with length %d "
            "must match length %d of argument 'values'." %
            (len(versions), _attr_N))
    table_handle = _execute.make_int(table_handle, "table_handle")
    try:
        _, _, _op, _outputs = _op_def_library._apply_op_helper(
            "SparseTablePush",
            values=values,
            grads=grads,
            versions=versions,
            table_handle=table_handle,
            name=name)
    except (TypeError, ValueError):
        result = _dispatch.dispatch(sparse_table_push,
                                    values=values,
                                    grads=grads,
                                    versions=versions,
                                    table_handle=table_handle,
                                    name=name)
        if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
            return result
        raise
    return _op
示例#9
0
def sparse_table_pull(resources, values, table_handle, name=None):
    r"""pull variable from parameter server

  Args:
    resources: A list of at least 1 `Tensor` objects with type `resource`.
    values: A list with the same length as `resources` of `Tensor` objects with type `int64`.
    table_handle: An `int`.
    name: A name for the operation (optional).

  Returns:
    A tuple of `Tensor` objects (mapped_values, versions).

    mapped_values: A list with the same length as `resources` of `Tensor` objects with type `int64`.
    versions: A list with the same length as `resources` of `Tensor` objects with 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, "SparseTablePull", name,
                tld.op_callbacks, resources, values, "table_handle",
                table_handle)
            _result = _SparseTablePullOutput._make(_result)
            return _result
        except _core._FallbackException:
            try:
                return sparse_table_pull_eager_fallback(
                    resources,
                    values,
                    table_handle=table_handle,
                    name=name,
                    ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
            except (TypeError, ValueError):
                result = _dispatch.dispatch(sparse_table_pull,
                                            resources=resources,
                                            values=values,
                                            table_handle=table_handle,
                                            name=name)
                if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
                    return result
                raise
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
    # Add nodes to the TensorFlow graph.
    if not isinstance(resources, (list, tuple)):
        raise TypeError("Expected list for 'resources' argument to "
                        "'sparse_table_pull' Op, not %r." % resources)
    _attr_N = len(resources)
    if not isinstance(values, (list, tuple)):
        raise TypeError("Expected list for 'values' argument to "
                        "'sparse_table_pull' Op, not %r." % values)
    if len(values) != _attr_N:
        raise ValueError(
            "List argument 'values' to 'sparse_table_pull' Op with length %d "
            "must match length %d of argument 'resources'." %
            (len(values), _attr_N))
    table_handle = _execute.make_int(table_handle, "table_handle")
    try:
        _, _, _op, _outputs = _op_def_library._apply_op_helper(
            "SparseTablePull",
            resources=resources,
            values=values,
            table_handle=table_handle,
            name=name)
    except (TypeError, ValueError):
        result = _dispatch.dispatch(sparse_table_pull,
                                    resources=resources,
                                    values=values,
                                    table_handle=table_handle,
                                    name=name)
        if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
            return result
        raise
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("table_handle", _op._get_attr_int("table_handle"), "N",
                  _op._get_attr_int("N"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("SparseTablePull", _inputs_flat, _attrs,
                                 _result)
    _result = [_result[:_attr_N]] + _result[_attr_N:]
    _result = _result[:1] + [_result[1:]]
    _result = _SparseTablePullOutput._make(_result)
    return _result
示例#10
0
def tree_ensemble_serialize(tree_ensemble_handle, name=None):
    r"""Serializes the tree ensemble to a proto.

  Args:
    tree_ensemble_handle: A `Tensor` of type `resource`.
      Handle to the tree ensemble.
    name: A name for the operation (optional).

  Returns:
    A tuple of `Tensor` objects (stamp_token, tree_ensemble_config).

    stamp_token: A `Tensor` of type `int64`. Stamp token of the tree ensemble resource.
    tree_ensemble_config: A `Tensor` of type `string`. Serialized proto of the ensemble.
  """
    _ctx = _context._context or _context.context()
    if _ctx is not None and _ctx._thread_local_data.is_eager:
        try:
            _result = _pywrap_tensorflow.TFE_Py_FastPathExecute(
                _ctx._context_handle, _ctx._thread_local_data.device_name,
                "TreeEnsembleSerialize", name, _ctx._post_execution_callbacks,
                tree_ensemble_handle)
            _result = _TreeEnsembleSerializeOutput._make(_result)
            return _result
        except _core._FallbackException:
            try:
                return tree_ensemble_serialize_eager_fallback(
                    tree_ensemble_handle, name=name, ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
            except (TypeError, ValueError):
                result = _dispatch.dispatch(
                    tree_ensemble_serialize,
                    tree_ensemble_handle=tree_ensemble_handle,
                    name=name)
                if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
                    return result
                raise
        except _core._NotOkStatusException as e:
            if name is not None:
                message = e.message + " name: " + name
            else:
                message = e.message
            _six.raise_from(_core._status_to_exception(e.code, message), None)
    # Add nodes to the TensorFlow graph.
    try:
        _, _, _op = _op_def_lib._apply_op_helper(
            "TreeEnsembleSerialize",
            tree_ensemble_handle=tree_ensemble_handle,
            name=name)
    except (TypeError, ValueError):
        result = _dispatch.dispatch(tree_ensemble_serialize,
                                    tree_ensemble_handle=tree_ensemble_handle,
                                    name=name)
        if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
            return result
        raise
    _result = _op.outputs[:]
    _inputs_flat = _op.inputs
    _attrs = None
    _execute.record_gradient("TreeEnsembleSerialize", _inputs_flat, _attrs,
                             _result, name)
    _result = _TreeEnsembleSerializeOutput._make(_result)
    return _result
示例#11
0
def tree_ensemble_used_handlers(tree_ensemble_handle,
                                stamp_token,
                                num_all_handlers,
                                name=None):
    r"""Returns the mask of used handlers along with the number of non-zero elements in

  this mask. Used in feature selection.

  Args:
    tree_ensemble_handle: A `Tensor` of type `resource`.
      Handle to the tree ensemble.
    stamp_token: A `Tensor` of type `int64`.
      Token to use as the new value of the resource stamp.
    num_all_handlers: An `int` that is `>= 0`.
    name: A name for the operation (optional).

  Returns:
    A tuple of `Tensor` objects (num_used_handlers, used_handlers_mask).

    num_used_handlers: A `Tensor` of type `int64`. number of feature column handlers used in the model.
    used_handlers_mask: A `Tensor` of type `bool`. A boolean vector of showing which handlers are used in the
      model.
  """
    _ctx = _context._context or _context.context()
    if _ctx is not None and _ctx._thread_local_data.is_eager:
        try:
            _result = _pywrap_tensorflow.TFE_Py_FastPathExecute(
                _ctx._context_handle, _ctx._thread_local_data.device_name,
                "TreeEnsembleUsedHandlers", name,
                _ctx._post_execution_callbacks, tree_ensemble_handle,
                stamp_token, "num_all_handlers", num_all_handlers)
            _result = _TreeEnsembleUsedHandlersOutput._make(_result)
            return _result
        except _core._FallbackException:
            try:
                return tree_ensemble_used_handlers_eager_fallback(
                    tree_ensemble_handle,
                    stamp_token,
                    num_all_handlers=num_all_handlers,
                    name=name,
                    ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
            except (TypeError, ValueError):
                result = _dispatch.dispatch(
                    tree_ensemble_used_handlers,
                    tree_ensemble_handle=tree_ensemble_handle,
                    stamp_token=stamp_token,
                    num_all_handlers=num_all_handlers,
                    name=name)
                if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
                    return result
                raise
        except _core._NotOkStatusException as e:
            if name is not None:
                message = e.message + " name: " + name
            else:
                message = e.message
            _six.raise_from(_core._status_to_exception(e.code, message), None)
    # Add nodes to the TensorFlow graph.
    num_all_handlers = _execute.make_int(num_all_handlers, "num_all_handlers")
    try:
        _, _, _op = _op_def_lib._apply_op_helper(
            "TreeEnsembleUsedHandlers",
            tree_ensemble_handle=tree_ensemble_handle,
            stamp_token=stamp_token,
            num_all_handlers=num_all_handlers,
            name=name)
    except (TypeError, ValueError):
        result = _dispatch.dispatch(tree_ensemble_used_handlers,
                                    tree_ensemble_handle=tree_ensemble_handle,
                                    stamp_token=stamp_token,
                                    num_all_handlers=num_all_handlers,
                                    name=name)
        if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
            return result
        raise
    _result = _op.outputs[:]
    _inputs_flat = _op.inputs
    _attrs = ("num_all_handlers", _op.get_attr("num_all_handlers"))
    _execute.record_gradient("TreeEnsembleUsedHandlers", _inputs_flat, _attrs,
                             _result, name)
    _result = _TreeEnsembleUsedHandlersOutput._make(_result)
    return _result
示例#12
0
def create_tree_ensemble_variable(tree_ensemble_handle,
                                  stamp_token,
                                  tree_ensemble_config,
                                  name=None):
    r"""Creates a tree ensemble model and returns a handle to it.

  Args:
    tree_ensemble_handle: A `Tensor` of type `resource`.
      Handle to the tree ensemble resource to be created.
    stamp_token: A `Tensor` of type `int64`.
      Token to use as the initial value of the resource stamp.
    tree_ensemble_config: A `Tensor` of type `string`.
      Serialized proto of the tree ensemble.
    name: A name for the operation (optional).

  Returns:
    The created Operation.
  """
    _ctx = _context._context or _context.context()
    if _ctx is not None and _ctx._thread_local_data.is_eager:
        try:
            _result = _pywrap_tensorflow.TFE_Py_FastPathExecute(
                _ctx._context_handle, _ctx._thread_local_data.device_name,
                "CreateTreeEnsembleVariable", name,
                _ctx._post_execution_callbacks, tree_ensemble_handle,
                stamp_token, tree_ensemble_config)
            return _result
        except _core._FallbackException:
            try:
                return create_tree_ensemble_variable_eager_fallback(
                    tree_ensemble_handle,
                    stamp_token,
                    tree_ensemble_config,
                    name=name,
                    ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
            except (TypeError, ValueError):
                result = _dispatch.dispatch(
                    create_tree_ensemble_variable,
                    tree_ensemble_handle=tree_ensemble_handle,
                    stamp_token=stamp_token,
                    tree_ensemble_config=tree_ensemble_config,
                    name=name)
                if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
                    return result
                raise
        except _core._NotOkStatusException as e:
            if name is not None:
                message = e.message + " name: " + name
            else:
                message = e.message
            _six.raise_from(_core._status_to_exception(e.code, message), None)
    # Add nodes to the TensorFlow graph.
    try:
        _, _, _op = _op_def_lib._apply_op_helper(
            "CreateTreeEnsembleVariable",
            tree_ensemble_handle=tree_ensemble_handle,
            stamp_token=stamp_token,
            tree_ensemble_config=tree_ensemble_config,
            name=name)
    except (TypeError, ValueError):
        result = _dispatch.dispatch(create_tree_ensemble_variable,
                                    tree_ensemble_handle=tree_ensemble_handle,
                                    stamp_token=stamp_token,
                                    tree_ensemble_config=tree_ensemble_config,
                                    name=name)
        if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
            return result
        raise
    return _op
    _result = None
    return _result
示例#13
0
def decision_tree_ensemble_resource_handle_op(container="",
                                              shared_name="",
                                              name=None):
    r"""TODO: add doc.

  Args:
    container: An optional `string`. Defaults to `""`.
    shared_name: An optional `string`. Defaults to `""`.
    name: A name for the operation (optional).

  Returns:
    A `Tensor` of type `resource`.
  """
    _ctx = _context._context or _context.context()
    if _ctx is not None and _ctx._thread_local_data.is_eager:
        try:
            _result = _pywrap_tensorflow.TFE_Py_FastPathExecute(
                _ctx._context_handle, _ctx._thread_local_data.device_name,
                "DecisionTreeEnsembleResourceHandleOp", name,
                _ctx._post_execution_callbacks, "container", container,
                "shared_name", shared_name)
            return _result
        except _core._FallbackException:
            try:
                return decision_tree_ensemble_resource_handle_op_eager_fallback(
                    container=container,
                    shared_name=shared_name,
                    name=name,
                    ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
            except (TypeError, ValueError):
                result = _dispatch.dispatch(
                    decision_tree_ensemble_resource_handle_op,
                    container=container,
                    shared_name=shared_name,
                    name=name)
                if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
                    return result
                raise
        except _core._NotOkStatusException as e:
            if name is not None:
                message = e.message + " name: " + name
            else:
                message = e.message
            _six.raise_from(_core._status_to_exception(e.code, message), None)
    # Add nodes to the TensorFlow graph.
    if container is None:
        container = ""
    container = _execute.make_str(container, "container")
    if shared_name is None:
        shared_name = ""
    shared_name = _execute.make_str(shared_name, "shared_name")
    try:
        _, _, _op = _op_def_lib._apply_op_helper(
            "DecisionTreeEnsembleResourceHandleOp",
            container=container,
            shared_name=shared_name,
            name=name)
    except (TypeError, ValueError):
        result = _dispatch.dispatch(decision_tree_ensemble_resource_handle_op,
                                    container=container,
                                    shared_name=shared_name,
                                    name=name)
        if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
            return result
        raise
    _result = _op.outputs[:]
    _inputs_flat = _op.inputs
    _attrs = ("container", _op.get_attr("container"), "shared_name",
              _op.get_attr("shared_name"))
    _execute.record_gradient("DecisionTreeEnsembleResourceHandleOp",
                             _inputs_flat, _attrs, _result, name)
    _result, = _result
    return _result
示例#14
0
def process_input_v4(tree_handle,
                     stats_handle,
                     input_data,
                     sparse_input_indices,
                     sparse_input_values,
                     sparse_input_shape,
                     input_labels,
                     input_weights,
                     leaf_ids,
                     random_seed,
                     input_spec,
                     params,
                     name=None):
    r"""Add labels to stats after traversing the tree for each example.

  Outputs node ids that are finished.

  Args:
    tree_handle: A `Tensor` of type `resource`. The handle to the tree.
    stats_handle: A `Tensor` of type `resource`. The handle to the stats.
    input_data: A `Tensor` of type `float32`.
      The training batch's features as a 2-d tensor; `input_data[i][j]`
      gives the j-th feature of the i-th input.
    sparse_input_indices: A `Tensor` of type `int64`.
      The indices tensor from the SparseTensor input.
    sparse_input_values: A `Tensor` of type `float32`.
      The values tensor from the SparseTensor input.
    sparse_input_shape: A `Tensor` of type `int64`.
      The shape tensor from the SparseTensor input.
    input_labels: A `Tensor` of type `float32`.
      The training batch's labels as a 1 or 2-d tensor.
      'input_labels[i][j]' gives the j-th label/target for the i-th input.
    input_weights: A `Tensor` of type `float32`.
      The training batch's weights as a 1-d tensor.
      'input_weights[i]' gives the weight for the i-th input.
    leaf_ids: A `Tensor` of type `int32`.
      `leaf_ids[i]` is the leaf id for input i.
    random_seed: An `int`.
    input_spec: A `string`.
    params: A `string`. A serialized TensorForestParams proto.
    name: A name for the operation (optional).

  Returns:
    A `Tensor` of type `int32`.
    A 1-d tensor of node ids that have finished and are ready to
    grow.
  """
    _ctx = _context._context or _context.context()
    if _ctx is not None and _ctx._thread_local_data.is_eager:
        try:
            _result = _pywrap_tensorflow.TFE_Py_FastPathExecute(
                _ctx._context_handle, _ctx._thread_local_data.device_name,
                "ProcessInputV4", name, _ctx.post_execution_callbacks,
                tree_handle, stats_handle, input_data, sparse_input_indices,
                sparse_input_values, sparse_input_shape, input_labels,
                input_weights, leaf_ids, "random_seed", random_seed,
                "input_spec", input_spec, "params", params)
            return _result
        except _core._FallbackException:
            try:
                return process_input_v4_eager_fallback(tree_handle,
                                                       stats_handle,
                                                       input_data,
                                                       sparse_input_indices,
                                                       sparse_input_values,
                                                       sparse_input_shape,
                                                       input_labels,
                                                       input_weights,
                                                       leaf_ids,
                                                       random_seed=random_seed,
                                                       input_spec=input_spec,
                                                       params=params,
                                                       name=name,
                                                       ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
            except (TypeError, ValueError):
                result = _dispatch.dispatch(
                    process_input_v4,
                    tree_handle=tree_handle,
                    stats_handle=stats_handle,
                    input_data=input_data,
                    sparse_input_indices=sparse_input_indices,
                    sparse_input_values=sparse_input_values,
                    sparse_input_shape=sparse_input_shape,
                    input_labels=input_labels,
                    input_weights=input_weights,
                    leaf_ids=leaf_ids,
                    random_seed=random_seed,
                    input_spec=input_spec,
                    params=params,
                    name=name)
                if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
                    return result
                raise
        except _core._NotOkStatusException as e:
            if name is not None:
                message = e.message + " name: " + name
            else:
                message = e.message
            _six.raise_from(_core._status_to_exception(e.code, message), None)
    # Add nodes to the TensorFlow graph.
    random_seed = _execute.make_int(random_seed, "random_seed")
    input_spec = _execute.make_str(input_spec, "input_spec")
    params = _execute.make_str(params, "params")
    try:
        _, _, _op = _op_def_lib._apply_op_helper(
            "ProcessInputV4",
            tree_handle=tree_handle,
            stats_handle=stats_handle,
            input_data=input_data,
            sparse_input_indices=sparse_input_indices,
            sparse_input_values=sparse_input_values,
            sparse_input_shape=sparse_input_shape,
            input_labels=input_labels,
            input_weights=input_weights,
            leaf_ids=leaf_ids,
            random_seed=random_seed,
            input_spec=input_spec,
            params=params,
            name=name)
    except (TypeError, ValueError):
        result = _dispatch.dispatch(process_input_v4,
                                    tree_handle=tree_handle,
                                    stats_handle=stats_handle,
                                    input_data=input_data,
                                    sparse_input_indices=sparse_input_indices,
                                    sparse_input_values=sparse_input_values,
                                    sparse_input_shape=sparse_input_shape,
                                    input_labels=input_labels,
                                    input_weights=input_weights,
                                    leaf_ids=leaf_ids,
                                    random_seed=random_seed,
                                    input_spec=input_spec,
                                    params=params,
                                    name=name)
        if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
            return result
        raise
    _result = _op.outputs[:]
    _inputs_flat = _op.inputs
    _attrs = ("random_seed", _op.get_attr("random_seed"), "input_spec",
              _op.get_attr("input_spec"), "params", _op.get_attr("params"))
    _execute.record_gradient("ProcessInputV4", _inputs_flat, _attrs, _result,
                             name)
    _result, = _result
    return _result
示例#15
0
def tree_ensemble_stats(tree_ensemble_handle, stamp_token, name=None):
    r"""Retrieves stats related to the tree ensemble.

  Args:
    tree_ensemble_handle: A `Tensor` of type `resource`.
      Handle to the ensemble variable.
    stamp_token: A `Tensor` of type `int64`.
      Stamp token for validating operation consistency.
    name: A name for the operation (optional).

  Returns:
    A tuple of `Tensor` objects (num_trees, num_layers, active_tree, active_layer, attempted_trees, attempted_layers).

    num_trees: A `Tensor` of type `int64`. Scalar indicating the number of finalized trees in the ensemble.
    num_layers: A `Tensor` of type `int64`. Scalar indicating the number of layers in the ensemble.
    active_tree: A `Tensor` of type `int64`. Scalar indicating the active tree being trained.
    active_layer: A `Tensor` of type `int64`. Scalar indicating the active layer being trained.
    attempted_trees: A `Tensor` of type `int64`.
    attempted_layers: A `Tensor` of type `int64`.
  """
    _ctx = _context._context
    if _ctx is not None and _ctx._eager_context.is_eager:
        try:
            _result = _pywrap_tensorflow.TFE_Py_FastPathExecute(
                _ctx._context_handle, _ctx._eager_context.device_name,
                "TreeEnsembleStats", name, _ctx._post_execution_callbacks,
                tree_ensemble_handle, stamp_token)
            _result = _TreeEnsembleStatsOutput._make(_result)
            return _result
        except _core._FallbackException:
            try:
                return tree_ensemble_stats_eager_fallback(tree_ensemble_handle,
                                                          stamp_token,
                                                          name=name,
                                                          ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
            except (TypeError, ValueError):
                result = _dispatch.dispatch(
                    tree_ensemble_stats,
                    tree_ensemble_handle=tree_ensemble_handle,
                    stamp_token=stamp_token,
                    name=name)
                if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
                    return result
                raise
        except _core._NotOkStatusException as e:
            if name is not None:
                message = e.message + " name: " + name
            else:
                message = e.message
            _six.raise_from(_core._status_to_exception(e.code, message), None)
    # Add nodes to the TensorFlow graph.
    try:
        _, _, _op = _op_def_lib._apply_op_helper(
            "TreeEnsembleStats",
            tree_ensemble_handle=tree_ensemble_handle,
            stamp_token=stamp_token,
            name=name)
    except (TypeError, ValueError):
        result = _dispatch.dispatch(tree_ensemble_stats,
                                    tree_ensemble_handle=tree_ensemble_handle,
                                    stamp_token=stamp_token,
                                    name=name)
        if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
            return result
        raise
    _result = _op.outputs[:]
    _inputs_flat = _op.inputs
    _attrs = None
    _execute.record_gradient("TreeEnsembleStats", _inputs_flat, _attrs,
                             _result, name)
    _result = _TreeEnsembleStatsOutput._make(_result)
    return _result
示例#16
0
def resampler_grad(data, warp, grad_output, name=None):
    r"""Resampler Grad op.

  Args:
    data: A `Tensor`. Must be one of the following types: `half`, `bfloat16`, `float32`, `float64`.
    warp: A `Tensor`. Must have the same type as `data`.
    grad_output: A `Tensor`. Must have the same type as `data`.
    name: A name for the operation (optional).

  Returns:
    A tuple of `Tensor` objects (grad_data, grad_warp).

    grad_data: A `Tensor`. Has the same type as `data`.
    grad_warp: A `Tensor`. Has the same type as `data`.
  """
    _ctx = _context._context or _context.context()
    if _ctx is not None and _ctx._thread_local_data.is_eager:
        try:
            _result = _pywrap_tensorflow.TFE_Py_FastPathExecute(
                _ctx._context_handle, _ctx._thread_local_data.device_name,
                "ResamplerGrad", name, _ctx.post_execution_callbacks, data,
                warp, grad_output)
            _result = _ResamplerGradOutput._make(_result)
            return _result
        except _core._FallbackException:
            try:
                return resampler_grad_eager_fallback(data,
                                                     warp,
                                                     grad_output,
                                                     name=name,
                                                     ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
            except (TypeError, ValueError):
                result = _dispatch.dispatch(resampler_grad,
                                            data=data,
                                            warp=warp,
                                            grad_output=grad_output,
                                            name=name)
                if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
                    return result
                raise
        except _core._NotOkStatusException as e:
            if name is not None:
                message = e.message + " name: " + name
            else:
                message = e.message
            _six.raise_from(_core._status_to_exception(e.code, message), None)
    # Add nodes to the TensorFlow graph.
    try:
        _, _, _op = _op_def_lib._apply_op_helper("ResamplerGrad",
                                                 data=data,
                                                 warp=warp,
                                                 grad_output=grad_output,
                                                 name=name)
    except (TypeError, ValueError):
        result = _dispatch.dispatch(resampler_grad,
                                    data=data,
                                    warp=warp,
                                    grad_output=grad_output,
                                    name=name)
        if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
            return result
        raise
    _result = _op.outputs[:]
    _inputs_flat = _op.inputs
    _attrs = ("T", _op._get_attr_type("T"))
    _execute.record_gradient("ResamplerGrad", _inputs_flat, _attrs, _result,
                             name)
    _result = _ResamplerGradOutput._make(_result)
    return _result
def encode_proto(sizes,
                 values,
                 field_names,
                 message_type,
                 descriptor_source="local://",
                 name=None):
    r"""TODO: add doc.

  Args:
    sizes: A `Tensor` of type `int32`.
    values: A list of `Tensor` objects.
    field_names: A list of `strings`.
    message_type: A `string`.
    descriptor_source: An optional `string`. Defaults to `"local://"`.
    name: A name for the operation (optional).

  Returns:
    A `Tensor` of type `string`.
  """
    _ctx = _context._context
    if _ctx is not None and _ctx._eager_context.is_eager:
        try:
            _result = _pywrap_tensorflow.TFE_Py_FastPathExecute(
                _ctx._context_handle, _ctx._eager_context.device_name,
                "EncodeProto", name, _ctx._post_execution_callbacks, sizes,
                values, "field_names", field_names, "message_type",
                message_type, "descriptor_source", descriptor_source)
            return _result
        except _core._FallbackException:
            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,
                    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
        except _core._NotOkStatusException as e:
            if name is not None:
                message = e.message + " name: " + name
            else:
                message = e.message
            _six.raise_from(_core._status_to_exception(e.code, message), None)
    # 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 = _op_def_lib._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,
                                    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 = _op.outputs[:]
    _inputs_flat = _op.inputs
    _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"))
    _execute.record_gradient("EncodeProto", _inputs_flat, _attrs, _result,
                             name)
    _result, = _result
    return _result
def sparse_feature_cross(indices,
                         values,
                         shapes,
                         dense,
                         hashed_output,
                         num_buckets,
                         out_type,
                         internal_type,
                         name=None):
    r"""Generates sparse cross form a list of sparse tensors.

  The op takes two lists, one of 2D `SparseTensor` and one of 2D `Tensor`, each
  representing features of one feature column. It outputs a 2D `SparseTensor` with
  the batchwise crosses of these features.

  For example, if the inputs are

      inputs[0]: SparseTensor with shape = [2, 2]
      [0, 0]: "a"
      [1, 0]: "b"
      [1, 1]: "c"

      inputs[1]: SparseTensor with shape = [2, 1]
      [0, 0]: "d"
      [1, 0]: "e"

      inputs[2]: Tensor [["f"], ["g"]]

  then the output will be

      shape = [2, 2]
      [0, 0]: "a_X_d_X_f"
      [1, 0]: "b_X_e_X_g"
      [1, 1]: "c_X_e_X_g"

  if hashed_output=true then the output will be

      shape = [2, 2]
      [0, 0]: HashCombine(
                  Fingerprint64("f"), HashCombine(
                      Fingerprint64("d"), Fingerprint64("a")))
      [1, 0]: HashCombine(
                  Fingerprint64("g"), HashCombine(
                      Fingerprint64("e"), Fingerprint64("b")))
      [1, 1]: HashCombine(
                  Fingerprint64("g"), HashCombine(
                      Fingerprint64("e"), Fingerprint64("c")))

  Args:
    indices: A list of `Tensor` objects with type `int64`.
      2-D.  Indices of each input `SparseTensor`.
    values: A list of `Tensor` objects with types from: `int64`, `string`.
      1-D.   values of each `SparseTensor`.
    shapes: A list with the same length as `indices` of `Tensor` objects with type `int64`.
      1-D.   Shapes of each `SparseTensor`.
    dense: A list of `Tensor` objects with types from: `int64`, `string`.
      2-D.    Columns represented by dense `Tensor`.
    hashed_output: A `bool`.
    num_buckets: An `int` that is `>= 0`.
    out_type: A `tf.DType` from: `tf.int64, tf.string`.
    internal_type: A `tf.DType` from: `tf.int64, tf.string`.
    name: A name for the operation (optional).

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

    output_indices: A `Tensor` of type `int64`. 2-D.  Indices of the concatenated `SparseTensor`.
    output_values: A `Tensor` of type `out_type`. 1-D.  Non-empty values of the concatenated or hashed
      `SparseTensor`.
    output_shape: A `Tensor` of type `int64`. 1-D.  Shape of the concatenated `SparseTensor`.
  """
    _ctx = _context._context or _context.context()
    if _ctx is not None and _ctx._thread_local_data.is_eager:
        try:
            _result = _pywrap_tensorflow.TFE_Py_FastPathExecute(
                _ctx._context_handle, _ctx._thread_local_data.device_name,
                "SparseFeatureCross", name, _ctx._post_execution_callbacks,
                indices, values, shapes, dense, "hashed_output", hashed_output,
                "num_buckets", num_buckets, "out_type", out_type,
                "internal_type", internal_type)
            _result = _SparseFeatureCrossOutput._make(_result)
            return _result
        except _core._FallbackException:
            try:
                return sparse_feature_cross_eager_fallback(
                    indices,
                    values,
                    shapes,
                    dense,
                    hashed_output=hashed_output,
                    num_buckets=num_buckets,
                    out_type=out_type,
                    internal_type=internal_type,
                    name=name,
                    ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
            except (TypeError, ValueError):
                result = _dispatch.dispatch(sparse_feature_cross,
                                            indices=indices,
                                            values=values,
                                            shapes=shapes,
                                            dense=dense,
                                            hashed_output=hashed_output,
                                            num_buckets=num_buckets,
                                            out_type=out_type,
                                            internal_type=internal_type,
                                            name=name)
                if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
                    return result
                raise
        except _core._NotOkStatusException as e:
            if name is not None:
                message = e.message + " name: " + name
            else:
                message = e.message
            _six.raise_from(_core._status_to_exception(e.code, message), None)
    # Add nodes to the TensorFlow graph.
    if not isinstance(indices, (list, tuple)):
        raise TypeError("Expected list for 'indices' argument to "
                        "'sparse_feature_cross' Op, not %r." % indices)
    _attr_N = len(indices)
    if not isinstance(shapes, (list, tuple)):
        raise TypeError("Expected list for 'shapes' argument to "
                        "'sparse_feature_cross' Op, not %r." % shapes)
    if len(shapes) != _attr_N:
        raise ValueError(
            "List argument 'shapes' to 'sparse_feature_cross' Op with length %d "
            "must match length %d of argument 'indices'." %
            (len(shapes), _attr_N))
    hashed_output = _execute.make_bool(hashed_output, "hashed_output")
    num_buckets = _execute.make_int(num_buckets, "num_buckets")
    out_type = _execute.make_type(out_type, "out_type")
    internal_type = _execute.make_type(internal_type, "internal_type")
    try:
        _, _, _op = _op_def_lib._apply_op_helper("SparseFeatureCross",
                                                 indices=indices,
                                                 values=values,
                                                 shapes=shapes,
                                                 dense=dense,
                                                 hashed_output=hashed_output,
                                                 num_buckets=num_buckets,
                                                 out_type=out_type,
                                                 internal_type=internal_type,
                                                 name=name)
    except (TypeError, ValueError):
        result = _dispatch.dispatch(sparse_feature_cross,
                                    indices=indices,
                                    values=values,
                                    shapes=shapes,
                                    dense=dense,
                                    hashed_output=hashed_output,
                                    num_buckets=num_buckets,
                                    out_type=out_type,
                                    internal_type=internal_type,
                                    name=name)
        if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
            return result
        raise
    _result = _op.outputs[:]
    _inputs_flat = _op.inputs
    _attrs = ("N", _op.get_attr("N"), "hashed_output",
              _op.get_attr("hashed_output"), "num_buckets",
              _op.get_attr("num_buckets"), "sparse_types",
              _op.get_attr("sparse_types"), "dense_types",
              _op.get_attr("dense_types"), "out_type",
              _op.get_attr("out_type"), "internal_type",
              _op.get_attr("internal_type"))
    _execute.record_gradient("SparseFeatureCross", _inputs_flat, _attrs,
                             _result, name)
    _result = _SparseFeatureCrossOutput._make(_result)
    return _result
示例#19
0
def rpc(address,
        method,
        request,
        protocol="",
        fail_fast=True,
        timeout_in_ms=0,
        name=None):
    r"""TODO: add doc.

  Args:
    address: A `Tensor` of type `string`.
    method: A `Tensor` of type `string`.
    request: A `Tensor` of type `string`.
    protocol: An optional `string`. Defaults to `""`.
    fail_fast: An optional `bool`. Defaults to `True`.
    timeout_in_ms: An optional `int`. Defaults to `0`.
    name: A name for the operation (optional).

  Returns:
    A `Tensor` of type `string`.
  """
    _ctx = _context._context or _context.context()
    if _ctx is not None and _ctx._thread_local_data.is_eager:
        try:
            _result = _pywrap_tensorflow.TFE_Py_FastPathExecute(
                _ctx._context_handle, _ctx._thread_local_data.device_name,
                "Rpc", name, _ctx._post_execution_callbacks, address, method,
                request, "protocol", protocol, "fail_fast", fail_fast,
                "timeout_in_ms", timeout_in_ms)
            return _result
        except _core._FallbackException:
            try:
                return rpc_eager_fallback(address,
                                          method,
                                          request,
                                          protocol=protocol,
                                          fail_fast=fail_fast,
                                          timeout_in_ms=timeout_in_ms,
                                          name=name,
                                          ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
            except (TypeError, ValueError):
                result = _dispatch.dispatch(rpc,
                                            address=address,
                                            method=method,
                                            request=request,
                                            protocol=protocol,
                                            fail_fast=fail_fast,
                                            timeout_in_ms=timeout_in_ms,
                                            name=name)
                if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
                    return result
                raise
        except _core._NotOkStatusException as e:
            if name is not None:
                message = e.message + " name: " + name
            else:
                message = e.message
            _six.raise_from(_core._status_to_exception(e.code, message), None)
    # Add nodes to the TensorFlow graph.
    if protocol is None:
        protocol = ""
    protocol = _execute.make_str(protocol, "protocol")
    if fail_fast is None:
        fail_fast = True
    fail_fast = _execute.make_bool(fail_fast, "fail_fast")
    if timeout_in_ms is None:
        timeout_in_ms = 0
    timeout_in_ms = _execute.make_int(timeout_in_ms, "timeout_in_ms")
    try:
        _, _, _op = _op_def_lib._apply_op_helper("Rpc",
                                                 address=address,
                                                 method=method,
                                                 request=request,
                                                 protocol=protocol,
                                                 fail_fast=fail_fast,
                                                 timeout_in_ms=timeout_in_ms,
                                                 name=name)
    except (TypeError, ValueError):
        result = _dispatch.dispatch(rpc,
                                    address=address,
                                    method=method,
                                    request=request,
                                    protocol=protocol,
                                    fail_fast=fail_fast,
                                    timeout_in_ms=timeout_in_ms,
                                    name=name)
        if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
            return result
        raise
    _result = _op.outputs[:]
    _inputs_flat = _op.inputs
    _attrs = ("protocol", _op.get_attr("protocol"), "fail_fast",
              _op.get_attr("fail_fast"), "timeout_in_ms",
              _op.get_attr("timeout_in_ms"))
    _execute.record_gradient("Rpc", _inputs_flat, _attrs, _result, name)
    _result, = _result
    return _result
示例#20
0
def wals_compute_partial_lhs_and_rhs(factors,
                                     factor_weights,
                                     unobserved_weights,
                                     input_weights,
                                     input_indices,
                                     input_values,
                                     entry_weights,
                                     input_block_size,
                                     input_is_transpose,
                                     name=None):
    r"""Computes the partial left-hand side and right-hand side of WALS update. For

  observed entry input_indices[i]=[m, n] with value input_values[i]=v, the weight
  should be specified either through (1) entry_weights[i] or (2) through
  input_weights[m] * factor_weights[n] (if input_is_transpose is false) or
  input_weights[n] * factor_weights[m] (if input_is_transpose is true). Note it is
  not allowed to have both (1) and (2) specified at the same time: when one
  approach is used, the input tensors related to the other approach must be kept
  completely empty.

  Args:
    factors: A `Tensor` of type `float32`. Matrix of size m * k.
    factor_weights: A `Tensor` of type `float32`.
      Vector of size m. Corresponds to column weights. Should be empty
      if entry_weights is used.
    unobserved_weights: A `Tensor` of type `float32`.
      Scalar. Weight for unobserved input entries.
    input_weights: A `Tensor` of type `float32`.
      Vector of size n. Corresponds to row weights. Should be empty if
      entry_weights is used.
    input_indices: A `Tensor` of type `int64`.
      Indices for the input SparseTensor.
    input_values: A `Tensor` of type `float32`.
      Values for the input SparseTensor.
    entry_weights: A `Tensor` of type `float32`.
      If not empty, this must be same length as input_vaues and is used
      as the per-entry non-zero weight. If this is used, input_weights and
      factor_weights must be empty.
    input_block_size: A `Tensor` of type `int64`.
      Scalar. Number of rows spanned by input.
    input_is_transpose: A `Tensor` of type `bool`.
      If true, logically transposes the input for processing.
    name: A name for the operation (optional).

  Returns:
    A tuple of `Tensor` objects (partial_lhs, partial_rhs).

    partial_lhs: A `Tensor` of type `float32`. 3-D tensor with size input_block_size x k x k.
    partial_rhs: A `Tensor` of type `float32`. Matrix with size input_block_size x k.
  """
    _ctx = _context._context or _context.context()
    if _ctx is not None and _ctx._thread_local_data.is_eager:
        try:
            _result = _pywrap_tensorflow.TFE_Py_FastPathExecute(
                _ctx._context_handle, _ctx._thread_local_data.device_name,
                "WALSComputePartialLhsAndRhs", name,
                _ctx._post_execution_callbacks, factors, factor_weights,
                unobserved_weights, input_weights, input_indices, input_values,
                entry_weights, input_block_size, input_is_transpose)
            _result = _WALSComputePartialLhsAndRhsOutput._make(_result)
            return _result
        except _core._FallbackException:
            try:
                return wals_compute_partial_lhs_and_rhs_eager_fallback(
                    factors,
                    factor_weights,
                    unobserved_weights,
                    input_weights,
                    input_indices,
                    input_values,
                    entry_weights,
                    input_block_size,
                    input_is_transpose,
                    name=name,
                    ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
            except (TypeError, ValueError):
                result = _dispatch.dispatch(
                    wals_compute_partial_lhs_and_rhs,
                    factors=factors,
                    factor_weights=factor_weights,
                    unobserved_weights=unobserved_weights,
                    input_weights=input_weights,
                    input_indices=input_indices,
                    input_values=input_values,
                    entry_weights=entry_weights,
                    input_block_size=input_block_size,
                    input_is_transpose=input_is_transpose,
                    name=name)
                if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
                    return result
                raise
        except _core._NotOkStatusException as e:
            if name is not None:
                message = e.message + " name: " + name
            else:
                message = e.message
            _six.raise_from(_core._status_to_exception(e.code, message), None)
    # Add nodes to the TensorFlow graph.
    try:
        _, _, _op = _op_def_lib._apply_op_helper(
            "WALSComputePartialLhsAndRhs",
            factors=factors,
            factor_weights=factor_weights,
            unobserved_weights=unobserved_weights,
            input_weights=input_weights,
            input_indices=input_indices,
            input_values=input_values,
            entry_weights=entry_weights,
            input_block_size=input_block_size,
            input_is_transpose=input_is_transpose,
            name=name)
    except (TypeError, ValueError):
        result = _dispatch.dispatch(wals_compute_partial_lhs_and_rhs,
                                    factors=factors,
                                    factor_weights=factor_weights,
                                    unobserved_weights=unobserved_weights,
                                    input_weights=input_weights,
                                    input_indices=input_indices,
                                    input_values=input_values,
                                    entry_weights=entry_weights,
                                    input_block_size=input_block_size,
                                    input_is_transpose=input_is_transpose,
                                    name=name)
        if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
            return result
        raise
    _result = _op.outputs[:]
    _inputs_flat = _op.inputs
    _attrs = None
    _execute.record_gradient("WALSComputePartialLhsAndRhs", _inputs_flat,
                             _attrs, _result, name)
    _result = _WALSComputePartialLhsAndRhsOutput._make(_result)
    return _result
示例#21
0
def fused_conv2d_bias_activation(conv_input,
                                 filter,
                                 bias,
                                 side_input,
                                 conv_input_scale,
                                 side_input_scale,
                                 strides,
                                 padding,
                                 data_format="NHWC",
                                 filter_format="HWIO",
                                 activation_mode="Relu",
                                 dilations=[1, 1, 1, 1],
                                 name=None):
    r"""    Computes a fused kernel which implements: 2-D convolution, adds side input,

      with separate scaling on convolution and side inputs, then adds bias and
      applies the RELU activation function to the result. Supports both float and
      qint8 data formats. In the case of qint8, the output is clipped to [0..127].

      conv_input: A tensor with format as specified by `data_format` (see below).
      filter: A tensor with format depending on `data_format` as follows:
          "NHWC", "NCHW":
               `float [ filter_height, filter_width, in_channels, out_channels ]`
          "NCHW_VECT_C":
               `qint8 [ out_channels, in_channels, filter_height, filter_width ]`
      bias: 1-D float tensor with size matching the `out_channels` dimension of
          `filter`.
          Note: this tensor is still float, even if other inputs are qint8.
      side_input: A tensor with format as specified by `data_format` (see below).
          This tensor will be ignored and can be [] if side_input_scale == 0.
          Otherwise, the size of each dimension must match the `output` tensor.
      conv_input_scale: scalar float value to be multiplied by `conv_input`.
          (conceptually.. in reality it is applied after convolution).
      side_input_scale: scalar float value to be multiplied by `side_input`.
      output: A tensor with format as specified by `data_format` (see below).
          The dimension sizes are determined automatically based on other inputs
          and attributes.
      T: The element data type of `conv_input`, `side_input` and `output` tensors.
          Note: must match with the `data_format`.
      Tbias: The element data type of `bias`.
      strides: 1-D tensor of length 4.  The stride of the sliding window for each
          dimension of `input`. The dimension order is determined by the value of
          `data_format`, see below for details.
          Note: the stride for batch and channel dimensions must be 1.
      padding: The type of padding algorithm to use.
      data_format: A string specifying the data format of `conv_input`,
          `side_input` and `output` tensors with the following options:
          "NHWC": `float [ batch, height, width, channels ]`
          "NCHW": `float [ batch, channels, height, width ]`
          "NCHW_VECT_C":
              `qint8 [ batch, channels / 4, height, width, channels % 4 ]`
          Note: for "NCHW_VECT_C", `channels` must be a multiple of 4.
      filter_format: A string specifying the data format of `filter`,
          "HWIO": `float [ kernel_height, kernel_width, input_channels,
                           output_channels ]`
          "OIHW_VECT_I":
              `qint8 [ output_channels, input_channels / 4,
                       kernel_height, kernel_width, input_channels % 4 ]`
      activation_mode: The activation applied to the output.
          Must be "Relu" or "None".
      dilations: 1-D tensor of length 4.  The dilation factor for each dimension
          of `input`. If set to k > 1, there will be k-1 skipped cells between
          each filter element on that dimension. The dimension order is determined
          by the value of `data_format`, see above for details. Dilations in the
          batch and depth dimensions must be 1.

  Args:
    conv_input: A `Tensor`. Must be one of the following types: `float32`, `half`, `qint8`.
    filter: A `Tensor`. Must have the same type as `conv_input`.
    bias: A `Tensor`. Must be one of the following types: `float32`, `half`.
    side_input: A `Tensor`. Must have the same type as `conv_input`.
    conv_input_scale: A `Tensor` of type `float32`.
    side_input_scale: A `Tensor` of type `float32`.
    strides: A list of `ints`.
    padding: A `string` from: `"SAME", "VALID"`.
    data_format: An optional `string` from: `"NHWC", "NCHW", "NCHW_VECT_C"`. Defaults to `"NHWC"`.
    filter_format: An optional `string` from: `"HWIO", "OIHW", "OIHW_VECT_I"`. Defaults to `"HWIO"`.
    activation_mode: An optional `string` from: `"Relu", "None"`. Defaults to `"Relu"`.
    dilations: An optional list of `ints`. Defaults to `[1, 1, 1, 1]`.
    name: A name for the operation (optional).

  Returns:
    A `Tensor`. Has the same type as `conv_input`.
  """
    _ctx = _context._context
    if _ctx is not None and _ctx._eager_context.is_eager:
        try:
            _result = _pywrap_tensorflow.TFE_Py_FastPathExecute(
                _ctx._context_handle, _ctx._eager_context.device_name,
                "FusedConv2DBiasActivation", name,
                _ctx._post_execution_callbacks, conv_input, filter, bias,
                side_input, conv_input_scale, side_input_scale, "strides",
                strides, "padding", padding, "data_format", data_format,
                "filter_format", filter_format, "activation_mode",
                activation_mode, "dilations", dilations)
            return _result
        except _core._FallbackException:
            try:
                return fused_conv2d_bias_activation_eager_fallback(
                    conv_input,
                    filter,
                    bias,
                    side_input,
                    conv_input_scale,
                    side_input_scale,
                    strides=strides,
                    padding=padding,
                    data_format=data_format,
                    filter_format=filter_format,
                    activation_mode=activation_mode,
                    dilations=dilations,
                    name=name,
                    ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
            except (TypeError, ValueError):
                result = _dispatch.dispatch(fused_conv2d_bias_activation,
                                            conv_input=conv_input,
                                            filter=filter,
                                            bias=bias,
                                            side_input=side_input,
                                            conv_input_scale=conv_input_scale,
                                            side_input_scale=side_input_scale,
                                            strides=strides,
                                            padding=padding,
                                            data_format=data_format,
                                            filter_format=filter_format,
                                            activation_mode=activation_mode,
                                            dilations=dilations,
                                            name=name)
                if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
                    return result
                raise
        except _core._NotOkStatusException as e:
            if name is not None:
                message = e.message + " name: " + name
            else:
                message = e.message
            _six.raise_from(_core._status_to_exception(e.code, message), None)
    # Add nodes to the TensorFlow graph.
    if not isinstance(strides, (list, tuple)):
        raise TypeError("Expected list for 'strides' argument to "
                        "'fused_conv2d_bias_activation' Op, not %r." % strides)
    strides = [_execute.make_int(_i, "strides") for _i in strides]
    padding = _execute.make_str(padding, "padding")
    if data_format is None:
        data_format = "NHWC"
    data_format = _execute.make_str(data_format, "data_format")
    if filter_format is None:
        filter_format = "HWIO"
    filter_format = _execute.make_str(filter_format, "filter_format")
    if activation_mode is None:
        activation_mode = "Relu"
    activation_mode = _execute.make_str(activation_mode, "activation_mode")
    if dilations is None:
        dilations = [1, 1, 1, 1]
    if not isinstance(dilations, (list, tuple)):
        raise TypeError("Expected list for 'dilations' argument to "
                        "'fused_conv2d_bias_activation' Op, not %r." %
                        dilations)
    dilations = [_execute.make_int(_i, "dilations") for _i in dilations]
    try:
        _, _, _op = _op_def_lib._apply_op_helper(
            "FusedConv2DBiasActivation",
            conv_input=conv_input,
            filter=filter,
            bias=bias,
            side_input=side_input,
            conv_input_scale=conv_input_scale,
            side_input_scale=side_input_scale,
            strides=strides,
            padding=padding,
            data_format=data_format,
            filter_format=filter_format,
            activation_mode=activation_mode,
            dilations=dilations,
            name=name)
    except (TypeError, ValueError):
        result = _dispatch.dispatch(fused_conv2d_bias_activation,
                                    conv_input=conv_input,
                                    filter=filter,
                                    bias=bias,
                                    side_input=side_input,
                                    conv_input_scale=conv_input_scale,
                                    side_input_scale=side_input_scale,
                                    strides=strides,
                                    padding=padding,
                                    data_format=data_format,
                                    filter_format=filter_format,
                                    activation_mode=activation_mode,
                                    dilations=dilations,
                                    name=name)
        if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
            return result
        raise
    _result = _op.outputs[:]
    _inputs_flat = _op.inputs
    _attrs = ("T", _op.get_attr("T"), "Tbias", _op.get_attr("Tbias"),
              "strides", _op.get_attr("strides"), "padding",
              _op.get_attr("padding"), "data_format",
              _op.get_attr("data_format"), "filter_format",
              _op.get_attr("filter_format"), "activation_mode",
              _op.get_attr("activation_mode"), "dilations",
              _op.get_attr("dilations"))
    _execute.record_gradient("FusedConv2DBiasActivation", _inputs_flat, _attrs,
                             _result, name)
    _result, = _result
    return _result
示例#22
0
def masked_matmul(a, b, mask_indices, transpose_a, transpose_b, name=None):
    r"""Computes the product a * b, but only for indices (i, j) in mask_indices. The

  result is stored in prod_values, a rank 1 tensor, such that for all i,
  prod_values[i] = (a * b)[mask_indices[i, 0], mask_indices[i, 1]].
  Note that the shapes of the input matrices a, b should be compatible (after
  transposing as specified by the arguments transpose_a and transpose_b).

  Input arguments:

  Args:
    a: A `Tensor` of type `float32`. A rank 2 tensor of shape [m, n].
    b: A `Tensor` of type `float32`.
      A rank 2 tensor of shape [s, t]. The inner dimensions of a and b should match
      after transposition.
    mask_indices: A `Tensor` of type `int64`.
      A rank 2 tensor, of shape [nnz, 2] where nnz is the number of
      non-zero elements in the output. The indices are not assumed to be in
      lexicographic, or any particular order.
      For all i, mask_indices[i, :] should represent a valid index of the product
      matrix (a * b) (after transposition). That is:
      mask_indices[i, 0] should be in [0, m) if !transpose_a, and in [0, n)
        otherwise.
      mask_indices[i, 1] should be in [0, t) if !transpose_b, and in [0, s)
        otherwise.
    transpose_a: A `Tensor` of type `bool`.
      A boolean, specifies whether to transpose the matrix a.
    transpose_b: A `Tensor` of type `bool`.
      A boolean, specifies whether to transpose the matrix b.

      Output arguments:
    name: A name for the operation (optional).

  Returns:
    A `Tensor` of type `float32`.
    A rank 1 tensor of shape [nnz], representing the values of the
    non-zero elements in the product, such that for all i,
    prod_values[i] = (a * b)[mask_indices[i, 0], mask_indices[i, 1]].
  """
    _ctx = _context._context or _context.context()
    if _ctx is not None and _ctx._thread_local_data.is_eager:
        try:
            _result = _pywrap_tensorflow.TFE_Py_FastPathExecute(
                _ctx._context_handle, _ctx._thread_local_data.device_name,
                "MaskedMatmul", name, _ctx._post_execution_callbacks, a, b,
                mask_indices, transpose_a, transpose_b)
            return _result
        except _core._FallbackException:
            try:
                return masked_matmul_eager_fallback(a,
                                                    b,
                                                    mask_indices,
                                                    transpose_a,
                                                    transpose_b,
                                                    name=name,
                                                    ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
            except (TypeError, ValueError):
                result = _dispatch.dispatch(masked_matmul,
                                            a=a,
                                            b=b,
                                            mask_indices=mask_indices,
                                            transpose_a=transpose_a,
                                            transpose_b=transpose_b,
                                            name=name)
                if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
                    return result
                raise
        except _core._NotOkStatusException as e:
            if name is not None:
                message = e.message + " name: " + name
            else:
                message = e.message
            _six.raise_from(_core._status_to_exception(e.code, message), None)
    # Add nodes to the TensorFlow graph.
    try:
        _, _, _op = _op_def_lib._apply_op_helper("MaskedMatmul",
                                                 a=a,
                                                 b=b,
                                                 mask_indices=mask_indices,
                                                 transpose_a=transpose_a,
                                                 transpose_b=transpose_b,
                                                 name=name)
    except (TypeError, ValueError):
        result = _dispatch.dispatch(masked_matmul,
                                    a=a,
                                    b=b,
                                    mask_indices=mask_indices,
                                    transpose_a=transpose_a,
                                    transpose_b=transpose_b,
                                    name=name)
        if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
            return result
        raise
    _result = _op.outputs[:]
    _inputs_flat = _op.inputs
    _attrs = None
    _execute.record_gradient("MaskedMatmul", _inputs_flat, _attrs, _result,
                             name)
    _result, = _result
    return _result
def invert(x, name=None):
    r"""Invert (flip) each bit of supported types; for example, type `uint8` value 01010101 becomes 10101010.

  Flip each bit of supported types.  For example, type `int8` (decimal 2) binary 00000010 becomes (decimal -3) binary 11111101.
  This operation is performed on each element of the tensor argument `x`.

  Example:
  ```python
  import tensorflow as tf
  from tensorflow.python.ops import bitwise_ops

  # flip 2 (00000010) to -3 (11111101)
  tf.assert_equal(-3, bitwise_ops.invert(2))

  dtype_list = [dtypes.int8, dtypes.int16, dtypes.int32, dtypes.int64,
                dtypes.uint8, dtypes.uint16, dtypes.uint32, dtypes.uint64]

  inputs = [0, 5, 3, 14]
  for dtype in dtype_list:
    # Because of issues with negative numbers, let's test this indirectly.
    # 1. invert(a) and a = 0
    # 2. invert(a) or a = invert(0)
    input_tensor = tf.constant([0, 5, 3, 14], dtype=dtype)
    not_a_and_a, not_a_or_a, not_0 = [bitwise_ops.bitwise_and(
                                        input_tensor, bitwise_ops.invert(input_tensor)),
                                      bitwise_ops.bitwise_or(
                                        input_tensor, bitwise_ops.invert(input_tensor)),
                                      bitwise_ops.invert(
                                        tf.constant(0, dtype=dtype))]

    expected = tf.constant([0, 0, 0, 0], dtype=tf.float32)
    tf.assert_equal(tf.cast(not_a_and_a, tf.float32), expected)

    expected = tf.cast([not_0] * 4, tf.float32)
    tf.assert_equal(tf.cast(not_a_or_a, tf.float32), expected)

    # For unsigned dtypes let's also check the result directly.
    if dtype.is_unsigned:
      inverted = bitwise_ops.invert(input_tensor)
      expected = tf.constant([dtype.max - x for x in inputs], dtype=tf.float32)
      tf.assert_equal(tf.cast(inverted, tf.float32), tf.cast(expected, tf.float32))
  ```

  Args:
    x: A `Tensor`. Must be one of the following types: `int8`, `int16`, `int32`, `int64`, `uint8`, `uint16`, `uint32`, `uint64`.
    name: A name for the operation (optional).

  Returns:
    A `Tensor`. Has the same type as `x`.
  """
    _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, "Invert", name,
                tld.op_callbacks, x)
            return _result
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
        except _core._FallbackException:
            pass
        try:
            return invert_eager_fallback(x, name=name, ctx=_ctx)
        except _core._SymbolicException:
            pass  # Add nodes to the TensorFlow graph.
        except (TypeError, ValueError):
            result = _dispatch.dispatch(invert, (), dict(x=x, 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("Invert",
                                                               x=x,
                                                               name=name)
    except (TypeError, ValueError):
        result = _dispatch.dispatch(invert, (), dict(x=x, name=name))
        if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
            return result
        raise
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("T", _op._get_attr_type("T"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("Invert", _inputs_flat, _attrs, _result)
    _result, = _result
    return _result
def audio_microfrontend(audio, sample_rate=16000, window_size=25, window_step=10, num_channels=32, upper_band_limit=7500, lower_band_limit=125, smoothing_bits=10, even_smoothing=0.025, odd_smoothing=0.06, min_signal_remaining=0.05, enable_pcan=False, pcan_strength=0.95, pcan_offset=80, gain_bits=21, enable_log=True, scale_shift=6, left_context=0, right_context=0, frame_stride=1, zero_padding=False, out_scale=1, out_type=_dtypes.uint16, name=None):
  r"""Audio Microfrontend Op.

  This Op converts a sequence of audio data into one or more
  feature vectors containing filterbanks of the input. The
  conversion process uses a lightweight library to perform:

  1. A slicing window function
  2. Short-time FFTs
  3. Filterbank calculations
  4. Noise reduction
  5. PCAN Auto Gain Control
  6. Logarithmic scaling

  Arguments
    audio: 1D Tensor, int16 audio data in temporal ordering.
    sample_rate: Integer, the sample rate of the audio in Hz.
    window_size: Integer, length of desired time frames in ms.
    window_step: Integer, length of step size for the next frame in ms.
    num_channels: Integer, the number of filterbank channels to use.
    upper_band_limit: Float, the highest frequency included in the filterbanks.
    lower_band_limit: Float, the lowest frequency included in the filterbanks.
    smoothing_bits: Int, scale up signal by 2^(smoothing_bits) before reduction.
    even_smoothing: Float, smoothing coefficient for even-numbered channels.
    odd_smoothing: Float, smoothing coefficient for odd-numbered channels.
    min_signal_remaining: Float, fraction of signal to preserve in smoothing.
    enable_pcan: Bool, enable PCAN auto gain control.
    pcan_strength: Float, gain normalization exponent.
    pcan_offset: Float, positive value added in the normalization denominator.
    gain_bits: Int, number of fractional bits in the gain.
    enable_log: Bool, enable logarithmic scaling of filterbanks.
    scale_shift: Integer, scale filterbanks by 2^(scale_shift).
    left_context: Integer, number of preceding frames to attach to each frame.
    right_context: Integer, number of preceding frames to attach to each frame.
    frame_stride: Integer, M frames to skip over, where output[n] = frame[n*M].
    zero_padding: Bool, if left/right context is out-of-bounds, attach frame of
                  zeroes. Otherwise, frame[0] or frame[size-1] will be copied.
    out_scale: Integer, divide all filterbanks by this number.
    out_type: DType, type of the output Tensor, defaults to UINT16.

  Returns
    filterbanks: 2D Tensor, each row is a time frame, each column is a channel.

  Args:
    audio: A `Tensor` of type `int16`.
    sample_rate: An optional `int`. Defaults to `16000`.
    window_size: An optional `int`. Defaults to `25`.
    window_step: An optional `int`. Defaults to `10`.
    num_channels: An optional `int`. Defaults to `32`.
    upper_band_limit: An optional `float`. Defaults to `7500`.
    lower_band_limit: An optional `float`. Defaults to `125`.
    smoothing_bits: An optional `int`. Defaults to `10`.
    even_smoothing: An optional `float`. Defaults to `0.025`.
    odd_smoothing: An optional `float`. Defaults to `0.06`.
    min_signal_remaining: An optional `float`. Defaults to `0.05`.
    enable_pcan: An optional `bool`. Defaults to `False`.
    pcan_strength: An optional `float`. Defaults to `0.95`.
    pcan_offset: An optional `float`. Defaults to `80`.
    gain_bits: An optional `int`. Defaults to `21`.
    enable_log: An optional `bool`. Defaults to `True`.
    scale_shift: An optional `int`. Defaults to `6`.
    left_context: An optional `int`. Defaults to `0`.
    right_context: An optional `int`. Defaults to `0`.
    frame_stride: An optional `int`. Defaults to `1`.
    zero_padding: An optional `bool`. Defaults to `False`.
    out_scale: An optional `int`. Defaults to `1`.
    out_type: An optional `tf.DType` from: `tf.uint16, tf.float32`. Defaults to `tf.uint16`.
    name: A name for the operation (optional).

  Returns:
    A `Tensor` of type `out_type`.
  """
  _ctx = _context._context or _context.context()
  if _ctx is not None and _ctx._thread_local_data.is_eager:
    try:
      _result = _pywrap_tensorflow.TFE_Py_FastPathExecute(
        _ctx._context_handle, _ctx._thread_local_data.device_name,
        "AudioMicrofrontend", name, _ctx._post_execution_callbacks, audio,
        "sample_rate", sample_rate, "window_size", window_size, "window_step",
        window_step, "num_channels", num_channels, "upper_band_limit",
        upper_band_limit, "lower_band_limit", lower_band_limit,
        "smoothing_bits", smoothing_bits, "even_smoothing", even_smoothing,
        "odd_smoothing", odd_smoothing, "min_signal_remaining",
        min_signal_remaining, "enable_pcan", enable_pcan, "pcan_strength",
        pcan_strength, "pcan_offset", pcan_offset, "gain_bits", gain_bits,
        "enable_log", enable_log, "scale_shift", scale_shift, "left_context",
        left_context, "right_context", right_context, "frame_stride",
        frame_stride, "zero_padding", zero_padding, "out_scale", out_scale,
        "out_type", out_type)
      return _result
    except _core._FallbackException:
      try:
        return audio_microfrontend_eager_fallback(
            audio, sample_rate=sample_rate, window_size=window_size,
            window_step=window_step, num_channels=num_channels,
            upper_band_limit=upper_band_limit,
            lower_band_limit=lower_band_limit, smoothing_bits=smoothing_bits,
            even_smoothing=even_smoothing, odd_smoothing=odd_smoothing,
            min_signal_remaining=min_signal_remaining,
            enable_pcan=enable_pcan, pcan_strength=pcan_strength,
            pcan_offset=pcan_offset, gain_bits=gain_bits,
            enable_log=enable_log, scale_shift=scale_shift,
            left_context=left_context, right_context=right_context,
            frame_stride=frame_stride, zero_padding=zero_padding,
            out_scale=out_scale, out_type=out_type, name=name, ctx=_ctx)
      except _core._SymbolicException:
        pass  # Add nodes to the TensorFlow graph.
      except (TypeError, ValueError):
        result = _dispatch.dispatch(
              audio_microfrontend, audio=audio, sample_rate=sample_rate,
                                   window_size=window_size,
                                   window_step=window_step,
                                   num_channels=num_channels,
                                   upper_band_limit=upper_band_limit,
                                   lower_band_limit=lower_band_limit,
                                   smoothing_bits=smoothing_bits,
                                   even_smoothing=even_smoothing,
                                   odd_smoothing=odd_smoothing,
                                   min_signal_remaining=min_signal_remaining,
                                   enable_pcan=enable_pcan,
                                   pcan_strength=pcan_strength,
                                   pcan_offset=pcan_offset,
                                   gain_bits=gain_bits, enable_log=enable_log,
                                   scale_shift=scale_shift,
                                   left_context=left_context,
                                   right_context=right_context,
                                   frame_stride=frame_stride,
                                   zero_padding=zero_padding,
                                   out_scale=out_scale, out_type=out_type,
                                   name=name)
        if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
          return result
        raise
    except _core._NotOkStatusException as e:
      if name is not None:
        message = e.message + " name: " + name
      else:
        message = e.message
      _six.raise_from(_core._status_to_exception(e.code, message), None)
  # Add nodes to the TensorFlow graph.
  if sample_rate is None:
    sample_rate = 16000
  sample_rate = _execute.make_int(sample_rate, "sample_rate")
  if window_size is None:
    window_size = 25
  window_size = _execute.make_int(window_size, "window_size")
  if window_step is None:
    window_step = 10
  window_step = _execute.make_int(window_step, "window_step")
  if num_channels is None:
    num_channels = 32
  num_channels = _execute.make_int(num_channels, "num_channels")
  if upper_band_limit is None:
    upper_band_limit = 7500
  upper_band_limit = _execute.make_float(upper_band_limit, "upper_band_limit")
  if lower_band_limit is None:
    lower_band_limit = 125
  lower_band_limit = _execute.make_float(lower_band_limit, "lower_band_limit")
  if smoothing_bits is None:
    smoothing_bits = 10
  smoothing_bits = _execute.make_int(smoothing_bits, "smoothing_bits")
  if even_smoothing is None:
    even_smoothing = 0.025
  even_smoothing = _execute.make_float(even_smoothing, "even_smoothing")
  if odd_smoothing is None:
    odd_smoothing = 0.06
  odd_smoothing = _execute.make_float(odd_smoothing, "odd_smoothing")
  if min_signal_remaining is None:
    min_signal_remaining = 0.05
  min_signal_remaining = _execute.make_float(min_signal_remaining, "min_signal_remaining")
  if enable_pcan is None:
    enable_pcan = False
  enable_pcan = _execute.make_bool(enable_pcan, "enable_pcan")
  if pcan_strength is None:
    pcan_strength = 0.95
  pcan_strength = _execute.make_float(pcan_strength, "pcan_strength")
  if pcan_offset is None:
    pcan_offset = 80
  pcan_offset = _execute.make_float(pcan_offset, "pcan_offset")
  if gain_bits is None:
    gain_bits = 21
  gain_bits = _execute.make_int(gain_bits, "gain_bits")
  if enable_log is None:
    enable_log = True
  enable_log = _execute.make_bool(enable_log, "enable_log")
  if scale_shift is None:
    scale_shift = 6
  scale_shift = _execute.make_int(scale_shift, "scale_shift")
  if left_context is None:
    left_context = 0
  left_context = _execute.make_int(left_context, "left_context")
  if right_context is None:
    right_context = 0
  right_context = _execute.make_int(right_context, "right_context")
  if frame_stride is None:
    frame_stride = 1
  frame_stride = _execute.make_int(frame_stride, "frame_stride")
  if zero_padding is None:
    zero_padding = False
  zero_padding = _execute.make_bool(zero_padding, "zero_padding")
  if out_scale is None:
    out_scale = 1
  out_scale = _execute.make_int(out_scale, "out_scale")
  if out_type is None:
    out_type = _dtypes.uint16
  out_type = _execute.make_type(out_type, "out_type")
  try:
    _, _, _op = _op_def_lib._apply_op_helper(
        "AudioMicrofrontend", audio=audio, sample_rate=sample_rate,
                              window_size=window_size,
                              window_step=window_step,
                              num_channels=num_channels,
                              upper_band_limit=upper_band_limit,
                              lower_band_limit=lower_band_limit,
                              smoothing_bits=smoothing_bits,
                              even_smoothing=even_smoothing,
                              odd_smoothing=odd_smoothing,
                              min_signal_remaining=min_signal_remaining,
                              enable_pcan=enable_pcan,
                              pcan_strength=pcan_strength,
                              pcan_offset=pcan_offset, gain_bits=gain_bits,
                              enable_log=enable_log, scale_shift=scale_shift,
                              left_context=left_context,
                              right_context=right_context,
                              frame_stride=frame_stride,
                              zero_padding=zero_padding, out_scale=out_scale,
                              out_type=out_type, name=name)
  except (TypeError, ValueError):
    result = _dispatch.dispatch(
          audio_microfrontend, audio=audio, sample_rate=sample_rate,
                               window_size=window_size,
                               window_step=window_step,
                               num_channels=num_channels,
                               upper_band_limit=upper_band_limit,
                               lower_band_limit=lower_band_limit,
                               smoothing_bits=smoothing_bits,
                               even_smoothing=even_smoothing,
                               odd_smoothing=odd_smoothing,
                               min_signal_remaining=min_signal_remaining,
                               enable_pcan=enable_pcan,
                               pcan_strength=pcan_strength,
                               pcan_offset=pcan_offset, gain_bits=gain_bits,
                               enable_log=enable_log, scale_shift=scale_shift,
                               left_context=left_context,
                               right_context=right_context,
                               frame_stride=frame_stride,
                               zero_padding=zero_padding, out_scale=out_scale,
                               out_type=out_type, name=name)
    if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
      return result
    raise
  _result = _op.outputs[:]
  _inputs_flat = _op.inputs
  _attrs = ("sample_rate", _op.get_attr("sample_rate"), "window_size",
            _op.get_attr("window_size"), "window_step",
            _op.get_attr("window_step"), "num_channels",
            _op.get_attr("num_channels"), "upper_band_limit",
            _op.get_attr("upper_band_limit"), "lower_band_limit",
            _op.get_attr("lower_band_limit"), "smoothing_bits",
            _op.get_attr("smoothing_bits"), "even_smoothing",
            _op.get_attr("even_smoothing"), "odd_smoothing",
            _op.get_attr("odd_smoothing"), "min_signal_remaining",
            _op.get_attr("min_signal_remaining"), "enable_pcan",
            _op.get_attr("enable_pcan"), "pcan_strength",
            _op.get_attr("pcan_strength"), "pcan_offset",
            _op.get_attr("pcan_offset"), "gain_bits",
            _op.get_attr("gain_bits"), "enable_log",
            _op.get_attr("enable_log"), "scale_shift",
            _op.get_attr("scale_shift"), "left_context",
            _op.get_attr("left_context"), "right_context",
            _op.get_attr("right_context"), "frame_stride",
            _op.get_attr("frame_stride"), "zero_padding",
            _op.get_attr("zero_padding"), "out_scale",
            _op.get_attr("out_scale"), "out_type",
            _op._get_attr_type("out_type"))
  _execute.record_gradient(
      "AudioMicrofrontend", _inputs_flat, _attrs, _result, name)
  _result, = _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._FallbackException:
            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,
                                            resource_name=resource_name,
                                            filename=filename,
                                            delete_resource=delete_resource,
                                            name=name)
                if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
                    return result
                raise
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
    # 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,
                                    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 grow_tree_ensemble(tree_ensemble_handle,
                       stamp_token,
                       next_stamp_token,
                       learning_rate,
                       dropout_seed,
                       max_tree_depth,
                       weak_learner_type,
                       partition_ids,
                       gains,
                       splits,
                       learner_config,
                       center_bias,
                       name=None):
    r"""Grows the tree ensemble by either adding a layer to the last tree being grown

  or by starting a new tree.

  Args:
    tree_ensemble_handle: A `Tensor` of type `resource`.
      Handle to the ensemble variable.
    stamp_token: A `Tensor` of type `int64`.
      Stamp token for validating operation consistency.
    next_stamp_token: A `Tensor` of type `int64`.
      Stamp token to be used for the next iteration.
    learning_rate: A `Tensor` of type `float32`. Scalar learning rate.
    dropout_seed: A `Tensor` of type `int64`.
    max_tree_depth: A `Tensor` of type `int32`.
    weak_learner_type: A `Tensor` of type `int32`.
      The type of weak learner to use.
    partition_ids: A list of `Tensor` objects with type `int32`.
      List of Rank 1 Tensors containing partition Id per candidate.
    gains: A list with the same length as `partition_ids` of `Tensor` objects with type `float32`.
      List of Rank 1 Tensors containing gains per candidate.
    splits: A list with the same length as `partition_ids` of `Tensor` objects with type `string`.
      List of Rank 1 Tensors containing serialized SplitInfo protos per candidate.
    learner_config: A `string`.
      Config for the learner of type LearnerConfig proto.
    center_bias: A `bool`.
    name: A name for the operation (optional).

  Returns:
    The created Operation.
  """
    _ctx = _context._context
    if _ctx is not None and _ctx._eager_context.is_eager:
        try:
            _result = _pywrap_tensorflow.TFE_Py_FastPathExecute(
                _ctx._context_handle, _ctx._eager_context.device_name,
                "GrowTreeEnsemble", name, _ctx._post_execution_callbacks,
                tree_ensemble_handle, stamp_token, next_stamp_token,
                learning_rate, dropout_seed, max_tree_depth, weak_learner_type,
                partition_ids, gains, splits, "learner_config", learner_config,
                "center_bias", center_bias)
            return _result
        except _core._FallbackException:
            try:
                return grow_tree_ensemble_eager_fallback(
                    tree_ensemble_handle,
                    stamp_token,
                    next_stamp_token,
                    learning_rate,
                    dropout_seed,
                    max_tree_depth,
                    weak_learner_type,
                    partition_ids,
                    gains,
                    splits,
                    learner_config=learner_config,
                    center_bias=center_bias,
                    name=name,
                    ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
            except (TypeError, ValueError):
                result = _dispatch.dispatch(
                    grow_tree_ensemble,
                    tree_ensemble_handle=tree_ensemble_handle,
                    stamp_token=stamp_token,
                    next_stamp_token=next_stamp_token,
                    learning_rate=learning_rate,
                    dropout_seed=dropout_seed,
                    max_tree_depth=max_tree_depth,
                    weak_learner_type=weak_learner_type,
                    partition_ids=partition_ids,
                    gains=gains,
                    splits=splits,
                    learner_config=learner_config,
                    center_bias=center_bias,
                    name=name)
                if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
                    return result
                raise
        except _core._NotOkStatusException as e:
            if name is not None:
                message = e.message + " name: " + name
            else:
                message = e.message
            _six.raise_from(_core._status_to_exception(e.code, message), None)
    # Add nodes to the TensorFlow graph.
    if not isinstance(partition_ids, (list, tuple)):
        raise TypeError("Expected list for 'partition_ids' argument to "
                        "'grow_tree_ensemble' Op, not %r." % partition_ids)
    _attr_num_handlers = len(partition_ids)
    if not isinstance(gains, (list, tuple)):
        raise TypeError("Expected list for 'gains' argument to "
                        "'grow_tree_ensemble' Op, not %r." % gains)
    if len(gains) != _attr_num_handlers:
        raise ValueError(
            "List argument 'gains' to 'grow_tree_ensemble' Op with length %d "
            "must match length %d of argument 'partition_ids'." %
            (len(gains), _attr_num_handlers))
    if not isinstance(splits, (list, tuple)):
        raise TypeError("Expected list for 'splits' argument to "
                        "'grow_tree_ensemble' Op, not %r." % splits)
    if len(splits) != _attr_num_handlers:
        raise ValueError(
            "List argument 'splits' to 'grow_tree_ensemble' Op with length %d "
            "must match length %d of argument 'partition_ids'." %
            (len(splits), _attr_num_handlers))
    learner_config = _execute.make_str(learner_config, "learner_config")
    center_bias = _execute.make_bool(center_bias, "center_bias")
    try:
        _, _, _op = _op_def_lib._apply_op_helper(
            "GrowTreeEnsemble",
            tree_ensemble_handle=tree_ensemble_handle,
            stamp_token=stamp_token,
            next_stamp_token=next_stamp_token,
            learning_rate=learning_rate,
            dropout_seed=dropout_seed,
            max_tree_depth=max_tree_depth,
            weak_learner_type=weak_learner_type,
            partition_ids=partition_ids,
            gains=gains,
            splits=splits,
            learner_config=learner_config,
            center_bias=center_bias,
            name=name)
    except (TypeError, ValueError):
        result = _dispatch.dispatch(grow_tree_ensemble,
                                    tree_ensemble_handle=tree_ensemble_handle,
                                    stamp_token=stamp_token,
                                    next_stamp_token=next_stamp_token,
                                    learning_rate=learning_rate,
                                    dropout_seed=dropout_seed,
                                    max_tree_depth=max_tree_depth,
                                    weak_learner_type=weak_learner_type,
                                    partition_ids=partition_ids,
                                    gains=gains,
                                    splits=splits,
                                    learner_config=learner_config,
                                    center_bias=center_bias,
                                    name=name)
        if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
            return result
        raise
    return _op
    _result = None
    return _result
示例#27
0
def generate_big_query_reader_partitions(project_id,
                                         dataset_id,
                                         table_id,
                                         columns,
                                         timestamp_millis,
                                         num_partitions,
                                         test_end_point="",
                                         name=None):
    r"""Generates serialized partition messages suitable for batch reads.

  This op should not be used directly by clients. Instead, the
  bigquery_reader_ops.py file defines a clean interface to the reader.

  Args:
    project_id: A `string`. GCP project ID.
    dataset_id: A `string`. BigQuery Dataset ID.
    table_id: A `string`. Table to read.
    columns: A list of `strings`.
      List of columns to read. Leave empty to read all columns.
    timestamp_millis: An `int`.
      Table snapshot timestamp in millis since epoch. Relative
      (negative or zero) snapshot times are not allowed. For more details, see
      'Table Decorators' in BigQuery docs.
    num_partitions: An `int`. Number of partitions to split the table into.
    test_end_point: An optional `string`. Defaults to `""`.
      Do not use. For testing purposes only.
    name: A name for the operation (optional).

  Returns:
    A `Tensor` of type `string`. Serialized table partitions.
  """
    _ctx = _context._context or _context.context()
    if _ctx is not None and _ctx._thread_local_data.is_eager:
        try:
            _result = _pywrap_tensorflow.TFE_Py_FastPathExecute(
                _ctx._context_handle, _ctx._thread_local_data.device_name,
                "GenerateBigQueryReaderPartitions", name,
                _ctx.post_execution_callbacks, "project_id", project_id,
                "dataset_id", dataset_id, "table_id", table_id, "columns",
                columns, "timestamp_millis", timestamp_millis,
                "num_partitions", num_partitions, "test_end_point",
                test_end_point)
            return _result
        except _core._FallbackException:
            try:
                return generate_big_query_reader_partitions_eager_fallback(
                    project_id=project_id,
                    dataset_id=dataset_id,
                    table_id=table_id,
                    columns=columns,
                    timestamp_millis=timestamp_millis,
                    num_partitions=num_partitions,
                    test_end_point=test_end_point,
                    name=name,
                    ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
            except (TypeError, ValueError):
                result = _dispatch.dispatch(
                    generate_big_query_reader_partitions,
                    project_id=project_id,
                    dataset_id=dataset_id,
                    table_id=table_id,
                    columns=columns,
                    timestamp_millis=timestamp_millis,
                    num_partitions=num_partitions,
                    test_end_point=test_end_point,
                    name=name)
                if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
                    return result
                raise
        except _core._NotOkStatusException as e:
            if name is not None:
                message = e.message + " name: " + name
            else:
                message = e.message
            _six.raise_from(_core._status_to_exception(e.code, message), None)
    # Add nodes to the TensorFlow graph.
    project_id = _execute.make_str(project_id, "project_id")
    dataset_id = _execute.make_str(dataset_id, "dataset_id")
    table_id = _execute.make_str(table_id, "table_id")
    if not isinstance(columns, (list, tuple)):
        raise TypeError("Expected list for 'columns' argument to "
                        "'generate_big_query_reader_partitions' Op, not %r." %
                        columns)
    columns = [_execute.make_str(_s, "columns") for _s in columns]
    timestamp_millis = _execute.make_int(timestamp_millis, "timestamp_millis")
    num_partitions = _execute.make_int(num_partitions, "num_partitions")
    if test_end_point is None:
        test_end_point = ""
    test_end_point = _execute.make_str(test_end_point, "test_end_point")
    try:
        _, _, _op = _op_def_lib._apply_op_helper(
            "GenerateBigQueryReaderPartitions",
            project_id=project_id,
            dataset_id=dataset_id,
            table_id=table_id,
            columns=columns,
            timestamp_millis=timestamp_millis,
            num_partitions=num_partitions,
            test_end_point=test_end_point,
            name=name)
    except (TypeError, ValueError):
        result = _dispatch.dispatch(generate_big_query_reader_partitions,
                                    project_id=project_id,
                                    dataset_id=dataset_id,
                                    table_id=table_id,
                                    columns=columns,
                                    timestamp_millis=timestamp_millis,
                                    num_partitions=num_partitions,
                                    test_end_point=test_end_point,
                                    name=name)
        if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
            return result
        raise
    _result = _op.outputs[:]
    _inputs_flat = _op.inputs
    _attrs = ("project_id", _op.get_attr("project_id"), "dataset_id",
              _op.get_attr("dataset_id"), "table_id", _op.get_attr("table_id"),
              "columns", _op.get_attr("columns"), "timestamp_millis",
              _op.get_attr("timestamp_millis"), "num_partitions",
              _op.get_attr("num_partitions"), "test_end_point",
              _op.get_attr("test_end_point"))
    _execute.record_gradient("GenerateBigQueryReaderPartitions", _inputs_flat,
                             _attrs, _result, name)
    _result, = _result
    return _result
示例#28
0
def center_tree_ensemble_bias(tree_ensemble_handle,
                              stamp_token,
                              next_stamp_token,
                              delta_updates,
                              learner_config,
                              centering_epsilon=0.01,
                              name=None):
    r"""Centers the tree ensemble bias before adding trees based on feature splits.

  Args:
    tree_ensemble_handle: A `Tensor` of type `resource`.
      Handle to the ensemble variable.
    stamp_token: A `Tensor` of type `int64`.
      Stamp token for validating operation consistency.
    next_stamp_token: A `Tensor` of type `int64`.
      Stamp token to be used for the next iteration.
    delta_updates: A `Tensor` of type `float32`.
      Rank 1 Tensor containing delta updates per bias dimension.
    learner_config: A `string`.
      Config for the learner of type LearnerConfig proto.
    centering_epsilon: An optional `float`. Defaults to `0.01`.
    name: A name for the operation (optional).

  Returns:
    A `Tensor` of type `bool`.
    Scalar indicating whether more centering is needed.
  """
    _ctx = _context._context
    if _ctx is not None and _ctx._eager_context.is_eager:
        try:
            _result = _pywrap_tensorflow.TFE_Py_FastPathExecute(
                _ctx._context_handle, _ctx._eager_context.device_name,
                "CenterTreeEnsembleBias", name, _ctx._post_execution_callbacks,
                tree_ensemble_handle, stamp_token, next_stamp_token,
                delta_updates, "learner_config", learner_config,
                "centering_epsilon", centering_epsilon)
            return _result
        except _core._FallbackException:
            try:
                return center_tree_ensemble_bias_eager_fallback(
                    tree_ensemble_handle,
                    stamp_token,
                    next_stamp_token,
                    delta_updates,
                    learner_config=learner_config,
                    centering_epsilon=centering_epsilon,
                    name=name,
                    ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
            except (TypeError, ValueError):
                result = _dispatch.dispatch(
                    center_tree_ensemble_bias,
                    tree_ensemble_handle=tree_ensemble_handle,
                    stamp_token=stamp_token,
                    next_stamp_token=next_stamp_token,
                    delta_updates=delta_updates,
                    learner_config=learner_config,
                    centering_epsilon=centering_epsilon,
                    name=name)
                if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
                    return result
                raise
        except _core._NotOkStatusException as e:
            if name is not None:
                message = e.message + " name: " + name
            else:
                message = e.message
            _six.raise_from(_core._status_to_exception(e.code, message), None)
    # Add nodes to the TensorFlow graph.
    learner_config = _execute.make_str(learner_config, "learner_config")
    if centering_epsilon is None:
        centering_epsilon = 0.01
    centering_epsilon = _execute.make_float(centering_epsilon,
                                            "centering_epsilon")
    try:
        _, _, _op = _op_def_lib._apply_op_helper(
            "CenterTreeEnsembleBias",
            tree_ensemble_handle=tree_ensemble_handle,
            stamp_token=stamp_token,
            next_stamp_token=next_stamp_token,
            delta_updates=delta_updates,
            learner_config=learner_config,
            centering_epsilon=centering_epsilon,
            name=name)
    except (TypeError, ValueError):
        result = _dispatch.dispatch(center_tree_ensemble_bias,
                                    tree_ensemble_handle=tree_ensemble_handle,
                                    stamp_token=stamp_token,
                                    next_stamp_token=next_stamp_token,
                                    delta_updates=delta_updates,
                                    learner_config=learner_config,
                                    centering_epsilon=centering_epsilon,
                                    name=name)
        if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
            return result
        raise
    _result = _op.outputs[:]
    _inputs_flat = _op.inputs
    _attrs = ("learner_config", _op.get_attr("learner_config"),
              "centering_epsilon", _op.get_attr("centering_epsilon"))
    _execute.record_gradient("CenterTreeEnsembleBias", _inputs_flat, _attrs,
                             _result, name)
    _result, = _result
    return _result
示例#29
0
def reinterpret_string_to_float(input_data, name=None):
    r"""   Converts byte arrays represented by strings to 32-bit

     floating point numbers. The output numbers themselves are meaningless, and
     should only be used in == comparisons.

     input_data: A batch of string features as a 2-d tensor; `input_data[i][j]`
       gives the j-th feature of the i-th input.
     output_data: A tensor of the same shape as input_data but the values are
       float32.

  Args:
    input_data: A `Tensor` of type `string`.
    name: A name for the operation (optional).

  Returns:
    A `Tensor` of type `float32`.
  """
    _ctx = _context._context
    if _ctx is not None and _ctx._eager_context.is_eager:
        try:
            _result = _pywrap_tensorflow.TFE_Py_FastPathExecute(
                _ctx._context_handle, _ctx._eager_context.device_name,
                "ReinterpretStringToFloat", name,
                _ctx._post_execution_callbacks, input_data)
            return _result
        except _core._FallbackException:
            try:
                return reinterpret_string_to_float_eager_fallback(input_data,
                                                                  name=name,
                                                                  ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
            except (TypeError, ValueError):
                result = _dispatch.dispatch(reinterpret_string_to_float,
                                            input_data=input_data,
                                            name=name)
                if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
                    return result
                raise
        except _core._NotOkStatusException as e:
            if name is not None:
                message = e.message + " name: " + name
            else:
                message = e.message
            _six.raise_from(_core._status_to_exception(e.code, message), None)
    # Add nodes to the TensorFlow graph.
    try:
        _, _, _op = _op_def_lib._apply_op_helper("ReinterpretStringToFloat",
                                                 input_data=input_data,
                                                 name=name)
    except (TypeError, ValueError):
        result = _dispatch.dispatch(reinterpret_string_to_float,
                                    input_data=input_data,
                                    name=name)
        if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
            return result
        raise
    _result = _op.outputs[:]
    _inputs_flat = _op.inputs
    _attrs = None
    _execute.record_gradient("ReinterpretStringToFloat", _inputs_flat, _attrs,
                             _result, name)
    _result, = _result
    return _result
示例#30
0
def finalize_tree(tree_handle, stats_handle, params, name=None):
    r"""Puts the Leaf models inside the tree into their final form.

  If drop_final_class is true, the per-class probability prediction of the
  last class is not stored in the leaf models.

  Args:
    tree_handle: A `Tensor` of type `resource`. The handle to the tree.
    stats_handle: A `Tensor` of type `resource`. The handle to the stats.
    params: A `string`. A serialized TensorForestParams proto.
    name: A name for the operation (optional).

  Returns:
    The created Operation.
  """
    _ctx = _context._context or _context.context()
    if _ctx is not None and _ctx._thread_local_data.is_eager:
        try:
            _result = _pywrap_tensorflow.TFE_Py_FastPathExecute(
                _ctx._context_handle, _ctx._thread_local_data.device_name,
                "FinalizeTree", name, _ctx.post_execution_callbacks,
                tree_handle, stats_handle, "params", params)
            return _result
        except _core._FallbackException:
            try:
                return finalize_tree_eager_fallback(tree_handle,
                                                    stats_handle,
                                                    params=params,
                                                    name=name,
                                                    ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
            except (TypeError, ValueError):
                result = _dispatch.dispatch(finalize_tree,
                                            tree_handle=tree_handle,
                                            stats_handle=stats_handle,
                                            params=params,
                                            name=name)
                if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
                    return result
                raise
        except _core._NotOkStatusException as e:
            if name is not None:
                message = e.message + " name: " + name
            else:
                message = e.message
            _six.raise_from(_core._status_to_exception(e.code, message), None)
    # Add nodes to the TensorFlow graph.
    params = _execute.make_str(params, "params")
    try:
        _, _, _op = _op_def_lib._apply_op_helper("FinalizeTree",
                                                 tree_handle=tree_handle,
                                                 stats_handle=stats_handle,
                                                 params=params,
                                                 name=name)
    except (TypeError, ValueError):
        result = _dispatch.dispatch(finalize_tree,
                                    tree_handle=tree_handle,
                                    stats_handle=stats_handle,
                                    params=params,
                                    name=name)
        if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
            return result
        raise
    return _op
    _result = None
    return _result