從字符串的Tensor 返回子字符串。
用法
tf.strings.substr(
input, pos, len, unit='BYTE', name=None
)拋出
-
ValueError:如果第一個參數不能轉換為dtype string的張量。InvalidArgumentError:如果索引超出範圍。ValueError:如果pos和len不是同一個形狀。
參數
-
inputTensor類型為string。弦張量 -
pos一個Tensor。必須是以下類型之一:int32,int64。標量定義每個子字符串中第一個字符的位置 -
len一個Tensor。必須與pos具有相同的類型。標量定義要包含在每個子字符串中的字符數 -
unit一個可選的string來自:"BYTE", "UTF8_CHAR"。默認為"BYTE"。用於創建子字符串的單位。以下之一:"BYTE"(用於按字節定義位置和長度)或"UTF8_CHAR"(用於 UTF-8 編碼的 Unicode 代碼點)。默認值為"BYTE"。如果unit=UTF8_CHAR和input字符串不包含結構上有效的 UTF-8,則結果未定義。 -
name操作的名稱(可選)。
返回
-
Tensor類型為string。
對於輸入 Tensor 中的每個字符串,創建一個從索引 pos 開始的子字符串,總長度為 len 。
如果len 定義了一個超出輸入字符串長度的子字符串,或者如果len 為負數,則使用盡可能多的字符。
負數pos 表示字符串內從末端向後的距離。
如果 pos 指定的索引超出任何輸入字符串的範圍,則拋出 InvalidArgumentError。
pos 和 len 必須具有相同的形狀,否則在創建操作時會拋出 ValueError。
注意: Substr最多支持二維廣播。更多關於廣播這裏
例子
使用標量 pos 和 len :
input = [b'Hello', b'World']
position = 1
length = 3
output = [b'ell', b'orl']
使用與 input 形狀相同的 pos 和 len :
input = [[b'ten', b'eleven', b'twelve'],
[b'thirteen', b'fourteen', b'fifteen'],
[b'sixteen', b'seventeen', b'eighteen']]
position = [[1, 2, 3],
[1, 2, 3],
[1, 2, 3]]
length = [[2, 3, 4],
[4, 3, 2],
[5, 5, 5]]
output = [[b'en', b'eve', b'lve'],
[b'hirt', b'urt', b'te'],
[b'ixtee', b'vente', b'hteen']]
將 pos 和 len 廣播到 input :
input = [[b'ten', b'eleven', b'twelve'],
[b'thirteen', b'fourteen', b'fifteen'],
[b'sixteen', b'seventeen', b'eighteen'],
[b'nineteen', b'twenty', b'twentyone']]
position = [1, 2, 3]
length = [1, 2, 3]
output = [[b'e', b'ev', b'lve'],
[b'h', b'ur', b'tee'],
[b'i', b've', b'hte'],
[b'i', b'en', b'nty']]
將 input 廣播到 pos 和 len :
input = b'thirteen'
position = [1, 5, 7]
length = [3, 2, 1]
output = [b'hir', b'ee', b'n']
相關用法
- Python tf.strings.split用法及代碼示例
- Python tf.strings.strip用法及代碼示例
- Python tf.strings.reduce_join用法及代碼示例
- Python tf.strings.regex_full_match用法及代碼示例
- Python tf.strings.regex_replace用法及代碼示例
- Python tf.strings.length用法及代碼示例
- Python tf.strings.bytes_split用法及代碼示例
- Python tf.strings.as_string用法及代碼示例
- Python tf.strings.unsorted_segment_join用法及代碼示例
- Python tf.strings.lower用法及代碼示例
- Python tf.strings.upper用法及代碼示例
- Python tf.strings.unicode_decode_with_offsets用法及代碼示例
- Python tf.strings.join用法及代碼示例
- Python tf.strings.to_hash_bucket用法及代碼示例
- Python tf.strings.ngrams用法及代碼示例
- Python tf.strings.to_hash_bucket_strong用法及代碼示例
- Python tf.strings.unicode_decode用法及代碼示例
- Python tf.strings.unicode_encode用法及代碼示例
- Python tf.strings.format用法及代碼示例
- Python tf.strings.to_hash_bucket_fast用法及代碼示例
注:本文由純淨天空篩選整理自tensorflow.org大神的英文原創作品 tf.strings.substr。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
