目录

torchtext.transforms

变换是常见的文本变换。它们可以使用torch.nn.Sequential链接在一起,或者使用torchtext.transforms.Sequential以支持torch脚本化。

SentencePieceTokenizer

class torchtext.transforms.SentencePieceTokenizer(sp_model_path: str)[source]

来自预训练 SentencePiece 模型的句子片段转换器

附加详情:https://github.com/google/sentencepiece

Parameters

sp_model_path (str) – 预训练 SentencePiece 模型的路径

Example
>>> from torchtext.transforms import SpmTokenizerTransform
>>> transform = SentencePieceTokenizer("spm_model")
>>> transform(["hello world", "attention is all you need!"])
Tutorials using SentencePieceTokenizer:
forward(input: Any)Any[source]
Parameters

输入 (Union[str, List[str]]) – 需要应用分词器的输入句子或句子列表。

Returns

碎片化文本

Return type

联合[List[字符串], List[List(字符串)]]

GPT2BPETokenizer

class torchtext.transforms.GPT2BPETokenizer(encoder_json_path: str, vocab_bpe_path: str)[source]
forward(input: Any)Any[source]
Parameters

输入 (Union[str, List[str]]) – 需要应用分词器的输入句子或句子列表。

Returns

碎片化文本

Return type

联合[List[字符串], List[List(字符串)]]

CLIPTokenizer

class torchtext.transforms.CLIPTokenizer(merges_path: str, encoder_json_path: Optional[str] = None, num_merges: Optional[int] = None)[source]
forward(input: Any)Any[source]
Parameters

输入 (Union[str, List[str]]) – 需要应用分词器的输入句子或句子列表。

Returns

碎片化文本

Return type

联合[List[字符串], List[List(字符串)]]

VocabTransform

class torchtext.transforms.VocabTransform(vocab: torchtext.vocab.vocab.Vocab)[source]

词汇转换将输入的token批处理转换为对应的token ID。

Parameters

词汇表 – 是 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)
Tutorials using VocabTransform:
forward(input: Any)Any[source]
Parameters

输入 (Union[List[str], List[List[str]]]) – 要转换为相应token id的token批输入

Returns

将输入的内容转换为对应的标记 ID。

Return type

联合[List[整数], List[List[整数]]]

ToTensor

class torchtext.transforms.ToTensor(padding_value: Optional[int] = None, dtype: torch.dtype = torch.int64)[source]

将输入转换为 torch 张量

Parameters
  • 填充值 (可选[整数]) – 填充值以使批次中的每个输入长度等于批次中最长序列的长度。

  • 数据类型 (torch.dtype) – torch.dtype 输出张量的数据类型

forward(input: Any)torch.Tensor[source]
Parameters

输入 (Union[List[int], List[List[int]]]) – 词元 ID 的序列或批处理

Return type

张量

LabelToIndex

class torchtext.transforms.LabelToIndex(label_names: Optional[List[str]] = None, label_path: Optional[str] = None, sort_names=False)[source]

将标签从字符串名称转换为编号。

Parameters
  • 标签名称 (可选[列表[字符串]]) – 一个包含唯一标签名称的列表

  • label_path (可选[str]) – 文件路径,该文件包含每行一个唯一的标签名称。注意,label_names 和 label_path 中应提供其中一个,但不能同时提供。

forward(input: Any)Any[source]
Parameters

输入 (Union[str, List[str]]) – 要转换为相应ID的输入标签

Return type

联合[整数, 列表[整数]]

截断

class torchtext.transforms.Truncate(max_seq_len: int)[source]

截断输入序列

Parameters

max_seq_len (int) – 输入序列允许的最大长度

Tutorials using Truncate:
forward(input: Any)Any[source]
Parameters

输入 (Union[List[Union[str, int]], List[List[Union[str, int]]]]) – 需要截断的输入序列或序列批次

Returns

截断序列

Return type

联合[List[联合[字符串, 整数]], List[List[联合[字符串, 整数]]]]

AddToken

class torchtext.transforms.AddToken(token: Union[int, str], begin: bool = True)[source]

在序列开头或结尾添加令牌

Parameters
  • 标记 (Union[int, str]) – 要添加的标记

  • 开始 (布尔值, 可选) – 是否在序列开头或结尾插入标记,默认为True

Tutorials using AddToken:
forward(input: Any)Any[source]
Parameters

输入 (Union[List[Union[str, int]], List[List[Union[str, int]]]]) – 输入序列或批处理

顺序

class torchtext.transforms.Sequential(*args: torch.nn.modules.module.Module)[source]
class torchtext.transforms.Sequential(arg: OrderedDict[str, Module])

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

Tutorials using Sequential:
forward(input: Any)Any[source]
Parameters

输入 (Any) – 输入序列或批次。输入类型必须由序列中的第一个转换支持。

文档

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

查看文档

教程

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

查看教程

资源

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

查看资源