Example #1
0
def lstm_block_cell_grad(x, cs_prev, h_prev, w, wci, wcf, wco, b, i, cs, f, o, ci, co, cs_grad, h_grad, use_peephole, name=None):
  r"""Computes the LSTM cell backward propagation for 1 timestep.

  This implementation is to be used in conjunction of LSTMBlockCell.

  Args:
    x: A `Tensor`. Must be one of the following types: `half`, `float32`.
      The input to the LSTM cell, shape (batch_size, num_inputs).
    cs_prev: A `Tensor`. Must have the same type as `x`.
      The previous cell state.
    h_prev: A `Tensor`. Must have the same type as `x`. The previous h state.
    w: A `Tensor`. Must have the same type as `x`. The weight matrix.
    wci: A `Tensor`. Must have the same type as `x`.
      The weight matrix for input gate peephole connection.
    wcf: A `Tensor`. Must have the same type as `x`.
      The weight matrix for forget gate peephole connection.
    wco: A `Tensor`. Must have the same type as `x`.
      The weight matrix for output gate peephole connection.
    b: A `Tensor`. Must have the same type as `x`. The bias vector.
    i: A `Tensor`. Must have the same type as `x`. The input gate.
    cs: A `Tensor`. Must have the same type as `x`.
      The cell state before the tanh.
    f: A `Tensor`. Must have the same type as `x`. The forget gate.
    o: A `Tensor`. Must have the same type as `x`. The output gate.
    ci: A `Tensor`. Must have the same type as `x`. The cell input.
    co: A `Tensor`. Must have the same type as `x`. The cell after the tanh.
    cs_grad: A `Tensor`. Must have the same type as `x`.
      The current gradient of cs.
    h_grad: A `Tensor`. Must have the same type as `x`.
      The gradient of h vector.
    use_peephole: A `bool`. Whether the cell uses peephole connections.
    name: A name for the operation (optional).

  Returns:
    A tuple of `Tensor` objects (cs_prev_grad, dicfo, wci_grad, wcf_grad, wco_grad).

    cs_prev_grad: A `Tensor`. Has the same type as `x`.
    dicfo: A `Tensor`. Has the same type as `x`.
    wci_grad: A `Tensor`. Has the same type as `x`.
    wcf_grad: A `Tensor`. Has the same type as `x`.
    wco_grad: 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_tensorflow.TFE_Py_FastPathExecute(
        _ctx._context_handle, tld.device_name, "LSTMBlockCellGrad", name,
        tld.op_callbacks, x, cs_prev, h_prev, w, wci, wcf, wco, b, i, cs, f,
        o, ci, co, cs_grad, h_grad, "use_peephole", use_peephole)
      _result = _LSTMBlockCellGradOutput._make(_result)
      return _result
    except _core._FallbackException:
      try:
        return lstm_block_cell_grad_eager_fallback(
            x, cs_prev, h_prev, w, wci, wcf, wco, b, i, cs, f, o, ci, co,
            cs_grad, h_grad, use_peephole=use_peephole, 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.
  use_peephole = _execute.make_bool(use_peephole, "use_peephole")
  _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "LSTMBlockCellGrad", x=x, cs_prev=cs_prev, h_prev=h_prev, w=w,
                             wci=wci, wcf=wcf, wco=wco, b=b, i=i, cs=cs, f=f,
                             o=o, ci=ci, co=co, cs_grad=cs_grad,
                             h_grad=h_grad, use_peephole=use_peephole,
                             name=name)
  _result = _outputs[:]
  if _execute.must_record_gradient():
    _attrs = ("use_peephole", _op._get_attr_bool("use_peephole"), "T",
              _op._get_attr_type("T"))
    _inputs_flat = _op.inputs
    _execute.record_gradient(
        "LSTMBlockCellGrad", _inputs_flat, _attrs, _result)
  _result = _LSTMBlockCellGradOutput._make(_result)
  return _result
def generate_vocab_remapping(new_vocab_file,
                             old_vocab_file,
                             new_vocab_offset,
                             num_new_vocab,
                             old_vocab_size=-1,
                             name=None):
    r"""Given a path to new and old vocabulary files, returns a remapping Tensor of

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

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

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

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

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

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

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

    remapping: A `Tensor` of type `int64`.
    num_present: A `Tensor` of type `int32`.
  """
    _ctx = _context._context or _context.context()
    tld = _ctx._thread_local_data
    if tld.is_eager:
        try:
            _result = _pywrap_tensorflow.TFE_Py_FastPathExecute(
                _ctx._context_handle, tld.device_name,
                "GenerateVocabRemapping", name, tld.op_callbacks,
                new_vocab_file, old_vocab_file, "new_vocab_offset",
                new_vocab_offset, "num_new_vocab", num_new_vocab,
                "old_vocab_size", old_vocab_size)
            _result = _GenerateVocabRemappingOutput._make(_result)
            return _result
        except _core._FallbackException:
            try:
                return generate_vocab_remapping_eager_fallback(
                    new_vocab_file,
                    old_vocab_file,
                    new_vocab_offset=new_vocab_offset,
                    num_new_vocab=num_new_vocab,
                    old_vocab_size=old_vocab_size,
                    name=name,
                    ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
    # Add nodes to the TensorFlow graph.
    new_vocab_offset = _execute.make_int(new_vocab_offset, "new_vocab_offset")
    num_new_vocab = _execute.make_int(num_new_vocab, "num_new_vocab")
    if old_vocab_size is None:
        old_vocab_size = -1
    old_vocab_size = _execute.make_int(old_vocab_size, "old_vocab_size")
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "GenerateVocabRemapping",
        new_vocab_file=new_vocab_file,
        old_vocab_file=old_vocab_file,
        new_vocab_offset=new_vocab_offset,
        num_new_vocab=num_new_vocab,
        old_vocab_size=old_vocab_size,
        name=name)
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("new_vocab_offset", _op._get_attr_int("new_vocab_offset"),
                  "num_new_vocab", _op._get_attr_int("num_new_vocab"),
                  "old_vocab_size", _op._get_attr_int("old_vocab_size"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("GenerateVocabRemapping", _inputs_flat,
                                 _attrs, _result)
    _result = _GenerateVocabRemappingOutput._make(_result)
    return _result
Example #3
0
def roll(input, shift, axis, name=None):
    r"""Rolls the elements of a tensor along an axis.

  The elements are shifted positively (towards larger indices) by the offset of
  `shift` along the dimension of `axis`. Negative `shift` values will shift
  elements in the opposite direction. Elements that roll passed the last position
  will wrap around to the first and vice versa. Multiple shifts along multiple
  axes may be specified.

  For example:

  ```
  # 't' is [0, 1, 2, 3, 4]
  roll(t, shift=2, axis=0) ==> [3, 4, 0, 1, 2]

  # shifting along multiple dimensions
  # 't' is [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]
  roll(t, shift=[1, -2], axis=[0, 1]) ==> [[7, 8, 9, 5, 6], [2, 3, 4, 0, 1]]

  # shifting along the same axis multiple times
  # 't' is [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]
  roll(t, shift=[2, -3], axis=[1, 1]) ==> [[1, 2, 3, 4, 0], [6, 7, 8, 9, 5]]
  ```

  Args:
    input: A `Tensor`.
    shift: A `Tensor`. Must be one of the following types: `int32`, `int64`.
      Dimension must be 0-D or 1-D. `shift[i]` specifies the number of places by which
      elements are shifted positively (towards larger indices) along the dimension
      specified by `axis[i]`. Negative shifts will roll the elements in the opposite
      direction.
    axis: A `Tensor`. Must be one of the following types: `int32`, `int64`.
      Dimension must be 0-D or 1-D. `axis[i]` specifies the dimension that the shift
      `shift[i]` should occur. If the same axis is referenced more than once, the
      total shift for that axis will be the sum of all the shifts that belong to that
      axis.
    name: A name for the operation (optional).

  Returns:
    A `Tensor`. Has the same type as `input`.
  """
    _ctx = _context._context or _context.context()
    tld = _ctx._thread_local_data
    if tld.is_eager:
        try:
            _result = pywrap_tfe.TFE_Py_FastPathExecute(
                _ctx, "Roll", name, input, shift, axis)
            return _result
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
        except _core._FallbackException:
            pass
        try:
            return roll_eager_fallback(input, shift, axis, 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("Roll",
                                                           input=input,
                                                           shift=shift,
                                                           axis=axis,
                                                           name=name)
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("T", _op._get_attr_type("T"), "Tshift",
                  _op._get_attr_type("Tshift"), "Taxis",
                  _op._get_attr_type("Taxis"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("Roll", _inputs_flat, _attrs, _result)
    _result, = _result
    return _result
def encode_wav(audio, sample_rate, name=None):
    r"""Encode audio data using the WAV file format.

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

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

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

  Returns:
    A `Tensor` of type `string`.
  """
    _ctx = _context._context or _context.context()
    tld = _ctx._thread_local_data
    if tld.is_eager:
        try:
            _result = _pywrap_tensorflow.TFE_Py_FastPathExecute(
                _ctx._context_handle, tld.device_name, "EncodeWav", name,
                tld.op_callbacks, audio, sample_rate)
            return _result
        except _core._FallbackException:
            try:
                return encode_wav_eager_fallback(audio,
                                                 sample_rate,
                                                 name=name,
                                                 ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
            except (TypeError, ValueError):
                result = _dispatch.dispatch(encode_wav,
                                            audio=audio,
                                            sample_rate=sample_rate,
                                            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.
    try:
        _, _, _op, _outputs = _op_def_library._apply_op_helper(
            "EncodeWav", audio=audio, sample_rate=sample_rate, name=name)
    except (TypeError, ValueError):
        result = _dispatch.dispatch(encode_wav,
                                    audio=audio,
                                    sample_rate=sample_rate,
                                    name=name)
        if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
            return result
        raise
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ()
        _inputs_flat = _op.inputs
        _execute.record_gradient("EncodeWav", _inputs_flat, _attrs, _result)
    _result, = _result
    return _result
Example #5
0
def enter(data,
          frame_name,
          is_constant=False,
          parallel_iterations=10,
          name=None):
    r"""Creates or finds a child frame, and makes `data` available to the child frame.

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

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

  Returns:
    A `Tensor`. Has the same type as `data`.
  """
    _ctx = _context._context or _context.context()
    tld = _ctx._thread_local_data
    if tld.is_eager:
        try:
            _result = _pywrap_tensorflow.TFE_Py_FastPathExecute(
                _ctx._context_handle, tld.device_name, "Enter", name,
                tld.op_callbacks, data, "frame_name", frame_name,
                "is_constant", is_constant, "parallel_iterations",
                parallel_iterations)
            return _result
        except _core._FallbackException:
            try:
                return enter_eager_fallback(
                    data,
                    frame_name=frame_name,
                    is_constant=is_constant,
                    parallel_iterations=parallel_iterations,
                    name=name,
                    ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
    # Add nodes to the TensorFlow graph.
    frame_name = _execute.make_str(frame_name, "frame_name")
    if is_constant is None:
        is_constant = False
    is_constant = _execute.make_bool(is_constant, "is_constant")
    if parallel_iterations is None:
        parallel_iterations = 10
    parallel_iterations = _execute.make_int(parallel_iterations,
                                            "parallel_iterations")
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "Enter",
        data=data,
        frame_name=frame_name,
        is_constant=is_constant,
        parallel_iterations=parallel_iterations,
        name=name)
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("T", _op._get_attr_type("T"), "frame_name",
                  _op.get_attr("frame_name"), "is_constant",
                  _op._get_attr_bool("is_constant"), "parallel_iterations",
                  _op._get_attr_int("parallel_iterations"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("Enter", _inputs_flat, _attrs, _result)
    _result, = _result
    return _result
def ragged_tensor_from_variant(encoded_ragged, input_ragged_rank, output_ragged_rank, Tvalues, Tsplits=_dtypes.int64, name=None):
  r"""Decodes a `variant` Tensor into a `RaggedTensor`.

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

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

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

    output_nested_splits: A list of `output_ragged_rank` `Tensor` objects with type `Tsplits`.
    output_dense_values: A `Tensor` of type `Tvalues`.
  """
  _ctx = _context._context or _context.context()
  tld = _ctx._thread_local_data
  if tld.is_eager:
    try:
      _result = pywrap_tfe.TFE_Py_FastPathExecute(
        _ctx._context_handle, tld.device_name, "RaggedTensorFromVariant",
        name, tld.op_callbacks, encoded_ragged, "input_ragged_rank",
        input_ragged_rank, "output_ragged_rank", output_ragged_rank,
        "Tvalues", Tvalues, "Tsplits", Tsplits)
      _result = _RaggedTensorFromVariantOutput._make(_result)
      return _result
    except _core._NotOkStatusException as e:
      _ops.raise_from_not_ok_status(e, name)
    except _core._FallbackException:
      pass
    try:
      return ragged_tensor_from_variant_eager_fallback(
          encoded_ragged, input_ragged_rank=input_ragged_rank,
          output_ragged_rank=output_ragged_rank, Tvalues=Tvalues,
          Tsplits=Tsplits, name=name, ctx=_ctx)
    except _core._SymbolicException:
      pass  # Add nodes to the TensorFlow graph.
  # Add nodes to the TensorFlow graph.
  input_ragged_rank = _execute.make_int(input_ragged_rank, "input_ragged_rank")
  output_ragged_rank = _execute.make_int(output_ragged_rank, "output_ragged_rank")
  Tvalues = _execute.make_type(Tvalues, "Tvalues")
  if Tsplits is None:
    Tsplits = _dtypes.int64
  Tsplits = _execute.make_type(Tsplits, "Tsplits")
  _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "RaggedTensorFromVariant", encoded_ragged=encoded_ragged,
                                   input_ragged_rank=input_ragged_rank,
                                   output_ragged_rank=output_ragged_rank,
                                   Tvalues=Tvalues, Tsplits=Tsplits,
                                   name=name)
  _result = _outputs[:]
  if _execute.must_record_gradient():
    _attrs = ("input_ragged_rank", _op._get_attr_int("input_ragged_rank"),
              "output_ragged_rank", _op._get_attr_int("output_ragged_rank"),
              "Tvalues", _op._get_attr_type("Tvalues"), "Tsplits",
              _op._get_attr_type("Tsplits"))
    _inputs_flat = _op.inputs
    _execute.record_gradient(
        "RaggedTensorFromVariant", _inputs_flat, _attrs, _result)
  _result = [_result[:output_ragged_rank]] + _result[output_ragged_rank:]
  _result = _RaggedTensorFromVariantOutput._make(_result)
  return _result
def decode_wav(contents, desired_channels=-1, desired_samples=-1, name=None):
    r"""Decode a 16-bit PCM WAV file to a float tensor.

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

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

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

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

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

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

    audio: A `Tensor` of type `float32`.
    sample_rate: A `Tensor` of type `int32`.
  """
    _ctx = _context._context or _context.context()
    tld = _ctx._thread_local_data
    if tld.is_eager:
        try:
            _result = _pywrap_tensorflow.TFE_Py_FastPathExecute(
                _ctx._context_handle, tld.device_name, "DecodeWav", name,
                tld.op_callbacks, contents, "desired_channels",
                desired_channels, "desired_samples", desired_samples)
            _result = _DecodeWavOutput._make(_result)
            return _result
        except _core._FallbackException:
            try:
                return decode_wav_eager_fallback(
                    contents,
                    desired_channels=desired_channels,
                    desired_samples=desired_samples,
                    name=name,
                    ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
            except (TypeError, ValueError):
                result = _dispatch.dispatch(decode_wav,
                                            contents=contents,
                                            desired_channels=desired_channels,
                                            desired_samples=desired_samples,
                                            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 desired_channels is None:
        desired_channels = -1
    desired_channels = _execute.make_int(desired_channels, "desired_channels")
    if desired_samples is None:
        desired_samples = -1
    desired_samples = _execute.make_int(desired_samples, "desired_samples")
    try:
        _, _, _op, _outputs = _op_def_library._apply_op_helper(
            "DecodeWav",
            contents=contents,
            desired_channels=desired_channels,
            desired_samples=desired_samples,
            name=name)
    except (TypeError, ValueError):
        result = _dispatch.dispatch(decode_wav,
                                    contents=contents,
                                    desired_channels=desired_channels,
                                    desired_samples=desired_samples,
                                    name=name)
        if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
            return result
        raise
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("desired_channels", _op._get_attr_int("desired_channels"),
                  "desired_samples", _op._get_attr_int("desired_samples"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("DecodeWav", _inputs_flat, _attrs, _result)
    _result = _DecodeWavOutput._make(_result)
    return _result
Example #8
0
def stateful_partitioned_call(args, Tout, f, config="", config_proto="", executor_type="", name=None):
  r"""returns `f(inputs)`, where `f`'s body is placed and partitioned.

  Args:
    args: A list of `Tensor` objects. A list of input tensors.
    Tout: A list of `tf.DTypes`. A list of output types.
    f: A function decorated with @Defun.
            A function that takes 'args', a list of tensors, and returns 'output',
            another list of tensors. Input and output types are specified by 'Tin'
            and 'Tout'. The function body of f will be placed and partitioned across
            devices, setting this op apart from the regular Call op. This op is
            stateful.
    config: An optional `string`. Defaults to `""`.
    config_proto: An optional `string`. Defaults to `""`.
    executor_type: An optional `string`. Defaults to `""`.
    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_tensorflow.TFE_Py_FastPathExecute(
        _ctx._context_handle, tld.device_name, "StatefulPartitionedCall",
        name, tld.op_callbacks, args, "Tout", Tout, "f", f, "config", config,
        "config_proto", config_proto, "executor_type", executor_type)
      return _result
    except _core._FallbackException:
      try:
        return stateful_partitioned_call_eager_fallback(
            args, Tout=Tout, f=f, config=config, config_proto=config_proto,
            executor_type=executor_type, name=name, ctx=_ctx)
      except _core._SymbolicException:
        pass  # Add nodes to the TensorFlow graph.
    except _core._NotOkStatusException as e:
      _ops.raise_from_not_ok_status(e, name)
  # Add nodes to the TensorFlow graph.
  if not isinstance(Tout, (list, tuple)):
    raise TypeError(
        "Expected list for 'Tout' argument to "
        "'stateful_partitioned_call' Op, not %r." % Tout)
  Tout = [_execute.make_type(_t, "Tout") for _t in Tout]
  if config is None:
    config = ""
  config = _execute.make_str(config, "config")
  if config_proto is None:
    config_proto = ""
  config_proto = _execute.make_str(config_proto, "config_proto")
  if executor_type is None:
    executor_type = ""
  executor_type = _execute.make_str(executor_type, "executor_type")
  _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "StatefulPartitionedCall", args=args, Tout=Tout, f=f, config=config,
                                   config_proto=config_proto,
                                   executor_type=executor_type, name=name)
  _result = _outputs[:]
  if not _result:
    return _op
  if _execute.must_record_gradient():
    _attrs = ("Tin", _op.get_attr("Tin"), "Tout", _op.get_attr("Tout"), "f",
              _op.get_attr("f"), "config", _op.get_attr("config"),
              "config_proto", _op.get_attr("config_proto"), "executor_type",
              _op.get_attr("executor_type"))
    _inputs_flat = _op.inputs
    _execute.record_gradient(
        "StatefulPartitionedCall", _inputs_flat, _attrs, _result)
  return _result
Example #9
0
def stateless_if(cond, input, Tout, then_branch, else_branch, output_shapes=[], name=None):
  r"""output = cond ? then_branch(input) : else_branch(input)

  Args:
    cond: A `Tensor`.
            A Tensor. If the tensor is a scalar of non-boolean type, the
            scalar is converted to a boolean according to the
            following rule: if the scalar is a numerical value, non-zero means
            `True` and zero means False; if the scalar is a string, non-empty
            means `True` and empty means `False`. If the tensor is not a scalar,
            being empty means False and being non-empty means True.

            This should only be used when the if then/else body functions do not
            have stateful ops.
    input: A list of `Tensor` objects. A list of input tensors.
    Tout: A list of `tf.DTypes`. A list of output types.
    then_branch: A function decorated with @Defun.
            A function that takes 'inputs' and returns a list of tensors, whose
            types are the same as what else_branch returns.
    else_branch: A function decorated with @Defun.
          A function that takes 'inputs' and returns a list of tensors, whose
          types are the same as what then_branch returns.
    output_shapes: An optional list of shapes (each a `tf.TensorShape` or list of `ints`). Defaults to `[]`.
    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_tensorflow.TFE_Py_FastPathExecute(
        _ctx._context_handle, tld.device_name, "StatelessIf", name,
        tld.op_callbacks, cond, input, "Tout", Tout, "then_branch",
        then_branch, "else_branch", else_branch, "output_shapes",
        output_shapes)
      return _result
    except _core._FallbackException:
      try:
        return stateless_if_eager_fallback(
            cond, input, Tout=Tout, then_branch=then_branch,
            else_branch=else_branch, output_shapes=output_shapes, name=name,
            ctx=_ctx)
      except _core._SymbolicException:
        pass  # Add nodes to the TensorFlow graph.
    except _core._NotOkStatusException as e:
      _ops.raise_from_not_ok_status(e, name)
  # Add nodes to the TensorFlow graph.
  if not isinstance(Tout, (list, tuple)):
    raise TypeError(
        "Expected list for 'Tout' argument to "
        "'stateless_if' Op, not %r." % Tout)
  Tout = [_execute.make_type(_t, "Tout") for _t in Tout]
  if output_shapes is None:
    output_shapes = []
  if not isinstance(output_shapes, (list, tuple)):
    raise TypeError(
        "Expected list for 'output_shapes' argument to "
        "'stateless_if' Op, not %r." % output_shapes)
  output_shapes = [_execute.make_shape(_s, "output_shapes") for _s in output_shapes]
  _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "StatelessIf", cond=cond, input=input, Tout=Tout,
                       then_branch=then_branch, else_branch=else_branch,
                       output_shapes=output_shapes, name=name)
  _result = _outputs[:]
  if _execute.must_record_gradient():
    _attrs = ("Tcond", _op._get_attr_type("Tcond"), "Tin",
              _op.get_attr("Tin"), "Tout", _op.get_attr("Tout"),
              "then_branch", _op.get_attr("then_branch"), "else_branch",
              _op.get_attr("else_branch"), "output_shapes",
              _op.get_attr("output_shapes"))
    _inputs_flat = _op.inputs
    _execute.record_gradient(
        "StatelessIf", _inputs_flat, _attrs, _result)
  return _result
Example #10
0
def nccl_all_reduce(input, reduction, num_devices, shared_name, name=None):
    r"""Outputs a tensor containing the reduction across all input tensors.

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

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

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

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

  Returns:
    A `Tensor`. Has the same type as `input`.
  """
    _ctx = _context._context or _context.context()
    tld = _ctx._thread_local_data
    if tld.is_eager:
        try:
            _result = _pywrap_tensorflow.TFE_Py_FastPathExecute(
                _ctx._context_handle, tld.device_name, "NcclAllReduce", name,
                tld.op_callbacks, input, "reduction", reduction, "num_devices",
                num_devices, "shared_name", shared_name)
            return _result
        except _core._FallbackException:
            try:
                return nccl_all_reduce_eager_fallback(input,
                                                      reduction=reduction,
                                                      num_devices=num_devices,
                                                      shared_name=shared_name,
                                                      name=name,
                                                      ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
    # Add nodes to the TensorFlow graph.
    reduction = _execute.make_str(reduction, "reduction")
    num_devices = _execute.make_int(num_devices, "num_devices")
    shared_name = _execute.make_str(shared_name, "shared_name")
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "NcclAllReduce",
        input=input,
        reduction=reduction,
        num_devices=num_devices,
        shared_name=shared_name,
        name=name)
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("reduction", _op.get_attr("reduction"), "T",
                  _op._get_attr_type("T"), "num_devices",
                  _op._get_attr_int("num_devices"), "shared_name",
                  _op.get_attr("shared_name"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("NcclAllReduce", _inputs_flat, _attrs,
                                 _result)
    _result, = _result
    return _result
Example #11
0
def case(branch_index, input, Tout, branches, output_shapes=[], name=None):
  r"""An n-way switch statement which calls a single branch function.

      An n-way switch statement, implementing the following:
      ```
      switch (branch_index) {
        case 0:
          output = branches[0](input);
          break;
        case 1:
          output = branches[1](input);
          break;
        ...
        case [[nbranches-1]]:
        default:
          output = branches[nbranches-1](input);
          break;
      }
      ```

  Args:
    branch_index: A `Tensor` of type `int32`.
      The branch selector, an int32 Tensor.
    input: A list of `Tensor` objects.
      A list of input tensors passed to the branch function.
    Tout: A list of `tf.DTypes`. A list of output types.
    branches: A list of functions decorated with @Defun that has length `>= 1`.
            A list of functions each of which takes 'inputs' and returns a list of
            tensors, whose types are the same as what every other branch returns.
    output_shapes: An optional list of shapes (each a `tf.TensorShape` or list of `ints`). Defaults to `[]`.
    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_tensorflow.TFE_Py_FastPathExecute(
        _ctx._context_handle, tld.device_name, "Case", name, tld.op_callbacks,
        branch_index, input, "Tout", Tout, "branches", branches,
        "output_shapes", output_shapes)
      return _result
    except _core._FallbackException:
      try:
        return case_eager_fallback(
            branch_index, input, Tout=Tout, branches=branches,
            output_shapes=output_shapes, name=name, ctx=_ctx)
      except _core._SymbolicException:
        pass  # Add nodes to the TensorFlow graph.
    except _core._NotOkStatusException as e:
      _ops.raise_from_not_ok_status(e, name)
  # Add nodes to the TensorFlow graph.
  if not isinstance(Tout, (list, tuple)):
    raise TypeError(
        "Expected list for 'Tout' argument to "
        "'case' Op, not %r." % Tout)
  Tout = [_execute.make_type(_t, "Tout") for _t in Tout]
  if not isinstance(branches, (list, tuple)):
    raise TypeError(
        "Expected list for 'branches' argument to "
        "'case' Op, not %r." % branches)
  if output_shapes is None:
    output_shapes = []
  if not isinstance(output_shapes, (list, tuple)):
    raise TypeError(
        "Expected list for 'output_shapes' argument to "
        "'case' Op, not %r." % output_shapes)
  output_shapes = [_execute.make_shape(_s, "output_shapes") for _s in output_shapes]
  _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "Case", branch_index=branch_index, input=input, Tout=Tout,
                branches=branches, output_shapes=output_shapes, name=name)
  _result = _outputs[:]
  if not _result:
    return _op
  if _execute.must_record_gradient():
    _attrs = ("Tin", _op.get_attr("Tin"), "Tout", _op.get_attr("Tout"),
              "branches", _op.get_attr("branches"), "output_shapes",
              _op.get_attr("output_shapes"))
    _inputs_flat = _op.inputs
    _execute.record_gradient(
        "Case", _inputs_flat, _attrs, _result)
  return _result
def decode_proto_v2(bytes,
                    message_type,
                    field_names,
                    output_types,
                    descriptor_source="local://",
                    message_format="binary",
                    sanitize=False,
                    name=None):
    r"""The op extracts fields from a serialized protocol buffers message into tensors.

  The `decode_proto` op extracts fields from a serialized protocol buffers
  message into tensors.  The fields in `field_names` are decoded and converted
  to the corresponding `output_types` if possible.

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

  Each output tensor is a dense tensor. This means that it is padded to hold
  the largest number of repeated elements seen in the input minibatch. (The
  shape is also padded by one to prevent zero-sized dimensions). The actual
  repeat counts for each example in the minibatch can be found in the `sizes`
  output. In many cases the output of `decode_proto` is fed immediately into
  tf.squeeze if missing values are not a concern. When using tf.squeeze, always
  pass the squeeze dimension explicitly to avoid surprises.

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

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

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

  Both binary and text proto serializations are supported, and can be
  chosen using the `format` attribute.

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

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

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

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

  Args:
    bytes: A `Tensor` of type `string`.
      Tensor of serialized protos with shape `batch_shape`.
    message_type: A `string`. Name of the proto message type to decode.
    field_names: A list of `strings`.
      List of strings containing proto field names. An extension field can be decoded
      by using its full name, e.g. EXT_PACKAGE.EXT_FIELD_NAME.
    output_types: A list of `tf.DTypes`.
      List of TF types to use for the respective field in field_names.
    descriptor_source: An optional `string`. Defaults to `"local://"`.
      Either the special value `local://` or a path to a file containing
      a serialized `FileDescriptorSet`.
    message_format: An optional `string`. Defaults to `"binary"`.
      Either `binary` or `text`.
    sanitize: An optional `bool`. Defaults to `False`.
      Whether to sanitize the result or not.
    name: A name for the operation (optional).

  Returns:
    A tuple of `Tensor` objects (sizes, values).

    sizes: A `Tensor` of type `int32`.
    values: A list of `Tensor` objects of type `output_types`.
  """
    _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, "DecodeProtoV2", name,
                tld.op_callbacks, bytes, "message_type", message_type,
                "field_names", field_names, "output_types", output_types,
                "descriptor_source", descriptor_source, "message_format",
                message_format, "sanitize", sanitize)
            _result = _DecodeProtoV2Output._make(_result)
            return _result
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
        except _core._FallbackException:
            pass
        try:
            return decode_proto_v2_eager_fallback(
                bytes,
                message_type=message_type,
                field_names=field_names,
                output_types=output_types,
                descriptor_source=descriptor_source,
                message_format=message_format,
                sanitize=sanitize,
                name=name,
                ctx=_ctx)
        except _core._SymbolicException:
            pass  # Add nodes to the TensorFlow graph.
    # Add nodes to the TensorFlow graph.
    message_type = _execute.make_str(message_type, "message_type")
    if not isinstance(field_names, (list, tuple)):
        raise TypeError("Expected list for 'field_names' argument to "
                        "'decode_proto_v2' Op, not %r." % field_names)
    field_names = [_execute.make_str(_s, "field_names") for _s in field_names]
    if not isinstance(output_types, (list, tuple)):
        raise TypeError("Expected list for 'output_types' argument to "
                        "'decode_proto_v2' Op, not %r." % output_types)
    output_types = [
        _execute.make_type(_t, "output_types") for _t in output_types
    ]
    if descriptor_source is None:
        descriptor_source = "local://"
    descriptor_source = _execute.make_str(descriptor_source,
                                          "descriptor_source")
    if message_format is None:
        message_format = "binary"
    message_format = _execute.make_str(message_format, "message_format")
    if sanitize is None:
        sanitize = False
    sanitize = _execute.make_bool(sanitize, "sanitize")
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "DecodeProtoV2",
        bytes=bytes,
        message_type=message_type,
        field_names=field_names,
        output_types=output_types,
        descriptor_source=descriptor_source,
        message_format=message_format,
        sanitize=sanitize,
        name=name)
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("message_type", _op.get_attr("message_type"), "field_names",
                  _op.get_attr("field_names"), "output_types",
                  _op.get_attr("output_types"), "descriptor_source",
                  _op.get_attr("descriptor_source"), "message_format",
                  _op.get_attr("message_format"), "sanitize",
                  _op._get_attr_bool("sanitize"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("DecodeProtoV2", _inputs_flat, _attrs,
                                 _result)
    _result = _result[:1] + [_result[1:]]
    _result = _DecodeProtoV2Output._make(_result)
    return _result
Example #13
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
Example #14
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
Example #15
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
Example #16
0
def symbolic_gradient(input, Tout, f, name=None):
  r"""Computes the gradient function for function f via backpropagation.

  Args:
    input: A list of `Tensor` objects. a list of input tensors of size N + M;
    Tout: A list of `tf.DTypes` that has length `>= 1`.
      the type list for the input list.
    f: A function decorated with @Defun.
      The function we want to compute the gradient for.

      The function 'f' must be a numerical function which takes N inputs and
      produces M outputs. Its gradient function 'g', which is computed by
      this SymbolicGradient op is a function taking N + M inputs and
      produces N outputs.

      I.e. if we have
         (y1, y2, ..., y_M) = f(x1, x2, ..., x_N),
      then, g is
         (dL/dx1, dL/dx2, ..., dL/dx_N) = g(x1, x2, ..., x_N,
                                           dL/dy1, dL/dy2, ..., dL/dy_M),

      where L is a scalar-value function of (x1, x2, ..., xN) (e.g., the
      loss function). dL/dx_i is the partial derivative of L with respect
      to x_i.

      (Needs some math expert to say the comment above better.)
    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_tensorflow.TFE_Py_FastPathExecute(
        _ctx._context_handle, tld.device_name, "SymbolicGradient", name,
        tld.op_callbacks, input, "Tout", Tout, "f", f)
      return _result
    except _core._FallbackException:
      try:
        return symbolic_gradient_eager_fallback(
            input, Tout=Tout, f=f, name=name, ctx=_ctx)
      except _core._SymbolicException:
        pass  # Add nodes to the TensorFlow graph.
    except _core._NotOkStatusException as e:
      _ops.raise_from_not_ok_status(e, name)
  # Add nodes to the TensorFlow graph.
  if not isinstance(Tout, (list, tuple)):
    raise TypeError(
        "Expected list for 'Tout' argument to "
        "'symbolic_gradient' Op, not %r." % Tout)
  Tout = [_execute.make_type(_t, "Tout") for _t in Tout]
  _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "SymbolicGradient", input=input, Tout=Tout, f=f, name=name)
  _result = _outputs[:]
  if _execute.must_record_gradient():
    _attrs = ("Tin", _op.get_attr("Tin"), "Tout", _op.get_attr("Tout"), "f",
              _op.get_attr("f"))
    _inputs_flat = _op.inputs
    _execute.record_gradient(
        "SymbolicGradient", _inputs_flat, _attrs, _result)
  return _result
def ragged_tensor_to_tensor(shape, values, default_value, row_partition_tensors, row_partition_types, name=None):
  r"""Create a dense tensor from a ragged tensor, possibly altering its shape.

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

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

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

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

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

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

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

  Returns:
    A `Tensor`. Has the same type as `values`.
  """
  _ctx = _context._context or _context.context()
  tld = _ctx._thread_local_data
  if tld.is_eager:
    try:
      _result = pywrap_tfe.TFE_Py_FastPathExecute(
        _ctx._context_handle, tld.device_name, "RaggedTensorToTensor", name,
        tld.op_callbacks, shape, values, default_value, row_partition_tensors,
        "row_partition_types", row_partition_types)
      return _result
    except _core._NotOkStatusException as e:
      _ops.raise_from_not_ok_status(e, name)
    except _core._FallbackException:
      pass
    try:
      return ragged_tensor_to_tensor_eager_fallback(
          shape, values, default_value, row_partition_tensors,
          row_partition_types=row_partition_types, name=name, ctx=_ctx)
    except _core._SymbolicException:
      pass  # Add nodes to the TensorFlow graph.
  # Add nodes to the TensorFlow graph.
  if not isinstance(row_partition_tensors, (list, tuple)):
    raise TypeError(
        "Expected list for 'row_partition_tensors' argument to "
        "'ragged_tensor_to_tensor' Op, not %r." % row_partition_tensors)
  _attr_num_row_partition_tensors = len(row_partition_tensors)
  if not isinstance(row_partition_types, (list, tuple)):
    raise TypeError(
        "Expected list for 'row_partition_types' argument to "
        "'ragged_tensor_to_tensor' Op, not %r." % row_partition_types)
  row_partition_types = [_execute.make_str(_s, "row_partition_types") for _s in row_partition_types]
  _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "RaggedTensorToTensor", shape=shape, values=values,
                                default_value=default_value,
                                row_partition_tensors=row_partition_tensors,
                                row_partition_types=row_partition_types,
                                name=name)
  _result = _outputs[:]
  if _execute.must_record_gradient():
    _attrs = ("T", _op._get_attr_type("T"), "Tindex",
              _op._get_attr_type("Tindex"), "Tshape",
              _op._get_attr_type("Tshape"), "num_row_partition_tensors",
              _op._get_attr_int("num_row_partition_tensors"),
              "row_partition_types", _op.get_attr("row_partition_types"))
    _inputs_flat = _op.inputs
    _execute.record_gradient(
        "RaggedTensorToTensor", _inputs_flat, _attrs, _result)
  _result, = _result
  return _result
Example #18
0
def _while(input, cond, body, output_shapes=[], parallel_iterations=10, name=None):
  r"""output = input; While (Cond(output)) { output = Body(output) }

  Args:
    input: A list of `Tensor` objects.
      A list of input tensors whose types are T.
    cond: A function decorated with @Defun.
            A function takes 'input' and returns a tensor.  If the tensor is
            a scalar of non-boolean, the scalar is converted to a boolean
            according to the following rule: if the scalar is a numerical
            value, non-zero means True and zero means False; if the scalar is
            a string, non-empty means True and empty means False. If the
            tensor is not a scalar, non-emptiness means True and False
            otherwise.
    body: A function decorated with @Defun.
            A function that takes a list of tensors and returns another
            list of tensors. Both lists have the same types as specified
            by T.
    output_shapes: An optional list of shapes (each a `tf.TensorShape` or list of `ints`). Defaults to `[]`.
    parallel_iterations: An optional `int`. Defaults to `10`.
    name: A name for the operation (optional).

  Returns:
    A list of `Tensor` objects. Has the same type as `input`.
  """
  _ctx = _context._context or _context.context()
  tld = _ctx._thread_local_data
  if tld.is_eager:
    try:
      _result = _pywrap_tensorflow.TFE_Py_FastPathExecute(
        _ctx._context_handle, tld.device_name, "While", name,
        tld.op_callbacks, input, "cond", cond, "body", body, "output_shapes",
        output_shapes, "parallel_iterations", parallel_iterations)
      return _result
    except _core._FallbackException:
      try:
        return _while_eager_fallback(
            input, cond=cond, body=body, output_shapes=output_shapes,
            parallel_iterations=parallel_iterations, 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_shapes is None:
    output_shapes = []
  if not isinstance(output_shapes, (list, tuple)):
    raise TypeError(
        "Expected list for 'output_shapes' argument to "
        "'while' Op, not %r." % output_shapes)
  output_shapes = [_execute.make_shape(_s, "output_shapes") for _s in output_shapes]
  if parallel_iterations is None:
    parallel_iterations = 10
  parallel_iterations = _execute.make_int(parallel_iterations, "parallel_iterations")
  _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "While", input=input, cond=cond, body=body,
                 output_shapes=output_shapes,
                 parallel_iterations=parallel_iterations, name=name)
  _result = _outputs[:]
  if not _result:
    return _op
  if _execute.must_record_gradient():
    _attrs = ("T", _op.get_attr("T"), "cond", _op.get_attr("cond"), "body",
              _op.get_attr("body"), "output_shapes",
              _op.get_attr("output_shapes"), "parallel_iterations",
              _op._get_attr_int("parallel_iterations"))
    _inputs_flat = _op.inputs
    _execute.record_gradient(
        "While", _inputs_flat, _attrs, _result)
  return _result
def ragged_tensor_to_variant(rt_nested_splits, rt_dense_values, batched_input, name=None):
  r"""Encodes a `RaggedTensor` into a `variant` Tensor.

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

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

  Returns:
    A `Tensor` of type `variant`.
  """
  _ctx = _context._context or _context.context()
  tld = _ctx._thread_local_data
  if tld.is_eager:
    try:
      _result = pywrap_tfe.TFE_Py_FastPathExecute(
        _ctx._context_handle, tld.device_name, "RaggedTensorToVariant", name,
        tld.op_callbacks, rt_nested_splits, rt_dense_values, "batched_input",
        batched_input)
      return _result
    except _core._NotOkStatusException as e:
      _ops.raise_from_not_ok_status(e, name)
    except _core._FallbackException:
      pass
    try:
      return ragged_tensor_to_variant_eager_fallback(
          rt_nested_splits, rt_dense_values, batched_input=batched_input,
          name=name, ctx=_ctx)
    except _core._SymbolicException:
      pass  # Add nodes to the TensorFlow graph.
  # Add nodes to the TensorFlow graph.
  if not isinstance(rt_nested_splits, (list, tuple)):
    raise TypeError(
        "Expected list for 'rt_nested_splits' argument to "
        "'ragged_tensor_to_variant' Op, not %r." % rt_nested_splits)
  _attr_RAGGED_RANK = len(rt_nested_splits)
  batched_input = _execute.make_bool(batched_input, "batched_input")
  _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "RaggedTensorToVariant", rt_nested_splits=rt_nested_splits,
                                 rt_dense_values=rt_dense_values,
                                 batched_input=batched_input, name=name)
  _result = _outputs[:]
  if _execute.must_record_gradient():
    _attrs = ("RAGGED_RANK", _op._get_attr_int("RAGGED_RANK"), "Tvalues",
              _op._get_attr_type("Tvalues"), "Tsplits",
              _op._get_attr_type("Tsplits"), "batched_input",
              _op._get_attr_bool("batched_input"))
    _inputs_flat = _op.inputs
    _execute.record_gradient(
        "RaggedTensorToVariant", _inputs_flat, _attrs, _result)
  _result, = _result
  return _result
Example #20
0
def image_summary(
        tag,
        tensor,
        max_images=3,
        bad_color=_execute.
    make_tensor(
        """dtype: DT_UINT8 tensor_shape { dim { size: 4 } } int_val: 255 int_val: 0 int_val: 0 int_val: 255""",
        "bad_color"),
        name=None):
    r"""Outputs a `Summary` protocol buffer with images.

  The summary has up to `max_images` summary values containing images. The
  images are built from `tensor` which must be 4-D with shape `[batch_size,
  height, width, channels]` and where `channels` can be:

  *  1: `tensor` is interpreted as Grayscale.
  *  3: `tensor` is interpreted as RGB.
  *  4: `tensor` is interpreted as RGBA.

  The images have the same number of channels as the input tensor. For float
  input, the values are normalized one image at a time to fit in the range
  `[0, 255]`.  `uint8` values are unchanged.  The op uses two different
  normalization algorithms:

  *  If the input values are all positive, they are rescaled so the largest one
     is 255.

  *  If any input value is negative, the values are shifted so input value 0.0
     is at 127.  They are then rescaled so that either the smallest value is 0,
     or the largest one is 255.

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

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

  The `bad_color` argument is the color to use in the generated images for
  non-finite input values.  It is a `uint8` 1-D tensor of length `channels`.
  Each element must be in the range `[0, 255]` (It represents the value of a
  pixel in the output image).  Non-finite values in the input tensor are
  replaced by this tensor in the output image.  The default value is the color
  red.

  Args:
    tag: A `Tensor` of type `string`.
      Scalar. Used to build the `tag` attribute of the summary values.
    tensor: A `Tensor`. Must be one of the following types: `uint8`, `float32`, `half`, `float64`.
      4-D of shape `[batch_size, height, width, channels]` where
      `channels` is 1, 3, or 4.
    max_images: An optional `int` that is `>= 1`. Defaults to `3`.
      Max number of batch elements to generate images for.
    bad_color: An optional `tf.TensorProto`. Defaults to `dtype: DT_UINT8 tensor_shape { dim { size: 4 } } int_val: 255 int_val: 0 int_val: 0 int_val: 255`.
      Color to use for pixels with non-finite values.
    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, "ImageSummary", name, tag, tensor, "max_images",
                max_images, "bad_color", bad_color)
            return _result
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
        except _core._FallbackException:
            pass
        try:
            return image_summary_eager_fallback(tag,
                                                tensor,
                                                max_images=max_images,
                                                bad_color=bad_color,
                                                name=name,
                                                ctx=_ctx)
        except _core._SymbolicException:
            pass  # Add nodes to the TensorFlow graph.
    # Add nodes to the TensorFlow graph.
    if max_images is None:
        max_images = 3
    max_images = _execute.make_int(max_images, "max_images")
    if bad_color is None:
        bad_color = _execute.make_tensor(
            """dtype: DT_UINT8 tensor_shape { dim { size: 4 } } int_val: 255 int_val: 0 int_val: 0 int_val: 255""",
            "bad_color")
    bad_color = _execute.make_tensor(bad_color, "bad_color")
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "ImageSummary",
        tag=tag,
        tensor=tensor,
        max_images=max_images,
        bad_color=bad_color,
        name=name)
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("max_images", _op._get_attr_int("max_images"), "T",
                  _op._get_attr_type("T"), "bad_color",
                  _op.get_attr("bad_color"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("ImageSummary", _inputs_flat, _attrs, _result)
    _result, = _result
    return _result
def audio_spectrogram(input,
                      window_size,
                      stride,
                      magnitude_squared=False,
                      name=None):
    r"""Produces a visualization of audio data over time.

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

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

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

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

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

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

  Returns:
    A `Tensor` of type `float32`.
  """
    _ctx = _context._context or _context.context()
    tld = _ctx._thread_local_data
    if tld.is_eager:
        try:
            _result = _pywrap_tensorflow.TFE_Py_FastPathExecute(
                _ctx._context_handle, tld.device_name, "AudioSpectrogram",
                name, tld.op_callbacks, input, "window_size", window_size,
                "stride", stride, "magnitude_squared", magnitude_squared)
            return _result
        except _core._FallbackException:
            try:
                return audio_spectrogram_eager_fallback(
                    input,
                    window_size=window_size,
                    stride=stride,
                    magnitude_squared=magnitude_squared,
                    name=name,
                    ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
    # Add nodes to the TensorFlow graph.
    window_size = _execute.make_int(window_size, "window_size")
    stride = _execute.make_int(stride, "stride")
    if magnitude_squared is None:
        magnitude_squared = False
    magnitude_squared = _execute.make_bool(magnitude_squared,
                                           "magnitude_squared")
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "AudioSpectrogram",
        input=input,
        window_size=window_size,
        stride=stride,
        magnitude_squared=magnitude_squared,
        name=name)
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("window_size", _op._get_attr_int("window_size"), "stride",
                  _op._get_attr_int("stride"), "magnitude_squared",
                  _op._get_attr_bool("magnitude_squared"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("AudioSpectrogram", _inputs_flat, _attrs,
                                 _result)
    _result, = _result
    return _result
Example #22
0
def _print(input, data, message="", first_n=-1, summarize=3, name=None):
    r"""Prints a list of tensors.

  Passes `input` through to `output` and prints `data` when evaluating.

  Args:
    input: A `Tensor`. The tensor passed to `output`
    data: A list of `Tensor` objects.
      A list of tensors to print out when op is evaluated.
    message: An optional `string`. Defaults to `""`.
      A string, prefix of the error message.
    first_n: An optional `int`. Defaults to `-1`.
      Only log `first_n` number of times. -1 disables logging.
    summarize: An optional `int`. Defaults to `3`.
      Only print this many entries of each tensor.
    name: A name for the operation (optional).

  Returns:
    A `Tensor`. Has the same type as `input`.
  """
    _ctx = _context._context or _context.context()
    tld = _ctx._thread_local_data
    if tld.is_eager:
        try:
            _result = pywrap_tfe.TFE_Py_FastPathExecute(
                _ctx, "Print", name, input, data, "message", message,
                "first_n", first_n, "summarize", summarize)
            return _result
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
        except _core._FallbackException:
            pass
        try:
            return _print_eager_fallback(input,
                                         data,
                                         message=message,
                                         first_n=first_n,
                                         summarize=summarize,
                                         name=name,
                                         ctx=_ctx)
        except _core._SymbolicException:
            pass  # Add nodes to the TensorFlow graph.
    # Add nodes to the TensorFlow graph.
    if message is None:
        message = ""
    message = _execute.make_str(message, "message")
    if first_n is None:
        first_n = -1
    first_n = _execute.make_int(first_n, "first_n")
    if summarize is None:
        summarize = 3
    summarize = _execute.make_int(summarize, "summarize")
    _, _, _op, _outputs = _op_def_library._apply_op_helper("Print",
                                                           input=input,
                                                           data=data,
                                                           message=message,
                                                           first_n=first_n,
                                                           summarize=summarize,
                                                           name=name)
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("T", _op._get_attr_type("T"), "U", _op.get_attr("U"),
                  "message", _op.get_attr("message"), "first_n",
                  _op._get_attr_int("first_n"), "summarize",
                  _op._get_attr_int("summarize"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("Print", _inputs_flat, _attrs, _result)
    _result, = _result
    return _result
def mfcc(spectrogram,
         sample_rate,
         upper_frequency_limit=4000,
         lower_frequency_limit=20,
         filterbank_channel_count=40,
         dct_coefficient_count=13,
         name=None):
    r"""Transforms a spectrogram into a form that's useful for speech recognition.

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

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

  Returns:
    A `Tensor` of type `float32`.
  """
    _ctx = _context._context or _context.context()
    tld = _ctx._thread_local_data
    if tld.is_eager:
        try:
            _result = _pywrap_tensorflow.TFE_Py_FastPathExecute(
                _ctx._context_handle, tld.device_name, "Mfcc", name,
                tld.op_callbacks, spectrogram, sample_rate,
                "upper_frequency_limit", upper_frequency_limit,
                "lower_frequency_limit", lower_frequency_limit,
                "filterbank_channel_count", filterbank_channel_count,
                "dct_coefficient_count", dct_coefficient_count)
            return _result
        except _core._FallbackException:
            try:
                return mfcc_eager_fallback(
                    spectrogram,
                    sample_rate,
                    upper_frequency_limit=upper_frequency_limit,
                    lower_frequency_limit=lower_frequency_limit,
                    filterbank_channel_count=filterbank_channel_count,
                    dct_coefficient_count=dct_coefficient_count,
                    name=name,
                    ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
    # Add nodes to the TensorFlow graph.
    if upper_frequency_limit is None:
        upper_frequency_limit = 4000
    upper_frequency_limit = _execute.make_float(upper_frequency_limit,
                                                "upper_frequency_limit")
    if lower_frequency_limit is None:
        lower_frequency_limit = 20
    lower_frequency_limit = _execute.make_float(lower_frequency_limit,
                                                "lower_frequency_limit")
    if filterbank_channel_count is None:
        filterbank_channel_count = 40
    filterbank_channel_count = _execute.make_int(filterbank_channel_count,
                                                 "filterbank_channel_count")
    if dct_coefficient_count is None:
        dct_coefficient_count = 13
    dct_coefficient_count = _execute.make_int(dct_coefficient_count,
                                              "dct_coefficient_count")
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "Mfcc",
        spectrogram=spectrogram,
        sample_rate=sample_rate,
        upper_frequency_limit=upper_frequency_limit,
        lower_frequency_limit=lower_frequency_limit,
        filterbank_channel_count=filterbank_channel_count,
        dct_coefficient_count=dct_coefficient_count,
        name=name)
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("upper_frequency_limit",
                  _op.get_attr("upper_frequency_limit"),
                  "lower_frequency_limit",
                  _op.get_attr("lower_frequency_limit"),
                  "filterbank_channel_count",
                  _op._get_attr_int("filterbank_channel_count"),
                  "dct_coefficient_count",
                  _op._get_attr_int("dct_coefficient_count"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("Mfcc", _inputs_flat, _attrs, _result)
    _result, = _result
    return _result
Example #24
0
def tensor_summary(tensor,
                   description="",
                   labels=[],
                   display_name="",
                   name=None):
    r"""Outputs a `Summary` protocol buffer with a tensor.

  This op is being phased out in favor of TensorSummaryV2, which lets callers pass
  a tag as well as a serialized SummaryMetadata proto string that contains
  plugin-specific data. We will keep this op to maintain backwards compatibility.

  Args:
    tensor: A `Tensor`. A tensor to serialize.
    description: An optional `string`. Defaults to `""`.
      A json-encoded SummaryDescription proto.
    labels: An optional list of `strings`. Defaults to `[]`.
      An unused list of strings.
    display_name: An optional `string`. Defaults to `""`. An unused string.
    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, "TensorSummary", name, tensor, "description",
                description, "labels", labels, "display_name", display_name)
            return _result
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
        except _core._FallbackException:
            pass
        try:
            return tensor_summary_eager_fallback(tensor,
                                                 description=description,
                                                 labels=labels,
                                                 display_name=display_name,
                                                 name=name,
                                                 ctx=_ctx)
        except _core._SymbolicException:
            pass  # Add nodes to the TensorFlow graph.
    # Add nodes to the TensorFlow graph.
    if description is None:
        description = ""
    description = _execute.make_str(description, "description")
    if labels is None:
        labels = []
    if not isinstance(labels, (list, tuple)):
        raise TypeError("Expected list for 'labels' argument to "
                        "'tensor_summary' Op, not %r." % labels)
    labels = [_execute.make_str(_s, "labels") for _s in labels]
    if display_name is None:
        display_name = ""
    display_name = _execute.make_str(display_name, "display_name")
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "TensorSummary",
        tensor=tensor,
        description=description,
        labels=labels,
        display_name=display_name,
        name=name)
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("T", _op._get_attr_type("T"), "description",
                  _op.get_attr("description"), "labels",
                  _op.get_attr("labels"), "display_name",
                  _op.get_attr("display_name"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("TensorSummary", _inputs_flat, _attrs,
                                 _result)
    _result, = _result
    return _result
def load_and_remap_matrix(ckpt_path,
                          old_tensor_name,
                          row_remapping,
                          col_remapping,
                          initializing_values,
                          num_rows,
                          num_cols,
                          max_rows_in_memory=-1,
                          name=None):
    r"""Loads a 2-D (matrix) `Tensor` with name `old_tensor_name` from the checkpoint

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

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

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

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

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

  The remapping tensors can be generated using the GenerateVocabRemapping op.

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

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

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

  Returns:
    A `Tensor` of type `float32`.
  """
    _ctx = _context._context or _context.context()
    tld = _ctx._thread_local_data
    if tld.is_eager:
        try:
            _result = _pywrap_tensorflow.TFE_Py_FastPathExecute(
                _ctx._context_handle, tld.device_name, "LoadAndRemapMatrix",
                name, tld.op_callbacks, ckpt_path, old_tensor_name,
                row_remapping, col_remapping, initializing_values, "num_rows",
                num_rows, "num_cols", num_cols, "max_rows_in_memory",
                max_rows_in_memory)
            return _result
        except _core._FallbackException:
            try:
                return load_and_remap_matrix_eager_fallback(
                    ckpt_path,
                    old_tensor_name,
                    row_remapping,
                    col_remapping,
                    initializing_values,
                    num_rows=num_rows,
                    num_cols=num_cols,
                    max_rows_in_memory=max_rows_in_memory,
                    name=name,
                    ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
    # Add nodes to the TensorFlow graph.
    num_rows = _execute.make_int(num_rows, "num_rows")
    num_cols = _execute.make_int(num_cols, "num_cols")
    if max_rows_in_memory is None:
        max_rows_in_memory = -1
    max_rows_in_memory = _execute.make_int(max_rows_in_memory,
                                           "max_rows_in_memory")
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "LoadAndRemapMatrix",
        ckpt_path=ckpt_path,
        old_tensor_name=old_tensor_name,
        row_remapping=row_remapping,
        col_remapping=col_remapping,
        initializing_values=initializing_values,
        num_rows=num_rows,
        num_cols=num_cols,
        max_rows_in_memory=max_rows_in_memory,
        name=name)
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("num_rows", _op._get_attr_int("num_rows"), "num_cols",
                  _op._get_attr_int("num_cols"), "max_rows_in_memory",
                  _op._get_attr_int("max_rows_in_memory"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("LoadAndRemapMatrix", _inputs_flat, _attrs,
                                 _result)
    _result, = _result
    return _result
Example #26
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, "AudioSummary", name, 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
def ragged_gather(params_nested_splits,
                  params_dense_values,
                  indices,
                  OUTPUT_RAGGED_RANK,
                  name=None):
    r"""Gather ragged slices from `params` axis `0` according to `indices`.

  Outputs a `RaggedTensor` output composed from `output_dense_values` and
  `output_nested_splits`, such that:

  ```python
  output.shape = indices.shape + params.shape[1:]
  output.ragged_rank = indices.shape.ndims + params.ragged_rank
  output[i...j, d0...dn] = params[indices[i...j], d0...dn]
  ```

  where

  * `params =
     ragged.from_nested_row_splits(params_dense_values, params_nested_splits)`
     provides the values that should be gathered.
  * `indices` ia a dense tensor with dtype `int32` or `int64`, indicating which
     values should be gathered.
  * `output =
     ragged.from_nested_row_splits(output_dense_values, output_nested_splits)`
     is the output tensor.

  (Note: This c++ op is used to implement the higher-level python
  `tf.ragged.gather` op, which also supports ragged indices.)

  Args:
    params_nested_splits: A list of at least 1 `Tensor` objects with the same type in: `int32`, `int64`.
      The `nested_row_splits` tensors that define the row-partitioning for the
      `params` RaggedTensor input.
    params_dense_values: A `Tensor`.
      The `flat_values` for the `params` RaggedTensor. There was a terminology change
      at the python level from dense_values to flat_values, so dense_values is the
      deprecated name.
    indices: A `Tensor`. Must be one of the following types: `int32`, `int64`.
      Indices in the outermost dimension of `params` of the values that should be
      gathered.
    OUTPUT_RAGGED_RANK: An `int` that is `>= 0`.
      The ragged rank of the output RaggedTensor. `output_nested_splits` will contain
      this number of `row_splits` tensors. This value should equal
      `indices.shape.ndims + params.ragged_rank - 1`.
    name: A name for the operation (optional).

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

    output_nested_splits: A list of `OUTPUT_RAGGED_RANK` `Tensor` objects with the same type as `params_nested_splits`.
    output_dense_values: A `Tensor`. Has the same type as `params_dense_values`.
  """
    _ctx = _context._context or _context.context()
    tld = _ctx._thread_local_data
    if tld.is_eager:
        try:
            _result = _pywrap_tensorflow.TFE_Py_FastPathExecute(
                _ctx._context_handle, tld.device_name, "RaggedGather", name,
                tld.op_callbacks, params_nested_splits, params_dense_values,
                indices, "OUTPUT_RAGGED_RANK", OUTPUT_RAGGED_RANK)
            _result = _RaggedGatherOutput._make(_result)
            return _result
        except _core._FallbackException:
            try:
                return ragged_gather_eager_fallback(
                    params_nested_splits,
                    params_dense_values,
                    indices,
                    OUTPUT_RAGGED_RANK=OUTPUT_RAGGED_RANK,
                    name=name,
                    ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
        except _core._NotOkStatusException as e:
            _ops.raise_from_not_ok_status(e, name)
    # Add nodes to the TensorFlow graph.
    if not isinstance(params_nested_splits, (list, tuple)):
        raise TypeError("Expected list for 'params_nested_splits' argument to "
                        "'ragged_gather' Op, not %r." % params_nested_splits)
    _attr_PARAMS_RAGGED_RANK = len(params_nested_splits)
    OUTPUT_RAGGED_RANK = _execute.make_int(OUTPUT_RAGGED_RANK,
                                           "OUTPUT_RAGGED_RANK")
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "RaggedGather",
        params_nested_splits=params_nested_splits,
        params_dense_values=params_dense_values,
        indices=indices,
        OUTPUT_RAGGED_RANK=OUTPUT_RAGGED_RANK,
        name=name)
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("Tvalues", _op._get_attr_type("Tvalues"), "Tindices",
                  _op._get_attr_type("Tindices"), "Tsplits",
                  _op._get_attr_type("Tsplits"), "PARAMS_RAGGED_RANK",
                  _op._get_attr_int("PARAMS_RAGGED_RANK"),
                  "OUTPUT_RAGGED_RANK",
                  _op._get_attr_int("OUTPUT_RAGGED_RANK"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("RaggedGather", _inputs_flat, _attrs, _result)
    _result = [_result[:OUTPUT_RAGGED_RANK]] + _result[OUTPUT_RAGGED_RANK:]
    _result = _RaggedGatherOutput._make(_result)
    return _result
Example #28
0
def dense_table_push_pull(vars, grads, table_handle, name=None):
  r"""push pull variable from parameter server

  Args:
    vars: A list of at least 1 `Tensor` objects with type `resource`.
    grads: A list with the same length as `vars` of `Tensor` objects with type `float32`.
    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, "DenseTablePushPull", name,
        tld.op_callbacks, vars, grads, "table_handle", table_handle)
      return _result
    except _core._FallbackException:
      try:
        return dense_table_push_pull_eager_fallback(
            vars, grads, table_handle=table_handle, name=name, ctx=_ctx)
      except _core._SymbolicException:
        pass  # Add nodes to the TensorFlow graph.
      except (TypeError, ValueError):
        result = _dispatch.dispatch(
              dense_table_push_pull, vars=vars, grads=grads,
                                     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(vars, (list, tuple)):
    raise TypeError(
        "Expected list for 'vars' argument to "
        "'dense_table_push_pull' Op, not %r." % vars)
  _attr_N = len(vars)
  if not isinstance(grads, (list, tuple)):
    raise TypeError(
        "Expected list for 'grads' argument to "
        "'dense_table_push_pull' Op, not %r." % grads)
  if len(grads) != _attr_N:
    raise ValueError(
        "List argument 'grads' to 'dense_table_push_pull' Op with length %d "
        "must match length %d of argument 'vars'." %
        (len(grads), _attr_N))
  table_handle = _execute.make_int(table_handle, "table_handle")
  try:
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "DenseTablePushPull", vars=vars, grads=grads,
                              table_handle=table_handle, name=name)
  except (TypeError, ValueError):
    result = _dispatch.dispatch(
          dense_table_push_pull, vars=vars, grads=grads,
                                 table_handle=table_handle, name=name)
    if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
      return result
    raise
  return _op
Example #29
0
def balance_dataset(input_dataset, output_types, output_shapes, name=None):
    r"""balance input data between datasets

  Args:
    input_dataset: A `Tensor` of type `variant`.
    output_types: A list of `tf.DTypes` that has length `>= 1`.
    output_shapes: A list of shapes (each a `tf.TensorShape` or list of `ints`) that has length `>= 1`.
    name: A name for the operation (optional).

  Returns:
    A `Tensor` of type `variant`.
  """
    _ctx = _context._context or _context.context()
    tld = _ctx._thread_local_data
    if tld.is_eager:
        try:
            _result = pywrap_tfe.TFE_Py_FastPathExecute(
                _ctx._context_handle, tld.device_name, "BalanceDataset", name,
                tld.op_callbacks, input_dataset, "output_types", output_types,
                "output_shapes", output_shapes)
            return _result
        except _core._FallbackException:
            try:
                return balance_dataset_eager_fallback(
                    input_dataset,
                    output_types=output_types,
                    output_shapes=output_shapes,
                    name=name,
                    ctx=_ctx)
            except _core._SymbolicException:
                pass  # Add nodes to the TensorFlow graph.
            except (TypeError, ValueError):
                result = _dispatch.dispatch(balance_dataset,
                                            input_dataset=input_dataset,
                                            output_types=output_types,
                                            output_shapes=output_shapes,
                                            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(output_types, (list, tuple)):
        raise TypeError("Expected list for 'output_types' argument to "
                        "'balance_dataset' Op, not %r." % output_types)
    output_types = [
        _execute.make_type(_t, "output_types") for _t in output_types
    ]
    if not isinstance(output_shapes, (list, tuple)):
        raise TypeError("Expected list for 'output_shapes' argument to "
                        "'balance_dataset' Op, not %r." % output_shapes)
    output_shapes = [
        _execute.make_shape(_s, "output_shapes") for _s in output_shapes
    ]
    try:
        _, _, _op, _outputs = _op_def_library._apply_op_helper(
            "BalanceDataset",
            input_dataset=input_dataset,
            output_types=output_types,
            output_shapes=output_shapes,
            name=name)
    except (TypeError, ValueError):
        result = _dispatch.dispatch(balance_dataset,
                                    input_dataset=input_dataset,
                                    output_types=output_types,
                                    output_shapes=output_shapes,
                                    name=name)
        if result is not _dispatch.OpDispatcher.NOT_SUPPORTED:
            return result
        raise
    _result = _outputs[:]
    if _execute.must_record_gradient():
        _attrs = ("output_types", _op.get_attr("output_types"),
                  "output_shapes", _op.get_attr("output_shapes"))
        _inputs_flat = _op.inputs
        _execute.record_gradient("BalanceDataset", _inputs_flat, _attrs,
                                 _result)
    _result, = _result
    return _result
Example #30
0
def lstm_block_cell(x, cs_prev, h_prev, w, wci, wcf, wco, b, forget_bias=1, cell_clip=3, use_peephole=False, name=None):
  r"""Computes the LSTM cell forward propagation for 1 time step.

  This implementation uses 1 weight matrix and 1 bias vector, and there's an
  optional peephole connection.

  This kernel op implements the following mathematical equations:

  ```python
  xh = [x, h_prev]
  [i, f, ci, o] = xh * w + b
  f = f + forget_bias

  if not use_peephole:
    wci = wcf = wco = 0

  i = sigmoid(cs_prev * wci + i)
  f = sigmoid(cs_prev * wcf + f)
  ci = tanh(ci)

  cs = ci .* i + cs_prev .* f
  cs = clip(cs, cell_clip)

  o = sigmoid(cs * wco + o)
  co = tanh(cs)
  h = co .* o
  ```

  Args:
    x: A `Tensor`. Must be one of the following types: `half`, `float32`.
      The input to the LSTM cell, shape (batch_size, num_inputs).
    cs_prev: A `Tensor`. Must have the same type as `x`.
      Value of the cell state at previous time step.
    h_prev: A `Tensor`. Must have the same type as `x`.
      Output of the previous cell at previous time step.
    w: A `Tensor`. Must have the same type as `x`. The weight matrix.
    wci: A `Tensor`. Must have the same type as `x`.
      The weight matrix for input gate peephole connection.
    wcf: A `Tensor`. Must have the same type as `x`.
      The weight matrix for forget gate peephole connection.
    wco: A `Tensor`. Must have the same type as `x`.
      The weight matrix for output gate peephole connection.
    b: A `Tensor`. Must have the same type as `x`. The bias vector.
    forget_bias: An optional `float`. Defaults to `1`. The forget gate bias.
    cell_clip: An optional `float`. Defaults to `3`.
      Value to clip the 'cs' value to.
    use_peephole: An optional `bool`. Defaults to `False`.
      Whether to use peephole weights.
    name: A name for the operation (optional).

  Returns:
    A tuple of `Tensor` objects (i, cs, f, o, ci, co, h).

    i: A `Tensor`. Has the same type as `x`.
    cs: A `Tensor`. Has the same type as `x`.
    f: A `Tensor`. Has the same type as `x`.
    o: A `Tensor`. Has the same type as `x`.
    ci: A `Tensor`. Has the same type as `x`.
    co: A `Tensor`. Has the same type as `x`.
    h: 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_tensorflow.TFE_Py_FastPathExecute(
        _ctx._context_handle, tld.device_name, "LSTMBlockCell", name,
        tld.op_callbacks, x, cs_prev, h_prev, w, wci, wcf, wco, b,
        "forget_bias", forget_bias, "cell_clip", cell_clip, "use_peephole",
        use_peephole)
      _result = _LSTMBlockCellOutput._make(_result)
      return _result
    except _core._FallbackException:
      try:
        return lstm_block_cell_eager_fallback(
            x, cs_prev, h_prev, w, wci, wcf, wco, b, forget_bias=forget_bias,
            cell_clip=cell_clip, use_peephole=use_peephole, 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 forget_bias is None:
    forget_bias = 1
  forget_bias = _execute.make_float(forget_bias, "forget_bias")
  if cell_clip is None:
    cell_clip = 3
  cell_clip = _execute.make_float(cell_clip, "cell_clip")
  if use_peephole is None:
    use_peephole = False
  use_peephole = _execute.make_bool(use_peephole, "use_peephole")
  _, _, _op, _outputs = _op_def_library._apply_op_helper(
        "LSTMBlockCell", x=x, cs_prev=cs_prev, h_prev=h_prev, w=w, wci=wci,
                         wcf=wcf, wco=wco, b=b, forget_bias=forget_bias,
                         cell_clip=cell_clip, use_peephole=use_peephole,
                         name=name)
  _result = _outputs[:]
  if _execute.must_record_gradient():
    _attrs = ("forget_bias", _op.get_attr("forget_bias"), "cell_clip",
              _op.get_attr("cell_clip"), "use_peephole",
              _op._get_attr_bool("use_peephole"), "T",
              _op._get_attr_type("T"))
    _inputs_flat = _op.inputs
    _execute.record_gradient(
        "LSTMBlockCell", _inputs_flat, _attrs, _result)
  _result = _LSTMBlockCellOutput._make(_result)
  return _result