目录

torchtext.transforms

转换是常见的文本转换。它们可以使用torch.nn.Sequential或使用torchtext.transforms.Sequential以支持 torch 脚本化。

SentencePieceTokenizer

sp_model_path str[来源]torchtext.transforms.SentencePieceTokenizer

从预先训练的句子词模型转换 Sentence Piece 分词器

Additiona 详细信息: https://github.com/google/sentencepiece

参数

sp_model_pathstr) – 预训练句子模型的路径

>>> from torchtext.transforms import SpmTokenizerTransform
>>> transform = SentencePieceTokenizer("spm_model")
>>> transform(["hello world", "attention is all you need!"])
教程使用:SentencePieceTokenizer
forward(input AnyAny[来源]
参数

inputUnion[strList[str]]) – 输入要应用分词器的句子或句子列表。

返回

分词文本

返回类型

联合[List[str], List[List(str)]]

GPT2BPETokenizer

encoder_json_pathstr,vocab_bpe_path:str[来源]torchtext.transforms.GPT2BPETokenizer
forward(input AnyAny[来源]
参数

inputUnion[strList[str]]) – 输入要应用分词器的句子或句子列表。

返回

分词文本

返回类型

联合[List[str], List[List(str)]]

CLIPTokenizer

class merges_path strencoder_json_path Optional[str] = Nonenum_merges: 可选[int] = [来源]torchtext.transforms.CLIPTokenizer
forward(input AnyAny[来源]
参数

inputUnion[strList[str]]) – 输入要应用分词器的句子或句子列表。

返回

分词文本

返回类型

联合[List[str], List[List(str)]]

词汇转换

词汇torchtext.vocab.vocab.Vocab)[来源]torchtext.transforms.VocabTransform

词汇转换,用于将输入批次的词元转换为相应的词元 ID

参数

词汇 – 一个torchtext.vocab.Vocab类。

>>> import torch
>>> from torchtext.vocab import vocab
>>> from torchtext.transforms import VocabTransform
>>> from collections import OrderedDict
>>> vocab_obj = vocab(OrderedDict([('a', 1), ('b', 1), ('c', 1)]))
>>> vocab_transform = VocabTransform(vocab_obj)
>>> output = vocab_transform([['a','b'],['a','b','c']])
>>> jit_vocab_transform = torch.jit.script(vocab_transform)
教程使用:VocabTransform
forward(input AnyAny[来源]
参数

inputUnion[List[str]List[List[str]]] – 要转换为相应令牌 ID 的输入令牌批次

返回

将输入转换为相应的令牌 ID

返回类型

Union[List[int], List[List[int]]]

ToTensor

class padding_value: 可选[int] = dtype torch.dtype = torch.int64[来源]torchtext.transforms.ToTensor

将输入转换为 torch 张量

参数
  • padding_valueOptional[int]) – 填充值,使批次中的每个输入的长度等于批次中最长的序列。

  • dtype () – 输出张量torch.dtypetorch.dtype

forward(input Any割torch。张量[来源]
参数

inputUnion[List[int]List[List[int]]]) – 令牌 ID 的序列或批次

返回类型

张肌

标签到索引

class label_names 可选[List[str]] = label_path 可选[str] = sort_names=False[来源]torchtext.transforms.LabelToIndex

将标签从 string 名称转换为 id。

参数
  • label_namesOptional[List[str]]) – 唯一标签名称的列表

  • label_pathOptional[str]) – 包含唯一标签名称的文件路径,每行包含 1 个标签。请注意,应提供 label_names 或 label_path 但不能两者兼而有之。

forward(input AnyAny[来源]
参数

inputUnion[strList[str]]) – 输入要转换为相应 ID 的标签

返回类型

联合[int, 列表[int]]

截断

max_seq_len int[来源]torchtext.transforms.Truncate

Truncate input sequence (截断输入序列)

参数

max_seq_lenint) – 输入序列允许的最大长度

教程使用:Truncate
forward(input AnyAny[来源]
参数

inputUnion[List[Union[strint]]], List[List[Union[strint]]]]]) – 输入序列或批次要截断的序列

返回

截断序列

返回类型

Union[List[Union[strint]], List[List[Union[strint]]]]

添加令牌

class token Union[int str]begin bool = True[来源]torchtext.transforms.AddToken

将标记添加到序列的开头或结尾

参数
  • tokenUnion[intstr]) – 要添加的令牌

  • beginbooloptional) – 是在开始或结束还是序列处插入标记,默认为 True

教程使用:AddToken
forward(input AnyAny[来源]
参数

inputUnion[List[Union[strint]]], List[List[Union[strint]]]]]) – 输入序列或批次

顺序

class *args torch.nn.modules.module.Module[来源]torchtext.transforms.Sequential
class arg OrderedDict[str Module]torchtext.transforms.Sequential)

用于托管一系列文本转换的容器。

教程使用:Sequential
forward(input AnyAny[来源]
参数

inputAny) - 输入序列或批次。序列中的第一个转换必须支持输入类型。

文档

访问 PyTorch 的全面开发人员文档

查看文档

教程

获取面向初学者和高级开发人员的深入教程

查看教程

资源

查找开发资源并解答您的问题

查看资源