基於 data 創建一個 n-grams 的張量。
用法
tf.strings.ngrams(
data, ngram_width, separator=' ', pad_values=None, padding_width=None,
preserve_short_sequences=False, name=None
)參數
-
data包含 ngram 的源數據的張量或 RaggedTensor。 -
ngram_width要創建的 ngram 的寬度。如果這是一個列表或元組,則 op 將按列表順序返回所有指定元組的 ngram。值必須是大於 0 的非張量整數。 -
separatorngram 元素之間使用的分隔符字符串。必須是字符串常量,而不是張量。 -
pad_values(left_pad_value、right_pad_value) 的元組、單個字符串或無。如果為 None,則不會添加任何填充;如果是單個字符串,則該字符串將用於左右填充。值必須是 Python 字符串。 -
padding_width如果設置,padding_width填充值將添加到每個序列的兩側。默認為ngram_width-1。必須大於- (請注意,無論該值如何,都不會填充 1-gram。)
-
preserve_short_sequences如果為真,則確保為每個輸入序列生成至少一個 ngram。特別是,如果輸入序列比min(ngram_width) + 2*pad_width短,則生成包含整個序列的單個 ngram。如果為 false,則不會為這些短輸入序列生成 ngram。 -
name操作名稱。
返回
-
ngram 的 RaggedTensor。如果
data.shape=[D1...DN, S],則output.shape=[D1...DN, NUM_NGRAMS],其中NUM_NGRAMS=S-ngram_width+1+2*padding_width。
拋出
-
TypeError如果pad_values設置為無效類型。 -
ValueError如果pad_values,padding_width或ngram_width設置為無效值。
基於 data 創建一個 n-grams 的張量。 n-grams 是通過使用 separator 從 data 的內軸連接 width 相鄰字符串的窗口來創建的。
如果需要,可以使用pad_values 參數在序列的開始和結束處填充輸入數據。如果設置,pad_values 應該包含字符串元組或單個字符串;元組的第 0 個元素將用於填充序列的左側,元組的第一個元素將用於填充序列的右側。 padding_width 參數控製每邊添加多少填充值;它默認為 ngram_width-1 。
如果此操作被配置為沒有填充,或者如果它被配置為添加填充且 padding_width 設置為小於 ngram_width-1,則序列或序列加填充可能小於 ngram寬度。在這種情況下,不會為該序列生成任何 ngram。這可以通過設置 preserve_short_sequences 來防止,這將導致操作始終為每個非空序列生成至少一個 ngram。
例子:
tf.strings.ngrams(["A", "B", "C", "D"], 2).numpy()
array([b'A B', b'B C', b'C D'], dtype=object)
tf.strings.ngrams(["TF", "and", "keras"], 1).numpy()
array([b'TF', b'and', b'keras'], dtype=object)
相關用法
- Python tf.strings.substr用法及代碼示例
- 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.split用法及代碼示例
- Python tf.strings.upper用法及代碼示例
- Python tf.strings.unicode_decode_with_offsets用法及代碼示例
- Python tf.strings.join用法及代碼示例
- Python tf.strings.to_hash_bucket用法及代碼示例
- 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用法及代碼示例
- Python tf.strings.to_number用法及代碼示例
注:本文由純淨天空篩選整理自tensorflow.org大神的英文原創作品 tf.strings.ngrams。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
