用法:
SubwordTokenizer.__call__(text, max_length: int, max_num_rows: int, add_special_tokens: bool = True, padding: str = 'max_length', truncation: Union[bool, str] = False, stride: int = 0, return_tensors: str = 'cp', return_token_type_ids: bool = False)
在 cuDF 字符串列上運行 CUDA BERT 子詞標記器。使用來自預訓練標記器的詞匯將單詞編碼為標記 ID。
- text:cudf字符串係列
要編碼的序列批次。
- max_length:int
控製要使用或填充的最大長度。
- max_num_rows:int
輸出token-ids 預計由標記器生成的最大行數。用於在 GPU 設備上分配臨時工作內存。如果輸出生成大量行,則行為未定義。這將根據步幅、截斷和max_length 而有所不同。例如,對於非重疊序列,輸出行將與輸入行相同。一個好的默認值可以是 max_length 的兩倍
- add_special_tokens:bool,可選,默認為 True
是否使用 BERT 分類模型的特殊標記對序列進行編碼
- padding:“max_length”
填充到參數 max_length 指定的最大長度
- truncation:布爾值,默認為 False
True:截斷到參數指定的最大長度 max_length False 或 ‘do_not_truncate’:默認不截斷(輸出與 HuggingFace 不同)
- stride:int,可選,默認為 0
此參數的值定義重疊標記的數量。有關重疊標記的信息存在於輸出的元數據中。
- return_tensors:str, {“cp”, “pt”, “tf”} 默認為 “cp”
“cp”:返回 cupy cp.ndarray 對象 “tf”:返回 TensorFlow tf.constant 對象 “pt”:返回 PyTorch torch.Tensor 對象
- return_token_type_ids:布爾型,可選
目前僅支持 False
- 具有以下字段的編碼:
- input_ids:(類型由return_tensors定義)
要饋送到模型的令牌 ID 的張量。
- attention_mask:(類型由return_tensors定義)
指定模型應關注哪些標記的索引張量
- 元數據:(由return_tensors定義的類型)
每行包含原始字符串的索引 id 以及 token-ids 的第一個和最後一個索引,它們是非填充和非重疊的
參數:
返回:
例子:
>>> import cudf >>> from cudf.utils.hash_vocab_utils import hash_vocab >>> hash_vocab('bert-base-cased-vocab.txt', 'voc_hash.txt')
>>> from cudf.core.subword_tokenizer import SubwordTokenizer >>> cudf_tokenizer = SubwordTokenizer('voc_hash.txt', ... do_lower_case=True) >>> str_series = cudf.Series(['This is the', 'best book']) >>> tokenizer_output = cudf_tokenizer(str_series, ... max_length=8, ... max_num_rows=len(str_series), ... padding='max_length', ... return_tensors='pt', ... truncation=True) >>> tokenizer_output['input_ids'] tensor([[ 101, 1142, 1110, 1103, 102, 0, 0, 0], [ 101, 1436, 1520, 102, 0, 0, 0, 0]], device='cuda:0', dtype=torch.int32) >>> tokenizer_output['attention_mask'] tensor([[1, 1, 1, 1, 1, 0, 0, 0], [1, 1, 1, 1, 0, 0, 0, 0]], device='cuda:0', dtype=torch.int32) >>> tokenizer_output['metadata'] tensor([[0, 1, 3], [1, 1, 2]], device='cuda:0', dtype=torch.int32)
相關用法
- Python cudf.core.series.DatetimeProperties.month用法及代碼示例
- Python cudf.core.series.DatetimeProperties.year用法及代碼示例
- Python cudf.core.series.DatetimeProperties.second用法及代碼示例
- Python cudf.core.series.TimedeltaProperties.microseconds用法及代碼示例
- Python cudf.core.series.DatetimeProperties.dayofweek用法及代碼示例
- Python cudf.core.series.DatetimeProperties.isocalendar用法及代碼示例
- Python cudf.core.series.DatetimeProperties.hour用法及代碼示例
- Python cudf.core.series.TimedeltaProperties.components用法及代碼示例
- Python cudf.core.series.DatetimeProperties.ceil用法及代碼示例
- Python cudf.core.series.DatetimeProperties.floor用法及代碼示例
- Python cudf.core.series.TimedeltaProperties.days用法及代碼示例
- Python cudf.core.series.TimedeltaProperties.seconds用法及代碼示例
- Python cudf.core.series.DatetimeProperties.quarter用法及代碼示例
- Python cudf.core.series.DatetimeProperties.weekday用法及代碼示例
- Python cudf.core.series.DatetimeProperties.day用法及代碼示例
- Python cudf.core.series.DatetimeProperties.dayofyear用法及代碼示例
- Python cudf.core.series.DatetimeProperties.minute用法及代碼示例
- Python cudf.core.series.DatetimeProperties.day_of_year用法及代碼示例
- Python cudf.core.series.DatetimeProperties.round用法及代碼示例
- Python cudf.core.series.TimedeltaProperties.nanoseconds用法及代碼示例
注:本文由純淨天空篩選整理自rapids.ai大神的英文原創作品 cudf.core.subword_tokenizer.SubwordTokenizer.__call__。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。