コード例 #1
0
def ragged_tensor_to_tensor_eager_fallback(shape, values, default_value,
                                           row_partition_tensors,
                                           row_partition_types, name, ctx):
    if not isinstance(row_partition_tensors, (list, tuple)):
        raise TypeError(
            "Expected list for 'row_partition_tensors' argument to "
            "'ragged_tensor_to_tensor' Op, not %r." % row_partition_tensors)
    _attr_num_row_partition_tensors = len(row_partition_tensors)
    if not isinstance(row_partition_types, (list, tuple)):
        raise TypeError("Expected list for 'row_partition_types' argument to "
                        "'ragged_tensor_to_tensor' Op, not %r." %
                        row_partition_types)
    row_partition_types = [
        _execute.make_str(_s, "row_partition_types")
        for _s in row_partition_types
    ]
    _attr_T, _inputs_T = _execute.args_to_matching_eager(
        [values, default_value], ctx)
    (values, default_value) = _inputs_T
    _attr_Tindex, row_partition_tensors = _execute.args_to_matching_eager(
        list(row_partition_tensors), ctx)
    _attr_Tshape, (shape, ) = _execute.args_to_matching_eager([shape], ctx)
    _inputs_flat = [shape, values, default_value] + list(row_partition_tensors)
    _attrs = ("T", _attr_T, "Tindex", _attr_Tindex, "Tshape", _attr_Tshape,
              "num_row_partition_tensors", _attr_num_row_partition_tensors,
              "row_partition_types", row_partition_types)
    _result = _execute.execute(b"RaggedTensorToTensor",
                               1,
                               inputs=_inputs_flat,
                               attrs=_attrs,
                               ctx=ctx,
                               name=name)
    if _execute.must_record_gradient():
        _execute.record_gradient("RaggedTensorToTensor", _inputs_flat, _attrs,
                                 _result)
    _result, = _result
    return _result
コード例 #2
0
def tensor_forest_tree_is_initialized_op(tree_handle, name=None):
    r"""Checks whether a tree has been initialized.

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

  Returns:
    A `Tensor` of type `bool`.
  """
    _ctx = _context._context or _context.context()
    tld = _ctx._thread_local_data
    if tld.is_eager:
        try:
            _result = pywrap_tfe.TFE_Py_FastPathExecute(
                _ctx, "TensorForestTreeIsInitializedOp", name, tree_handle)
            return _result
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
        except _core._FallbackException:
            pass
        try:
            return tensor_forest_tree_is_initialized_op_eager_fallback(
                tree_handle, name=name, ctx=_ctx)
        except _core._SymbolicException:
            pass  # Add nodes to the TensorFlow graph.
    # Add nodes to the TensorFlow graph.
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "TensorForestTreeIsInitializedOp", tree_handle=tree_handle, name=name)
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ()
        _inputs_flat = _op.inputs
        _execute.record_gradient("TensorForestTreeIsInitializedOp",
                                 _inputs_flat, _attrs, _result)
    _result, = _result
    return _result
コード例 #3
0
def batch_fft2d(input, name=None):
  r"""TODO: add doc.

  Args:
    input: A `Tensor` of type `complex64`.
    name: A name for the operation (optional).

  Returns:
    A `Tensor` of type `complex64`.
  """
  _ctx = _context._context or _context.context()
  tld = _ctx._thread_local_data
  if tld.is_eager:
    try:
      _result = pywrap_tfe.TFE_Py_FastPathExecute(
        _ctx, "BatchFFT2D", name, input)
      return _result
    except _core._NotOkStatusException as e:
      _ops.raise_from_not_ok_status(e, name)
    except _core._FallbackException:
      pass
    try:
      return batch_fft2d_eager_fallback(
          input, name=name, ctx=_ctx)
    except _core._SymbolicException:
      pass  # Add nodes to the TensorFlow graph.
  # Add nodes to the TensorFlow graph.
  _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "BatchFFT2D", input=input, name=name)
  _result = _outputs[:]
  if _execute.must_record_gradient():
    _attrs = ()
    _inputs_flat = _op.inputs
    _execute.record_gradient(
        "BatchFFT2D", _inputs_flat, _attrs, _result)
  _result, = _result
  return _result
コード例 #4
0
ファイル: gen_count_ops.py プロジェクト: Miq313/RUHacks2021
def sparse_count_sparse_output_eager_fallback(indices, values, dense_shape,
                                              weights, binary_output,
                                              minlength, maxlength, name, ctx):
    binary_output = _execute.make_bool(binary_output, "binary_output")
    if minlength is None:
        minlength = -1
    minlength = _execute.make_int(minlength, "minlength")
    if maxlength is None:
        maxlength = -1
    maxlength = _execute.make_int(maxlength, "maxlength")
    _attr_T, (values, ) = _execute.args_to_matching_eager([values], ctx, [
        _dtypes.int32,
        _dtypes.int64,
    ])
    _attr_output_type, (weights, ) = _execute.args_to_matching_eager(
        [weights], ctx, [
            _dtypes.int32,
            _dtypes.int64,
            _dtypes.float32,
            _dtypes.float64,
        ])
    indices = _ops.convert_to_tensor(indices, _dtypes.int64)
    dense_shape = _ops.convert_to_tensor(dense_shape, _dtypes.int64)
    _inputs_flat = [indices, values, dense_shape, weights]
    _attrs = ("T", _attr_T, "minlength", minlength, "maxlength", maxlength,
              "binary_output", binary_output, "output_type", _attr_output_type)
    _result = _execute.execute(b"SparseCountSparseOutput",
                               3,
                               inputs=_inputs_flat,
                               attrs=_attrs,
                               ctx=ctx,
                               name=name)
    if _execute.must_record_gradient():
        _execute.record_gradient("SparseCountSparseOutput", _inputs_flat,
                                 _attrs, _result)
    _result = _SparseCountSparseOutputOutput._make(_result)
    return _result
コード例 #5
0
def next_iteration(data, name=None):
    r"""Makes its input available to the next iteration.

  Args:
    data: A `Tensor`. The tensor to be made available to the next iteration.
    name: A name for the operation (optional).

  Returns:
    A `Tensor`. Has the same type as `data`.
  """
    _ctx = _context._context or _context.context()
    tld = _ctx._thread_local_data
    if tld.is_eager:
        try:
            _result = pywrap_tfe.TFE_Py_FastPathExecute(
                _ctx, "NextIteration", name, data)
            return _result
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
        except _core._FallbackException:
            pass
        try:
            return next_iteration_eager_fallback(data, name=name, ctx=_ctx)
        except _core._SymbolicException:
            pass  # Add nodes to the TensorFlow graph.
    # Add nodes to the TensorFlow graph.
    _, _, _op, _outputs = _op_def_library._apply_op_helper("NextIteration",
                                                           data=data,
                                                           name=name)
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("T", _op._get_attr_type("T"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("NextIteration", _inputs_flat, _attrs,
                                 _result)
    _result, = _result
    return _result
コード例 #6
0
def expint(x, name=None):
    r"""TODO: add doc.

  Args:
    x: A `Tensor`. Must be one of the following types: `float32`, `float64`.
    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, "Expint", name,
                tld.op_callbacks, x)
            return _result
        except _core._FallbackException:
            try:
                return expint_eager_fallback(x, name=name, ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
    # Add nodes to the TensorFlow graph.
    _, _, _op, _outputs = _op_def_library._apply_op_helper("Expint",
                                                           x=x,
                                                           name=name)
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("T", _op._get_attr_type("T"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("Expint", _inputs_flat, _attrs, _result)
    _result, = _result
    return _result
コード例 #7
0
def generate_vocab_remapping_eager_fallback(new_vocab_file, old_vocab_file,
                                            new_vocab_offset, num_new_vocab,
                                            old_vocab_size, name, ctx):
    new_vocab_offset = _execute.make_int(new_vocab_offset, "new_vocab_offset")
    num_new_vocab = _execute.make_int(num_new_vocab, "num_new_vocab")
    if old_vocab_size is None:
        old_vocab_size = -1
    old_vocab_size = _execute.make_int(old_vocab_size, "old_vocab_size")
    new_vocab_file = _ops.convert_to_tensor(new_vocab_file, _dtypes.string)
    old_vocab_file = _ops.convert_to_tensor(old_vocab_file, _dtypes.string)
    _inputs_flat = [new_vocab_file, old_vocab_file]
    _attrs = ("new_vocab_offset", new_vocab_offset, "num_new_vocab",
              num_new_vocab, "old_vocab_size", old_vocab_size)
    _result = _execute.execute(b"GenerateVocabRemapping",
                               2,
                               inputs=_inputs_flat,
                               attrs=_attrs,
                               ctx=ctx,
                               name=name)
    if _execute.must_record_gradient():
        _execute.record_gradient("GenerateVocabRemapping", _inputs_flat,
                                 _attrs, _result)
    _result = _GenerateVocabRemappingOutput._make(_result)
    return _result
コード例 #8
0
def enter_eager_fallback(data, frame_name, is_constant, parallel_iterations,
                         name, ctx):
    frame_name = _execute.make_str(frame_name, "frame_name")
    if is_constant is None:
        is_constant = False
    is_constant = _execute.make_bool(is_constant, "is_constant")
    if parallel_iterations is None:
        parallel_iterations = 10
    parallel_iterations = _execute.make_int(parallel_iterations,
                                            "parallel_iterations")
    _attr_T, (data, ) = _execute.args_to_matching_eager([data], ctx)
    _inputs_flat = [data]
    _attrs = ("T", _attr_T, "frame_name", frame_name, "is_constant",
              is_constant, "parallel_iterations", parallel_iterations)
    _result = _execute.execute(b"Enter",
                               1,
                               inputs=_inputs_flat,
                               attrs=_attrs,
                               ctx=ctx,
                               name=name)
    if _execute.must_record_gradient():
        _execute.record_gradient("Enter", _inputs_flat, _attrs, _result)
    _result, = _result
    return _result
コード例 #9
0
def mfcc_eager_fallback(spectrogram, sample_rate, upper_frequency_limit,
                        lower_frequency_limit, filterbank_channel_count,
                        dct_coefficient_count, name, ctx):
    if upper_frequency_limit is None:
        upper_frequency_limit = 4000
    upper_frequency_limit = _execute.make_float(upper_frequency_limit,
                                                "upper_frequency_limit")
    if lower_frequency_limit is None:
        lower_frequency_limit = 20
    lower_frequency_limit = _execute.make_float(lower_frequency_limit,
                                                "lower_frequency_limit")
    if filterbank_channel_count is None:
        filterbank_channel_count = 40
    filterbank_channel_count = _execute.make_int(filterbank_channel_count,
                                                 "filterbank_channel_count")
    if dct_coefficient_count is None:
        dct_coefficient_count = 13
    dct_coefficient_count = _execute.make_int(dct_coefficient_count,
                                              "dct_coefficient_count")
    spectrogram = _ops.convert_to_tensor(spectrogram, _dtypes.float32)
    sample_rate = _ops.convert_to_tensor(sample_rate, _dtypes.int32)
    _inputs_flat = [spectrogram, sample_rate]
    _attrs = ("upper_frequency_limit", upper_frequency_limit,
              "lower_frequency_limit", lower_frequency_limit,
              "filterbank_channel_count", filterbank_channel_count,
              "dct_coefficient_count", dct_coefficient_count)
    _result = _execute.execute(b"Mfcc",
                               1,
                               inputs=_inputs_flat,
                               attrs=_attrs,
                               ctx=ctx,
                               name=name)
    if _execute.must_record_gradient():
        _execute.record_gradient("Mfcc", _inputs_flat, _attrs, _result)
    _result, = _result
    return _result
コード例 #10
0
ファイル: gen_bitwise_ops.py プロジェクト: Miq313/RUHacks2021
def right_shift_eager_fallback(x, y, name, ctx):
    _attr_T, _inputs_T = _execute.args_to_matching_eager([x, y], ctx, [
        _dtypes.int8,
        _dtypes.int16,
        _dtypes.int32,
        _dtypes.int64,
        _dtypes.uint8,
        _dtypes.uint16,
        _dtypes.uint32,
        _dtypes.uint64,
    ])
    (x, y) = _inputs_T
    _inputs_flat = [x, y]
    _attrs = ("T", _attr_T)
    _result = _execute.execute(b"RightShift",
                               1,
                               inputs=_inputs_flat,
                               attrs=_attrs,
                               ctx=ctx,
                               name=name)
    if _execute.must_record_gradient():
        _execute.record_gradient("RightShift", _inputs_flat, _attrs, _result)
    _result, = _result
    return _result
コード例 #11
0
ファイル: gen_bitwise_ops.py プロジェクト: Miq313/RUHacks2021
def population_count_eager_fallback(x, name, ctx):
    _attr_T, (x, ) = _execute.args_to_matching_eager([x], ctx, [
        _dtypes.int8,
        _dtypes.int16,
        _dtypes.int32,
        _dtypes.int64,
        _dtypes.uint8,
        _dtypes.uint16,
        _dtypes.uint32,
        _dtypes.uint64,
    ])
    _inputs_flat = [x]
    _attrs = ("T", _attr_T)
    _result = _execute.execute(b"PopulationCount",
                               1,
                               inputs=_inputs_flat,
                               attrs=_attrs,
                               ctx=ctx,
                               name=name)
    if _execute.must_record_gradient():
        _execute.record_gradient("PopulationCount", _inputs_flat, _attrs,
                                 _result)
    _result, = _result
    return _result
コード例 #12
0
def stateful_random_binomial_eager_fallback(resource, algorithm, shape, counts,
                                            probs, dtype, name, ctx):
    if dtype is None:
        dtype = _dtypes.int64
    dtype = _execute.make_type(dtype, "dtype")
    _attr_S, (shape, ) = _execute.args_to_matching_eager([shape], ctx)
    _attr_T, _inputs_T = _execute.args_to_matching_eager([counts, probs], ctx,
                                                         _dtypes.float64)
    (counts, probs) = _inputs_T
    resource = _ops.convert_to_tensor(resource, _dtypes.resource)
    algorithm = _ops.convert_to_tensor(algorithm, _dtypes.int64)
    _inputs_flat = [resource, algorithm, shape, counts, probs]
    _attrs = ("S", _attr_S, "T", _attr_T, "dtype", dtype)
    _result = _execute.execute(b"StatefulRandomBinomial",
                               1,
                               inputs=_inputs_flat,
                               attrs=_attrs,
                               ctx=ctx,
                               name=name)
    if _execute.must_record_gradient():
        _execute.record_gradient("StatefulRandomBinomial", _inputs_flat,
                                 _attrs, _result)
    _result, = _result
    return _result
コード例 #13
0
def ref_switch(data, pred, name=None):
  r"""Forwards the ref tensor `data` to the output port determined by `pred`.

  If `pred` is true, the `data` input is forwarded to `output_true`. Otherwise,
  the data goes to `output_false`.

  See also `Switch` and `Merge`.

  Args:
    data: A mutable `Tensor`.
      The ref tensor to be forwarded to the appropriate output.
    pred: A `Tensor` of type `bool`.
      A scalar that specifies which output port will receive data.
    name: A name for the operation (optional).

  Returns:
    A tuple of `Tensor` objects (output_false, output_true).

    output_false: A mutable `Tensor`. Has the same type as `data`.
    output_true: A mutable `Tensor`. Has the same type as `data`.
  """
  _ctx = _context._context or _context.context()
  tld = _ctx._thread_local_data
  if tld.is_eager:
    raise RuntimeError("ref_switch op does not support eager execution. Arg 'output_true' is a ref.")
  # Add nodes to the TensorFlow graph.
  _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "RefSwitch", data=data, pred=pred, name=name)
  _result = _outputs[:]
  if _execute.must_record_gradient():
    _attrs = ("T", _op._get_attr_type("T"))
    _inputs_flat = _op.inputs
    _execute.record_gradient(
        "RefSwitch", _inputs_flat, _attrs, _result)
  _result = _RefSwitchOutput._make(_result)
  return _result
コード例 #14
0
ファイル: gen_random_ops.py プロジェクト: saikiran2711/Web
def random_poisson_v2_eager_fallback(shape, rate, seed, seed2, dtype, name,
                                     ctx):
    if seed is None:
        seed = 0
    seed = _execute.make_int(seed, "seed")
    if seed2 is None:
        seed2 = 0
    seed2 = _execute.make_int(seed2, "seed2")
    if dtype is None:
        dtype = _dtypes.int64
    dtype = _execute.make_type(dtype, "dtype")
    _attr_S, (shape, ) = _execute.args_to_matching_eager([shape], ctx, [
        _dtypes.int32,
        _dtypes.int64,
    ])
    _attr_R, (rate, ) = _execute.args_to_matching_eager([rate], ctx, [
        _dtypes.half,
        _dtypes.float32,
        _dtypes.float64,
        _dtypes.int32,
        _dtypes.int64,
    ], _dtypes.float64)
    _inputs_flat = [shape, rate]
    _attrs = ("seed", seed, "seed2", seed2, "S", _attr_S, "R", _attr_R,
              "dtype", dtype)
    _result = _execute.execute(b"RandomPoissonV2",
                               1,
                               inputs=_inputs_flat,
                               attrs=_attrs,
                               ctx=ctx,
                               name=name)
    if _execute.must_record_gradient():
        _execute.record_gradient("RandomPoissonV2", _inputs_flat, _attrs,
                                 _result)
    _result, = _result
    return _result
コード例 #15
0
def debug_numeric_summary_v2_eager_fallback(input, tensor_debug_mode,
                                            tensor_id, name, ctx):
    if tensor_debug_mode is None:
        tensor_debug_mode = -1
    tensor_debug_mode = _execute.make_int(tensor_debug_mode,
                                          "tensor_debug_mode")
    if tensor_id is None:
        tensor_id = -1
    tensor_id = _execute.make_int(tensor_id, "tensor_id")
    _attr_T, (input, ) = _execute.args_to_matching_eager([input], ctx)
    _inputs_flat = [input]
    _attrs = ("T", _attr_T, "tensor_debug_mode", tensor_debug_mode,
              "tensor_id", tensor_id)
    _result = _execute.execute(b"DebugNumericSummaryV2",
                               1,
                               inputs=_inputs_flat,
                               attrs=_attrs,
                               ctx=ctx,
                               name=name)
    if _execute.must_record_gradient():
        _execute.record_gradient("DebugNumericSummaryV2", _inputs_flat, _attrs,
                                 _result)
    _result, = _result
    return _result
コード例 #16
0
ファイル: gen_ctc_ops.py プロジェクト: RajeshM-DS/package
def ctc_loss_eager_fallback(inputs, labels_indices, labels_values,
                            sequence_length, preprocess_collapse_repeated,
                            ctc_merge_repeated,
                            ignore_longer_outputs_than_inputs, name, ctx):
    if preprocess_collapse_repeated is None:
        preprocess_collapse_repeated = False
    preprocess_collapse_repeated = _execute.make_bool(
        preprocess_collapse_repeated, "preprocess_collapse_repeated")
    if ctc_merge_repeated is None:
        ctc_merge_repeated = True
    ctc_merge_repeated = _execute.make_bool(ctc_merge_repeated,
                                            "ctc_merge_repeated")
    if ignore_longer_outputs_than_inputs is None:
        ignore_longer_outputs_than_inputs = False
    ignore_longer_outputs_than_inputs = _execute.make_bool(
        ignore_longer_outputs_than_inputs, "ignore_longer_outputs_than_inputs")
    _attr_T, (inputs, ) = _execute.args_to_matching_eager([inputs], ctx,
                                                          _dtypes.float32)
    labels_indices = _ops.convert_to_tensor(labels_indices, _dtypes.int64)
    labels_values = _ops.convert_to_tensor(labels_values, _dtypes.int32)
    sequence_length = _ops.convert_to_tensor(sequence_length, _dtypes.int32)
    _inputs_flat = [inputs, labels_indices, labels_values, sequence_length]
    _attrs = ("preprocess_collapse_repeated", preprocess_collapse_repeated,
              "ctc_merge_repeated", ctc_merge_repeated,
              "ignore_longer_outputs_than_inputs",
              ignore_longer_outputs_than_inputs, "T", _attr_T)
    _result = _execute.execute(b"CTCLoss",
                               2,
                               inputs=_inputs_flat,
                               attrs=_attrs,
                               ctx=ctx,
                               name=name)
    if _execute.must_record_gradient():
        _execute.record_gradient("CTCLoss", _inputs_flat, _attrs, _result)
    _result = _CTCLossOutput._make(_result)
    return _result
コード例 #17
0
def stateless_random_binomial_eager_fallback(shape, seed, counts, probs, dtype,
                                             name, ctx):
    if dtype is None:
        dtype = _dtypes.int64
    dtype = _execute.make_type(dtype, "dtype")
    _attr_S, (shape, ) = _execute.args_to_matching_eager([shape], ctx)
    _attr_Tseed, (seed, ) = _execute.args_to_matching_eager([seed], ctx,
                                                            _dtypes.int64)
    _attr_T, _inputs_T = _execute.args_to_matching_eager([counts, probs], ctx,
                                                         _dtypes.float64)
    (counts, probs) = _inputs_T
    _inputs_flat = [shape, seed, counts, probs]
    _attrs = ("S", _attr_S, "Tseed", _attr_Tseed, "T", _attr_T, "dtype", dtype)
    _result = _execute.execute(b"StatelessRandomBinomial",
                               1,
                               inputs=_inputs_flat,
                               attrs=_attrs,
                               ctx=ctx,
                               name=name)
    if _execute.must_record_gradient():
        _execute.record_gradient("StatelessRandomBinomial", _inputs_flat,
                                 _attrs, _result)
    _result, = _result
    return _result
コード例 #18
0
def xla_launch(constants, args, resources, Tresults, function, name=None):
  r"""XLA Launch Op. For use by the XLA JIT only.

  Args:
    constants: A list of `Tensor` objects.
    args: A list of `Tensor` objects.
    resources: A list of `Tensor` objects with type `resource`.
    Tresults: A list of `tf.DTypes`.
    function: A function decorated with @Defun.
    name: A name for the operation (optional).

  Returns:
    A list of `Tensor` objects of type `Tresults`.
  """
  _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, "XlaLaunch", name,
        tld.op_callbacks, constants, args, resources, "Tresults", Tresults,
        "function", function)
      return _result
    except _core._NotOkStatusException as e:
      _ops.raise_from_not_ok_status(e, name)
    except _core._FallbackException:
      pass
    try:
      return xla_launch_eager_fallback(
          constants, args, resources, Tresults=Tresults, function=function,
          name=name, ctx=_ctx)
    except _core._SymbolicException:
      pass  # Add nodes to the TensorFlow graph.
    except (TypeError, ValueError):
      result = _dispatch.dispatch(
            xla_launch, (), dict(constants=constants, args=args,
                                 resources=resources, Tresults=Tresults,
                                 function=function, name=name)
          )
      if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
        return result
      raise
  # Add nodes to the TensorFlow graph.
  if not isinstance(resources, (list, tuple)):
    raise TypeError(
        "Expected list for 'resources' argument to "
        "'xla_launch' Op, not %r." % resources)
  _attr_Nresources = len(resources)
  if not isinstance(Tresults, (list, tuple)):
    raise TypeError(
        "Expected list for 'Tresults' argument to "
        "'xla_launch' Op, not %r." % Tresults)
  Tresults = [_execute.make_type(_t, "Tresults") for _t in Tresults]
  try:
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "XlaLaunch", constants=constants, args=args, resources=resources,
                     Tresults=Tresults, function=function, name=name)
  except (TypeError, ValueError):
    result = _dispatch.dispatch(
          xla_launch, (), dict(constants=constants, args=args,
                               resources=resources, Tresults=Tresults,
                               function=function, name=name)
        )
    if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
      return result
    raise
  _result = _outputs[:]
  if not _result:
    return _op
  if _execute.must_record_gradient():
    _attrs = ("Tconstants", _op.get_attr("Tconstants"), "Targs",
              _op.get_attr("Targs"), "Nresources",
              _op._get_attr_int("Nresources"), "Tresults",
              _op.get_attr("Tresults"), "function", _op.get_attr("function"))
    _inputs_flat = _op.inputs
    _execute.record_gradient(
        "XlaLaunch", _inputs_flat, _attrs, _result)
  return _result
コード例 #19
0
def stateless_truncated_normal(shape, seed, dtype=_dtypes.float32, name=None):
    r"""Outputs deterministic pseudorandom values from a truncated normal distribution.

  The generated values follow a normal distribution with mean 0 and standard
  deviation 1, except that values whose magnitude is more than 2 standard
  deviations from the mean are dropped and re-picked.

  The outputs are a deterministic function of `shape` and `seed`.

  Args:
    shape: A `Tensor`. Must be one of the following types: `int32`, `int64`.
      The shape of the output tensor.
    seed: A `Tensor`. Must be one of the following types: `int32`, `int64`.
      2 seeds (shape [2]).
    dtype: An optional `tf.DType` from: `tf.half, tf.bfloat16, tf.float32, tf.float64`. Defaults to `tf.float32`.
      The type of the output.
    name: A name for the operation (optional).

  Returns:
    A `Tensor` of type `dtype`.
  """
    _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,
                "StatelessTruncatedNormal", name, tld.op_callbacks, shape,
                seed, "dtype", dtype)
            return _result
        except _core._FallbackException:
            try:
                return stateless_truncated_normal_eager_fallback(shape,
                                                                 seed,
                                                                 dtype=dtype,
                                                                 name=name,
                                                                 ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
    # Add nodes to the TensorFlow graph.
    if dtype is None:
        dtype = _dtypes.float32
    dtype = _execute.make_type(dtype, "dtype")
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "StatelessTruncatedNormal",
        shape=shape,
        seed=seed,
        dtype=dtype,
        name=name)
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("dtype", _op._get_attr_type("dtype"), "T",
                  _op._get_attr_type("T"), "Tseed",
                  _op._get_attr_type("Tseed"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("StatelessTruncatedNormal", _inputs_flat,
                                 _attrs, _result)
    _result, = _result
    return _result
コード例 #20
0
def stateless_random_poisson(shape, seed, lam, dtype, name=None):
    r"""Outputs deterministic pseudorandom random numbers from a Poisson distribution.

  Outputs random values from a Poisson distribution.

  The outputs are a deterministic function of `shape`, `seed`, and `lam`.

  Args:
    shape: A `Tensor`. Must be one of the following types: `int32`, `int64`.
      The shape of the output tensor.
    seed: A `Tensor`. Must be one of the following types: `int32`, `int64`.
      2 seeds (shape [2]).
    lam: A `Tensor`. Must be one of the following types: `half`, `float32`, `float64`, `int32`, `int64`.
      The rate of the Poisson distribution. Shape must match the rightmost dimensions
      of `shape`.
    dtype: A `tf.DType` from: `tf.half, tf.float32, tf.float64, tf.int32, tf.int64`.
      The type of the output.
    name: A name for the operation (optional).

  Returns:
    A `Tensor` of type `dtype`.
  """
    _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,
                "StatelessRandomPoisson", name, tld.op_callbacks, shape, seed,
                lam, "dtype", dtype)
            return _result
        except _core._FallbackException:
            try:
                return stateless_random_poisson_eager_fallback(shape,
                                                               seed,
                                                               lam,
                                                               dtype=dtype,
                                                               name=name,
                                                               ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
    # Add nodes to the TensorFlow graph.
    dtype = _execute.make_type(dtype, "dtype")
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "StatelessRandomPoisson",
        shape=shape,
        seed=seed,
        lam=lam,
        dtype=dtype,
        name=name)
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("Rtype", _op._get_attr_type("Rtype"), "dtype",
                  _op._get_attr_type("dtype"), "T", _op._get_attr_type("T"),
                  "Tseed", _op._get_attr_type("Tseed"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("StatelessRandomPoisson", _inputs_flat,
                                 _attrs, _result)
    _result, = _result
    return _result
コード例 #21
0
def stateless_multinomial(logits,
                          num_samples,
                          seed,
                          output_dtype=_dtypes.int64,
                          name=None):
    r"""Draws samples from a multinomial distribution.

  Args:
    logits: A `Tensor`. Must be one of the following types: `float32`, `float64`, `int32`, `uint8`, `int16`, `int8`, `int64`, `bfloat16`, `uint16`, `half`, `uint32`, `uint64`.
      2-D Tensor with shape `[batch_size, num_classes]`.  Each slice `[i, :]`
      represents the unnormalized log probabilities for all classes.
    num_samples: A `Tensor` of type `int32`.
      0-D.  Number of independent samples to draw for each row slice.
    seed: A `Tensor`. Must be one of the following types: `int32`, `int64`.
      2 seeds (shape [2]).
    output_dtype: An optional `tf.DType` from: `tf.int32, tf.int64`. Defaults to `tf.int64`.
    name: A name for the operation (optional).

  Returns:
    A `Tensor` of type `output_dtype`.
  """
    _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, "StatelessMultinomial",
                name, tld.op_callbacks, logits, num_samples, seed,
                "output_dtype", output_dtype)
            return _result
        except _core._FallbackException:
            try:
                return stateless_multinomial_eager_fallback(
                    logits,
                    num_samples,
                    seed,
                    output_dtype=output_dtype,
                    name=name,
                    ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
    # Add nodes to the TensorFlow graph.
    if output_dtype is None:
        output_dtype = _dtypes.int64
    output_dtype = _execute.make_type(output_dtype, "output_dtype")
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "StatelessMultinomial",
        logits=logits,
        num_samples=num_samples,
        seed=seed,
        output_dtype=output_dtype,
        name=name)
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("T", _op._get_attr_type("T"), "Tseed",
                  _op._get_attr_type("Tseed"), "output_dtype",
                  _op._get_attr_type("output_dtype"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("StatelessMultinomial", _inputs_flat, _attrs,
                                 _result)
    _result, = _result
    return _result
コード例 #22
0
def kmeans_plus_plus_initialization(points,
                                    num_to_sample,
                                    seed,
                                    num_retries_per_sample,
                                    name=None):
    r"""Selects num_to_sample rows of input using the KMeans++ criterion.

  Rows of points are assumed to be input points. One row is selected at random.
  Subsequent rows are sampled with probability proportional to the squared L2
  distance from the nearest row selected thus far till num_to_sample rows have
  been sampled.

  Args:
    points: A `Tensor` of type `float32`.
      Matrix of shape (n, d). Rows are assumed to be input points.
    num_to_sample: A `Tensor` of type `int64`.
      Scalar. The number of rows to sample. This value must not be larger than n.
    seed: A `Tensor` of type `int64`.
      Scalar. Seed for initializing the random number generator.
    num_retries_per_sample: A `Tensor` of type `int64`.
      Scalar. For each row that is sampled, this parameter
      specifies the number of additional points to draw from the current
      distribution before selecting the best. If a negative value is specified, a
      heuristic is used to sample O(log(num_to_sample)) additional points.
    name: A name for the operation (optional).

  Returns:
    A `Tensor` of type `float32`.
  """
    _ctx = _context._context or _context.context()
    tld = _ctx._thread_local_data
    if tld.is_eager:
        try:
            _result = pywrap_tfe.TFE_Py_FastPathExecute(
                _ctx, "KmeansPlusPlusInitialization", name, points,
                num_to_sample, seed, num_retries_per_sample)
            return _result
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
        except _core._FallbackException:
            pass
        try:
            return kmeans_plus_plus_initialization_eager_fallback(
                points,
                num_to_sample,
                seed,
                num_retries_per_sample,
                name=name,
                ctx=_ctx)
        except _core._SymbolicException:
            pass  # Add nodes to the TensorFlow graph.
    # Add nodes to the TensorFlow graph.
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "KmeansPlusPlusInitialization",
        points=points,
        num_to_sample=num_to_sample,
        seed=seed,
        num_retries_per_sample=num_retries_per_sample,
        name=name)
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ()
        _inputs_flat = _op.inputs
        _execute.record_gradient("KmeansPlusPlusInitialization", _inputs_flat,
                                 _attrs, _result)
    _result, = _result
    return _result
コード例 #23
0
def stateless_random_binomial(shape,
                              seed,
                              counts,
                              probs,
                              dtype=_dtypes.int64,
                              name=None):
    r"""Outputs deterministic pseudorandom random numbers from a binomial distribution.

  Outputs random values from a binomial distribution.

  The outputs are a deterministic function of `shape`, `seed`, `counts`, and `probs`.

  Args:
    shape: A `Tensor`. Must be one of the following types: `int32`, `int64`.
      The shape of the output tensor.
    seed: A `Tensor`. Must be one of the following types: `int32`, `int64`.
      2 seeds (shape [2]).
    counts: A `Tensor`. Must be one of the following types: `half`, `float32`, `float64`, `int32`, `int64`.
      The counts of the binomial distribution. Must be broadcastable with `probs`,
      and broadcastable with the rightmost dimensions of `shape`.
    probs: A `Tensor`. Must have the same type as `counts`.
      The probability of success for the binomial distribution. Must be broadcastable
      with `counts` and broadcastable with the rightmost dimensions of `shape`.
    dtype: An optional `tf.DType` from: `tf.half, tf.float32, tf.float64, tf.int32, tf.int64`. Defaults to `tf.int64`.
      The type of the output.
    name: A name for the operation (optional).

  Returns:
    A `Tensor` of type `dtype`.
  """
    _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,
                "StatelessRandomBinomial", name, tld.op_callbacks, shape, seed,
                counts, probs, "dtype", dtype)
            return _result
        except _core._FallbackException:
            try:
                return stateless_random_binomial_eager_fallback(shape,
                                                                seed,
                                                                counts,
                                                                probs,
                                                                dtype=dtype,
                                                                name=name,
                                                                ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
    # Add nodes to the TensorFlow graph.
    if dtype is None:
        dtype = _dtypes.int64
    dtype = _execute.make_type(dtype, "dtype")
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "StatelessRandomBinomial",
        shape=shape,
        seed=seed,
        counts=counts,
        probs=probs,
        dtype=dtype,
        name=name)
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("S", _op._get_attr_type("S"), "Tseed",
                  _op._get_attr_type("Tseed"), "T", _op._get_attr_type("T"),
                  "dtype", _op._get_attr_type("dtype"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("StatelessRandomBinomial", _inputs_flat,
                                 _attrs, _result)
    _result, = _result
    return _result
コード例 #24
0
def unbatch(batched_tensor, batch_index, id, timeout_micros, container="", shared_name="", name=None):
  r"""Reverses the operation of Batch for a single output Tensor.

  An instance of Unbatch either receives an empty batched_tensor, in which case it
  asynchronously waits until the values become available from a concurrently
  running instance of Unbatch with the same container and shared_name, or receives
  a non-empty batched_tensor in which case it finalizes all other concurrently
  running instances and outputs its own element from the batch.

  batched_tensor: The possibly transformed output of Batch. The size of the first
   dimension should remain unchanged by the transformations for the operation to
   work.
  batch_index: The matching batch_index obtained from Batch.
  id: The id scalar emitted by Batch.
  unbatched_tensor: The Tensor corresponding to this execution.
  timeout_micros: Maximum amount of time (in microseconds) to wait to receive the
   batched input tensor associated with a given invocation of the op.
  container: Container to control resource sharing.
  shared_name: Instances of Unbatch with the same container and shared_name are
   assumed to possibly belong to the same batch. If left empty, the op name will
   be used as the shared name.

  Args:
    batched_tensor: A `Tensor`.
    batch_index: A `Tensor` of type `int64`.
    id: A `Tensor` of type `int64`.
    timeout_micros: An `int`.
    container: An optional `string`. Defaults to `""`.
    shared_name: An optional `string`. Defaults to `""`.
    name: A name for the operation (optional).

  Returns:
    A `Tensor`. Has the same type as `batched_tensor`.
  """
  _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, "Unbatch", name,
        tld.op_callbacks, batched_tensor, batch_index, id, "timeout_micros",
        timeout_micros, "container", container, "shared_name", shared_name)
      return _result
    except _core._NotOkStatusException as e:
      _ops.raise_from_not_ok_status(e, name)
    except _core._FallbackException:
      pass
    try:
      return unbatch_eager_fallback(
          batched_tensor, batch_index, id, timeout_micros=timeout_micros,
          container=container, shared_name=shared_name, name=name, ctx=_ctx)
    except _core._SymbolicException:
      pass  # Add nodes to the TensorFlow graph.
  # Add nodes to the TensorFlow graph.
  timeout_micros = _execute.make_int(timeout_micros, "timeout_micros")
  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")
  _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "Unbatch", batched_tensor=batched_tensor, batch_index=batch_index,
                   id=id, timeout_micros=timeout_micros, container=container,
                   shared_name=shared_name, name=name)
  _result = _outputs[:]
  if _execute.must_record_gradient():
    _attrs = ("timeout_micros", _op._get_attr_int("timeout_micros"),
              "container", _op.get_attr("container"), "shared_name",
              _op.get_attr("shared_name"), "T", _op._get_attr_type("T"))
    _inputs_flat = _op.inputs
    _execute.record_gradient(
        "Unbatch", _inputs_flat, _attrs, _result)
  _result, = _result
  return _result
コード例 #25
0
def unbatch_grad(original_input, batch_index, grad, id, container="", shared_name="", name=None):
  r"""Gradient of Unbatch.

  Acts like Batch but using the given batch_index index of batching things as they
  become available. This ensures that the gradients are propagated back in the
  same session which did the forward pass.

  original_input: The input to the Unbatch operation this is the gradient of.
  batch_index: The batch_index given to the Unbatch operation this is the gradient
  of.
  grad: The downstream gradient.
  id: The id scalar emitted by Batch.
  batched_grad: The return value, either an empty tensor or the batched gradient.
  container: Container to control resource sharing.
  shared_name: Instances of UnbatchGrad with the same container and shared_name
   are assumed to possibly belong to the same batch. If left empty, the op name
   will be used as the shared name.

  Args:
    original_input: A `Tensor`.
    batch_index: A `Tensor` of type `int64`.
    grad: A `Tensor`. Must have the same type as `original_input`.
    id: A `Tensor` of type `int64`.
    container: An optional `string`. Defaults to `""`.
    shared_name: An optional `string`. Defaults to `""`.
    name: A name for the operation (optional).

  Returns:
    A `Tensor`. Has the same type as `original_input`.
  """
  _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, "UnbatchGrad", name,
        tld.op_callbacks, original_input, batch_index, grad, id, "container",
        container, "shared_name", shared_name)
      return _result
    except _core._NotOkStatusException as e:
      _ops.raise_from_not_ok_status(e, name)
    except _core._FallbackException:
      pass
    try:
      return unbatch_grad_eager_fallback(
          original_input, batch_index, grad, id, container=container,
          shared_name=shared_name, name=name, ctx=_ctx)
    except _core._SymbolicException:
      pass  # Add nodes to the TensorFlow graph.
  # 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")
  _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "UnbatchGrad", original_input=original_input, batch_index=batch_index,
                       grad=grad, id=id, container=container,
                       shared_name=shared_name, name=name)
  _result = _outputs[:]
  if _execute.must_record_gradient():
    _attrs = ("container", _op.get_attr("container"), "shared_name",
              _op.get_attr("shared_name"), "T", _op._get_attr_type("T"))
    _inputs_flat = _op.inputs
    _execute.record_gradient(
        "UnbatchGrad", _inputs_flat, _attrs, _result)
  _result, = _result
  return _result
コード例 #26
0
def batch(in_tensors, num_batch_threads, max_batch_size, batch_timeout_micros, grad_timeout_micros, max_enqueued_batches=10, allowed_batch_sizes=[], container="", shared_name="", batching_queue="", name=None):
  r"""Batches all input tensors nondeterministically.

  When many instances of this Op are being run concurrently with the same
  container/shared_name in the same device, some will output zero-shaped Tensors
  and others will output Tensors of size up to max_batch_size.

  All Tensors in in_tensors are batched together (so, for example, labels and
  features should be batched with a single instance of this operation.

  Each invocation of batch emits an `id` scalar which will be used to identify
  this particular invocation when doing unbatch or its gradient.

  Each op which emits a non-empty batch will also emit a non-empty batch_index
  Tensor, which, is a [K, 3] matrix where each row contains the invocation's id,
  start, and length of elements of each set of Tensors present in batched_tensors.

  Batched tensors are concatenated along the first dimension, and all tensors in
  in_tensors must have the first dimension of the same size.

  in_tensors: The tensors to be batched.
  num_batch_threads: Number of scheduling threads for processing batches of work.
   Determines the number of batches processed in parallel.
  max_batch_size: Batch sizes will never be bigger than this.
  batch_timeout_micros: Maximum number of microseconds to wait before outputting
   an incomplete batch.
  allowed_batch_sizes: Optional list of allowed batch sizes. If left empty, does
   nothing. Otherwise, supplies a list of batch sizes, causing the op to pad
   batches up to one of those sizes. The entries must increase monotonically, and
   the final entry must equal max_batch_size.
  grad_timeout_micros: The timeout to use for the gradient. See Unbatch.
  batched_tensors: Either empty tensors or a batch of concatenated Tensors.
  batch_index: If out_tensors is non-empty, has information to invert it.
  container: Controls the scope of sharing of this batch.
  id: always contains a scalar with a unique ID for this invocation of Batch.
  shared_name: Concurrently running instances of batch in the same device with the
   same container and shared_name will batch their elements together. If left
   empty, the op name will be used as the shared name.
  T: the types of tensors to be batched.

  Args:
    in_tensors: A list of `Tensor` objects.
    num_batch_threads: An `int`.
    max_batch_size: An `int`.
    batch_timeout_micros: An `int`.
    grad_timeout_micros: An `int`.
    max_enqueued_batches: An optional `int`. Defaults to `10`.
    allowed_batch_sizes: An optional list of `ints`. Defaults to `[]`.
    container: An optional `string`. Defaults to `""`.
    shared_name: An optional `string`. Defaults to `""`.
    batching_queue: An optional `string`. Defaults to `""`.
    name: A name for the operation (optional).

  Returns:
    A tuple of `Tensor` objects (batched_tensors, batch_index, id).

    batched_tensors: A list of `Tensor` objects. Has the same type as `in_tensors`.
    batch_index: A `Tensor` of type `int64`.
    id: A `Tensor` of type `int64`.
  """
  _ctx = _context._context or _context.context()
  tld = _ctx._thread_local_data
  if tld.is_eager:
    try:
      _result = pywrap_tfe.TFE_Py_FastPathExecute(
        _ctx._context_handle, tld.device_name, "Batch", name,
        tld.op_callbacks, in_tensors, "num_batch_threads", num_batch_threads,
        "max_batch_size", max_batch_size, "max_enqueued_batches",
        max_enqueued_batches, "batch_timeout_micros", batch_timeout_micros,
        "allowed_batch_sizes", allowed_batch_sizes, "grad_timeout_micros",
        grad_timeout_micros, "container", container, "shared_name",
        shared_name, "batching_queue", batching_queue)
      _result = _BatchOutput._make(_result)
      return _result
    except _core._NotOkStatusException as e:
      _ops.raise_from_not_ok_status(e, name)
    except _core._FallbackException:
      pass
    try:
      return batch_eager_fallback(
          in_tensors, num_batch_threads=num_batch_threads,
          max_batch_size=max_batch_size,
          max_enqueued_batches=max_enqueued_batches,
          batch_timeout_micros=batch_timeout_micros,
          allowed_batch_sizes=allowed_batch_sizes,
          grad_timeout_micros=grad_timeout_micros, container=container,
          shared_name=shared_name, batching_queue=batching_queue, name=name,
          ctx=_ctx)
    except _core._SymbolicException:
      pass  # Add nodes to the TensorFlow graph.
  # Add nodes to the TensorFlow graph.
  num_batch_threads = _execute.make_int(num_batch_threads, "num_batch_threads")
  max_batch_size = _execute.make_int(max_batch_size, "max_batch_size")
  batch_timeout_micros = _execute.make_int(batch_timeout_micros, "batch_timeout_micros")
  grad_timeout_micros = _execute.make_int(grad_timeout_micros, "grad_timeout_micros")
  if max_enqueued_batches is None:
    max_enqueued_batches = 10
  max_enqueued_batches = _execute.make_int(max_enqueued_batches, "max_enqueued_batches")
  if allowed_batch_sizes is None:
    allowed_batch_sizes = []
  if not isinstance(allowed_batch_sizes, (list, tuple)):
    raise TypeError(
        "Expected list for 'allowed_batch_sizes' argument to "
        "'batch' Op, not %r." % allowed_batch_sizes)
  allowed_batch_sizes = [_execute.make_int(_i, "allowed_batch_sizes") for _i in allowed_batch_sizes]
  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 batching_queue is None:
    batching_queue = ""
  batching_queue = _execute.make_str(batching_queue, "batching_queue")
  _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "Batch", in_tensors=in_tensors, num_batch_threads=num_batch_threads,
                 max_batch_size=max_batch_size,
                 batch_timeout_micros=batch_timeout_micros,
                 grad_timeout_micros=grad_timeout_micros,
                 max_enqueued_batches=max_enqueued_batches,
                 allowed_batch_sizes=allowed_batch_sizes, container=container,
                 shared_name=shared_name, batching_queue=batching_queue,
                 name=name)
  _result = _outputs[:]
  if _execute.must_record_gradient():
    _attrs = ("num_batch_threads", _op._get_attr_int("num_batch_threads"),
              "max_batch_size", _op._get_attr_int("max_batch_size"),
              "max_enqueued_batches",
              _op._get_attr_int("max_enqueued_batches"),
              "batch_timeout_micros",
              _op._get_attr_int("batch_timeout_micros"),
              "allowed_batch_sizes", _op.get_attr("allowed_batch_sizes"),
              "grad_timeout_micros", _op._get_attr_int("grad_timeout_micros"),
              "container", _op.get_attr("container"), "shared_name",
              _op.get_attr("shared_name"), "batching_queue",
              _op.get_attr("batching_queue"), "T", _op.get_attr("T"))
    _inputs_flat = _op.inputs
    _execute.record_gradient(
        "Batch", _inputs_flat, _attrs, _result)
  _result = [_result[:len(in_tensors)]] + _result[len(in_tensors):]
  _result = _BatchOutput._make(_result)
  return _result
コード例 #27
0
def batch_function(in_tensors, captured_tensors, f, num_batch_threads, max_batch_size, batch_timeout_micros, Tout, max_enqueued_batches=10, allowed_batch_sizes=[], container="", shared_name="", batching_queue="", enable_large_batch_splitting=False, name=None):
  r"""Batches all the inputs tensors to the computation done by the function.

  So, for example, in the following code

    ```python

    # This input will be captured.
    y = tf.placeholder_with_default(1.0, shape=[])

    @tf.Defun(tf.float32)
    def computation(a):
      return tf.matmul(a, a) + y

    b = gen_batch_ops.batch_function(
            f=computation
            in_tensors=[a],
            captured_tensors=computation.captured_inputs,
            Tout=[o.type for o in computation.definition.signature.output_arg],
            num_batch_threads=1,
            max_batch_size=10,
            batch_timeout_micros=100000,  # 100ms
            allowed_batch_sizes=[3, 10],
            batching_queue="")

  If more than one session.run call is simultaneously trying to compute `b`
  the values of `a` will be gathered, non-deterministically concatenated
  along the first axis, and only one thread will run the computation.

  Assumes that all arguments of the function are Tensors which will be batched
  along their first dimension.

  Arguments that are captured, are not batched. The session.run call which does
  the concatenation, will use the values of the captured tensors available to it.
  Therefore, typical uses of captured tensors should involve values which remain
  unchanged across session.run calls. Inference is a good example of this.

  SparseTensor is not supported. The return value of the decorated function
  must be a Tensor or a list/tuple of Tensors.

  Args:
    in_tensors: A list of `Tensor` objects. The tensors to be batched.
    captured_tensors: A list of `Tensor` objects.
      The tensors which are captured in the function, and don't need
      to be batched.
    f: A function decorated with @Defun.
    num_batch_threads: An `int`.
      Number of scheduling threads for processing batches of work.
      Determines the number of batches processed in parallel.
    max_batch_size: An `int`. Batch sizes will never be bigger than this.
    batch_timeout_micros: An `int`.
      Maximum number of microseconds to wait before outputting
      an incomplete batch.
    Tout: A list of `tf.DTypes` that has length `>= 1`.
      the types of the output tensors.
    max_enqueued_batches: An optional `int`. Defaults to `10`.
      Maximum number of batches enqueued. Default: 10.
    allowed_batch_sizes: An optional list of `ints`. Defaults to `[]`.
      Optional list of allowed batch sizes. If left empty, does
      nothing. Otherwise, supplies a list of batch sizes, causing the op to pad
      batches up to one of those sizes. The entries must increase monotonically, and
      the final entry must equal max_batch_size.
    container: An optional `string`. Defaults to `""`.
      Controls the scope of sharing of this batch.
    shared_name: An optional `string`. Defaults to `""`.
      Concurrently running instances of batch in the same device with the
      same container and shared_name will batch their elements together. If left
      empty, the op name will be used as the shared name.
    batching_queue: An optional `string`. Defaults to `""`.
    enable_large_batch_splitting: An optional `bool`. Defaults to `False`.
      input with a large size (i.e., larger than the largest value of
      `allowed_batch_sizes`) will be splitted into multiple batches with batch size.
    name: A name for the operation (optional).

  Returns:
    A list of `Tensor` objects of type `Tout`.
  """
  _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, "BatchFunction", name,
        tld.op_callbacks, in_tensors, captured_tensors, "f", f,
        "num_batch_threads", num_batch_threads, "max_batch_size",
        max_batch_size, "batch_timeout_micros", batch_timeout_micros,
        "max_enqueued_batches", max_enqueued_batches, "allowed_batch_sizes",
        allowed_batch_sizes, "container", container, "shared_name",
        shared_name, "batching_queue", batching_queue, "Tout", Tout,
        "enable_large_batch_splitting", enable_large_batch_splitting)
      return _result
    except _core._NotOkStatusException as e:
      _ops.raise_from_not_ok_status(e, name)
    except _core._FallbackException:
      pass
    try:
      return batch_function_eager_fallback(
          in_tensors, captured_tensors, f=f,
          num_batch_threads=num_batch_threads, max_batch_size=max_batch_size,
          batch_timeout_micros=batch_timeout_micros,
          max_enqueued_batches=max_enqueued_batches,
          allowed_batch_sizes=allowed_batch_sizes, container=container,
          shared_name=shared_name, batching_queue=batching_queue, Tout=Tout,
          enable_large_batch_splitting=enable_large_batch_splitting,
          name=name, ctx=_ctx)
    except _core._SymbolicException:
      pass  # Add nodes to the TensorFlow graph.
  # Add nodes to the TensorFlow graph.
  num_batch_threads = _execute.make_int(num_batch_threads, "num_batch_threads")
  max_batch_size = _execute.make_int(max_batch_size, "max_batch_size")
  batch_timeout_micros = _execute.make_int(batch_timeout_micros, "batch_timeout_micros")
  if not isinstance(Tout, (list, tuple)):
    raise TypeError(
        "Expected list for 'Tout' argument to "
        "'batch_function' Op, not %r." % Tout)
  Tout = [_execute.make_type(_t, "Tout") for _t in Tout]
  if max_enqueued_batches is None:
    max_enqueued_batches = 10
  max_enqueued_batches = _execute.make_int(max_enqueued_batches, "max_enqueued_batches")
  if allowed_batch_sizes is None:
    allowed_batch_sizes = []
  if not isinstance(allowed_batch_sizes, (list, tuple)):
    raise TypeError(
        "Expected list for 'allowed_batch_sizes' argument to "
        "'batch_function' Op, not %r." % allowed_batch_sizes)
  allowed_batch_sizes = [_execute.make_int(_i, "allowed_batch_sizes") for _i in allowed_batch_sizes]
  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 batching_queue is None:
    batching_queue = ""
  batching_queue = _execute.make_str(batching_queue, "batching_queue")
  if enable_large_batch_splitting is None:
    enable_large_batch_splitting = False
  enable_large_batch_splitting = _execute.make_bool(enable_large_batch_splitting, "enable_large_batch_splitting")
  _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "BatchFunction", in_tensors=in_tensors,
                         captured_tensors=captured_tensors, f=f,
                         num_batch_threads=num_batch_threads,
                         max_batch_size=max_batch_size,
                         batch_timeout_micros=batch_timeout_micros, Tout=Tout,
                         max_enqueued_batches=max_enqueued_batches,
                         allowed_batch_sizes=allowed_batch_sizes,
                         container=container, shared_name=shared_name,
                         batching_queue=batching_queue,
                         enable_large_batch_splitting=enable_large_batch_splitting,
                         name=name)
  _result = _outputs[:]
  if _execute.must_record_gradient():
    _attrs = ("f", _op.get_attr("f"), "num_batch_threads",
              _op._get_attr_int("num_batch_threads"), "max_batch_size",
              _op._get_attr_int("max_batch_size"), "batch_timeout_micros",
              _op._get_attr_int("batch_timeout_micros"),
              "max_enqueued_batches",
              _op._get_attr_int("max_enqueued_batches"),
              "allowed_batch_sizes", _op.get_attr("allowed_batch_sizes"),
              "container", _op.get_attr("container"), "shared_name",
              _op.get_attr("shared_name"), "batching_queue",
              _op.get_attr("batching_queue"), "Tin", _op.get_attr("Tin"),
              "Tcaptured", _op.get_attr("Tcaptured"), "Tout",
              _op.get_attr("Tout"), "enable_large_batch_splitting",
              _op._get_attr_bool("enable_large_batch_splitting"))
    _inputs_flat = _op.inputs
    _execute.record_gradient(
        "BatchFunction", _inputs_flat, _attrs, _result)
  return _result
コード例 #28
0
def audio_microfrontend_eager_fallback(audio, sample_rate, window_size, window_step, num_channels, upper_band_limit, lower_band_limit, smoothing_bits, even_smoothing, odd_smoothing, min_signal_remaining, enable_pcan, pcan_strength, pcan_offset, gain_bits, enable_log, scale_shift, left_context, right_context, frame_stride, zero_padding, out_scale, out_type, name, ctx):
  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")
  audio = _ops.convert_to_tensor(audio, _dtypes.int16)
  _inputs_flat = [audio]
  _attrs = ("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)
  _result = _execute.execute(b"AudioMicrofrontend", 1, inputs=_inputs_flat,
                             attrs=_attrs, ctx=ctx, name=name)
  if _execute.must_record_gradient():
    _execute.record_gradient(
        "AudioMicrofrontend", _inputs_flat, _attrs, _result)
  _result, = _result
  return _result
コード例 #29
0
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()
  tld = _ctx._thread_local_data
  if tld.is_eager:
    try:
      _result = pywrap_tfe.TFE_Py_FastPathExecute(
        _ctx._context_handle, tld.device_name, "AudioMicrofrontend", name,
        tld.op_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._NotOkStatusException as e:
      _ops.raise_from_not_ok_status(e, name)
    except _core._FallbackException:
      pass
    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, (), dict(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
  # 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, _outputs = _op_def_library._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, (), dict(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 = _outputs[:]
  if _execute.must_record_gradient():
    _attrs = ("sample_rate", _op._get_attr_int("sample_rate"), "window_size",
              _op._get_attr_int("window_size"), "window_step",
              _op._get_attr_int("window_step"), "num_channels",
              _op._get_attr_int("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_int("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_bool("enable_pcan"), "pcan_strength",
              _op.get_attr("pcan_strength"), "pcan_offset",
              _op.get_attr("pcan_offset"), "gain_bits",
              _op._get_attr_int("gain_bits"), "enable_log",
              _op._get_attr_bool("enable_log"), "scale_shift",
              _op._get_attr_int("scale_shift"), "left_context",
              _op._get_attr_int("left_context"), "right_context",
              _op._get_attr_int("right_context"), "frame_stride",
              _op._get_attr_int("frame_stride"), "zero_padding",
              _op._get_attr_bool("zero_padding"), "out_scale",
              _op._get_attr_int("out_scale"), "out_type",
              _op._get_attr_type("out_type"))
    _inputs_flat = _op.inputs
    _execute.record_gradient(
        "AudioMicrofrontend", _inputs_flat, _attrs, _result)
  _result, = _result
  return _result
コード例 #30
0
def audio_summary(tag, tensor, sample_rate, max_outputs=3, name=None):
  r"""Outputs a `Summary` protocol buffer with audio.

  The summary has up to `max_outputs` summary values containing audio. The
  audio is built from `tensor` which must be 3-D with shape `[batch_size,
  frames, channels]` or 2-D with shape `[batch_size, frames]`. The values are
  assumed to be in the range of `[-1.0, 1.0]` with a sample rate of `sample_rate`.

  The `tag` argument is a scalar `Tensor` of type `string`.  It is used to
  build the `tag` of the summary values:

  *  If `max_outputs` is 1, the summary value tag is '*tag*/audio'.
  *  If `max_outputs` is greater than 1, the summary value tags are
     generated sequentially as '*tag*/audio/0', '*tag*/audio/1', etc.

  Args:
    tag: A `Tensor` of type `string`.
      Scalar. Used to build the `tag` attribute of the summary values.
    tensor: A `Tensor` of type `float32`. 2-D of shape `[batch_size, frames]`.
    sample_rate: A `float`. The sample rate of the signal in hertz.
    max_outputs: An optional `int` that is `>= 1`. Defaults to `3`.
      Max number of batch elements to generate audio for.
    name: A name for the operation (optional).

  Returns:
    A `Tensor` of type `string`.
  """
  _ctx = _context._context or _context.context()
  tld = _ctx._thread_local_data
  if tld.is_eager:
    try:
      _result = pywrap_tfe.TFE_Py_FastPathExecute(
        _ctx._context_handle, tld.device_name, "AudioSummary", name,
        tld.op_callbacks, tag, tensor, "sample_rate", sample_rate,
        "max_outputs", max_outputs)
      return _result
    except _core._NotOkStatusException as e:
      _ops.raise_from_not_ok_status(e, name)
    except _core._FallbackException:
      pass
    try:
      return audio_summary_eager_fallback(
          tag, tensor, sample_rate=sample_rate, max_outputs=max_outputs,
          name=name, ctx=_ctx)
    except _core._SymbolicException:
      pass  # Add nodes to the TensorFlow graph.
  # Add nodes to the TensorFlow graph.
  sample_rate = _execute.make_float(sample_rate, "sample_rate")
  if max_outputs is None:
    max_outputs = 3
  max_outputs = _execute.make_int(max_outputs, "max_outputs")
  _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "AudioSummary", tag=tag, tensor=tensor, sample_rate=sample_rate,
                        max_outputs=max_outputs, name=name)
  _result = _outputs[:]
  if _execute.must_record_gradient():
    _attrs = ("sample_rate", _op.get_attr("sample_rate"), "max_outputs",
              _op._get_attr_int("max_outputs"))
    _inputs_flat = _op.inputs
    _execute.record_gradient(
        "AudioSummary", _inputs_flat, _attrs, _result)
  _result, = _result
  return _result