當前位置: 首頁>>代碼示例>>Python>>正文


Python string_ops.substr方法代碼示例

本文整理匯總了Python中tensorflow.python.ops.string_ops.substr方法的典型用法代碼示例。如果您正苦於以下問題:Python string_ops.substr方法的具體用法?Python string_ops.substr怎麽用?Python string_ops.substr使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在tensorflow.python.ops.string_ops的用法示例。


在下文中一共展示了string_ops.substr方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: getTokenWord

# 需要導入模塊: from tensorflow.python.ops import string_ops [as 別名]
# 或者: from tensorflow.python.ops.string_ops import substr [as 別名]
def getTokenWord(self, text, token_starts, token_ends):
    def _FindSubstr(input_tensor):
      text, token_start, token_length = input_tensor
      return string_ops.substr(text, token_start, token_length)

    token_lengths = token_ends - token_starts
    token_word = ragged_map_ops.map_fn(
        _FindSubstr, (text, token_starts, token_lengths),
        dtype=ragged_tensor.RaggedTensorType(
            dtype=dtypes.string, ragged_rank=1),
        infer_shape=False)
    return token_word 
開發者ID:tensorflow,項目名稱:text,代碼行數:14,代碼來源:sentence_breaking_ops_test.py

示例2: decode_image

# 需要導入模塊: from tensorflow.python.ops import string_ops [as 別名]
# 或者: from tensorflow.python.ops.string_ops import substr [as 別名]
def decode_image(contents, channels=None, name=None):
  """Convenience function for `decode_gif`, `decode_jpeg`, and `decode_png`.
  Detects whether an image is a GIF, JPEG, or PNG, and performs the appropriate 
  operation to convert the input bytes `string` into a `Tensor` of type `uint8`.

  Note: `decode_gif` returns a 4-D array `[num_frames, height, width, 3]`, as 
  opposed to `decode_jpeg` and `decode_png`, which return 3-D arrays 
  `[height, width, num_channels]`. Make sure to take this into account when 
  constructing your graph if you are intermixing GIF files with JPEG and/or PNG 
  files.

  Args:
    contents: 0-D `string`. The encoded image bytes.
    channels: An optional `int`. Defaults to `0`. Number of color channels for 
      the decoded image.
    name: A name for the operation (optional)
    
  Returns:
    `Tensor` with type `uint8` with shape `[height, width, num_channels]` for 
      JPEG and PNG images and shape `[num_frames, height, width, 3]` for GIF 
      images.
  """
  with ops.name_scope(name, 'decode_image') as scope:
    if channels not in (None, 0, 1, 3):
      raise ValueError('channels must be in (None, 0, 1, 3)')
    substr = string_ops.substr(contents, 0, 4)

    def _gif():
      # Create assert op to check that bytes are GIF decodable
      is_gif = math_ops.equal(substr, b'\x47\x49\x46\x38', name='is_gif')
      decode_msg = 'Unable to decode bytes as JPEG, PNG, or GIF'
      assert_decode = control_flow_ops.Assert(is_gif, [decode_msg])
      # Create assert to make sure that channels is not set to 1
      # Already checked above that channels is in (None, 0, 1, 3)
      gif_channels = 0 if channels is None else channels
      good_channels = math_ops.not_equal(gif_channels, 1, name='check_channels')
      channels_msg = 'Channels must be in (None, 0, 3) when decoding GIF images'
      assert_channels = control_flow_ops.Assert(good_channels, [channels_msg])
      with ops.control_dependencies([assert_decode, assert_channels]):
        return gen_image_ops.decode_gif(contents)

    def _png():
      return gen_image_ops.decode_png(contents, channels)

    def check_png():
      is_png = math_ops.equal(substr, b'\211PNG', name='is_png')
      return control_flow_ops.cond(is_png, _png, _gif, name='cond_png')

    def _jpeg():
      return gen_image_ops.decode_jpeg(contents, channels)

    is_jpeg = math_ops.equal(substr, b'\xff\xd8\xff\xe0', name='is_jpeg')
    return control_flow_ops.cond(is_jpeg, _jpeg, check_png, name='cond_jpeg') 
開發者ID:abhisuri97,項目名稱:auto-alt-text-lambda-api,代碼行數:55,代碼來源:image_ops_impl.py

示例3: break_sentences_with_offsets

# 需要導入模塊: from tensorflow.python.ops import string_ops [as 別名]
# 或者: from tensorflow.python.ops.string_ops import substr [as 別名]
def break_sentences_with_offsets(self, doc):
    """Splits `doc` into sentence fragments, returns text, start & end offsets.

    Example:
                      1                  1         2         3
            012345678901234    01234567890123456789012345678901234567
      doc: 'Hello...foo bar', 'Welcome to the U.S. don't be surprised'

      fragment_text: [['Hello...', 'foo bar'], ['Welcome to the U.S.' , 'don't
      be surprised']]
      start: [[0, 8],[0, 20]]
      end: [[8, 15],[19, 38]]

    Args:
      doc: A string `Tensor` of shape [batch] with a batch of documents.

    Returns:
      A tuple of (fragment_text, start, end) where:

      fragment_text: A string `RaggedTensor` of shape [batch, (num_sentences)]
      with each input broken up into its constituent sentence fragments.
      start: A int64 `RaggedTensor` of shape [batch, (num_sentences)]
        where each entry is the inclusive beginning byte offset of a sentence.
      end: A int64 `RaggedTensor` of shape [batch, (num_sentences)]
        where each entry is the exclusive ending byte offset of a sentence.
    """
    if not isinstance(doc, ragged_tensor.RaggedTensor):
      doc = ragged_tensor.RaggedTensor.from_tensor(doc)

    # Run sentence fragmenter op v2
    fragment = gen_state_based_sentence_breaker_op.sentence_fragments_v2(
        doc=doc.flat_values)
    start, end, properties, terminal_punc_token, row_lengths = fragment

    # Pack and create `RaggedTensor`s
    start, end, properties, terminal_punc_token = tuple(
        ragged_tensor.RaggedTensor.from_row_lengths(value, row_lengths)
        for value in [start, end, properties, terminal_punc_token])

    # Helper for use within map_fn (function must only take in one argument)
    def _substring(x):
      s, pos, length = x
      return string_ops.substr(s, pos, length)

    # Extract fragment text using offsets
    fragment_text = ragged_map_ops.map_fn(
        _substring, (doc, start, math_ops.subtract(end, start)),
        infer_shape=False,
        dtype=dtypes.string)

    return fragment_text, start, end 
開發者ID:tensorflow,項目名稱:text,代碼行數:53,代碼來源:state_based_sentence_breaker_op.py


注:本文中的tensorflow.python.ops.string_ops.substr方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。