compute_sentence_piece_proto
函數keras_nlp.tokenizers.compute_sentence_piece_proto(
data, vocabulary_size, model_type="unigram", proto_output_file=None, lowercase=False
)
用於訓練 SentencePiece 詞彙表的工具。
從輸入資料集或檔案名稱清單訓練 SentencePiece 詞彙表。
如果 data
是檔案名稱清單,則檔案格式必須是純文字檔案,並且在訓練期間將逐行讀取文字。
參數
tf.data.Dataset
或檔案名稱清單。"unigram"
、"bpe"
、"word"
或 "char"
其中之一。預設為 "unigram"
。None
,則 model_file 將是 io.BytesIO
物件。預設為 None
。False
。傳回值
具有序列化 SentencePiece proto 的 bytes
物件,如果提供了 proto_output_file,則為 None
。
範例
基本用法(來自資料集)。
>>> inputs = tf.data.Dataset.from_tensor_slices(["Drifting Along"])
>>> proto = keras_nlp.tokenizers.compute_sentence_piece_proto(inputs, vocabulary_size=15)
>>> tokenizer = keras_nlp.tokenizers.SentencePieceTokenizer(proto=proto)
>>> outputs = inputs.map(tokenizer)
>>> for output in outputs:
... print(output)
tf.Tensor([ 4 8 12 5 9 14 5 6 13 4 7 10 11 6 13],
shape=(15,), dtype=int32)
基本用法(使用檔案)。
with open("test.txt", "w+") as f: f.write("Drifting Along\n")
inputs = ["test.txt"]
proto = keras_nlp.tokenizers.compute_sentence_piece_proto(
inputs, vocabulary_size=15, proto_output_file="model.spm")
tokenizer = keras_nlp.tokenizers.SentencePieceTokenizer(proto="model.spm")
ds = tf.data.Dataset.from_tensor_slices(["the quick brown fox."])
ds = ds.map(tokenizer)
使用 lowercase 的用法
>>> inputs = tf.data.Dataset.from_tensor_slices(["Drifting Along"])
>>> proto = keras_nlp.tokenizers.compute_sentence_piece_proto(
... inputs, vocabulary_size=15, lowercase=True)
>>> tokenizer = keras_nlp.tokenizers.SentencePieceTokenizer(proto=proto)
>>> outputs = inputs.map(tokenizer)
>>> for output in outputs:
... print(output)
tf.Tensor([ 4 8 12 5 9 14 5 6 13 4 7 10 11 6 13],
shape=(15,), dtype=int32)