torchtext.data.Field

torchtext.data.Field

类接口

class torchtext.data.Field(sequential=True, use_vocab=True, init_token=None, eos_token=None, fix_length=None, dtype=torch.int64, preprocessing=None, postprocessing=None, lower=False, tokenize=None, tokenizer_language='en', include_lengths=False, batch_first=False, pad_token='<pad>', unk_token='<unk>', pad_first=False, truncate_first=False, stop_words=None, is_target=False)

功能

Defines a datatype together with instructions for converting to Tensor.
定义数据类型以及转换为张量的指令

Field class models common text processing datatypes that can be represented by tensors. It holds a Vocab object that defines the set of possible values for elements of the field and their corresponding numerical representations. The Field object also holds other parameters relating to how a datatype should be numericalized, such as a tokenization method and the kind of Tensor that should be produced.
字段类对可用张量表示的通用文本处理数据类型进行建模。它保存一个Vocab对象,该对象定义字段元素的可能值集及其相应的数值表示。Field对象还包含与数据类型应如何数值化有关的其他参数,例如标记化方法和应生成的张量类型。

If a Field is shared between two columns in a dataset (e.g., question and answer in a QA dataset), then they will have a shared vocabulary.
如果一个字段在数据集中的两列之间共享(例如,QA数据集中的问题和答案),那么它们将有一个共享词汇表

参数

  • sequential – Whether the datatype represents sequential data. If False, no tokenization is applied. Default: True.
  • use_vocab – Whether to use a Vocab object. If False, the data in this field should already be numerical. Default: True.
  • init_token – A token that will be prepended to every example using this field, or None for no initial token. Default: None.
  • eos_token – A token that will be appended to every example using this field, or None for no end-of-sentence token. Default: None.
  • fix_length – A fixed length that all examples using this field will be padded to, or None for flexible sequence lengths. Default: None.
  • dtype – The torch.dtype class that represents a batch of examples of this kind of data. Default: torch.long.
  • preprocessing – The Pipeline that will be applied to examples using this field after tokenizing but before numericalizing. Many Datasets replace this attribute with a custom preprocessor. Default: None.
  • postprocessing – A Pipeline that will be applied to examples using this field after numericalizing but before the numbers are turned into a Tensor. The pipeline function takes the batch as a list, and the field’s Vocab. Default: None.
  • lower – Whether to lowercase the text in this field. Default: False.
  • tokenize – The function used to tokenize strings using this field into sequential examples. If “spacy”, the SpaCy tokenizer is used. If a non-serializable function is passed as an argument, the field will not be able to be serialized. Default: string.split.
  • tokenizer_language – The language of the tokenizer to be constructed. Various languages currently supported only in SpaCy.
  • include_lengths – Whether to return a tuple of a padded minibatch and a list containing the lengths of each examples, or just a padded minibatch. Default: False.
  • batch_first – Whether to produce tensors with the batch dimension first. Default: False.
  • pad_token – The string token used as padding. Default: “”.
  • unk_token – The string token used to represent OOV words. Default: “”.
  • pad_first – Do the padding of the sequence at the beginning. Default: False.
  • truncate_first – Do the truncating of the sequence at the beginning. Default: False
  • stop_words – Tokens to discard during the preprocessing step. Default: None
  • is_target – Whether this field is a target variable. Affects iteration over batches. Default: False
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: torchtext.data.fieldtorchtext 库中用来定义数据字段的类。它可以用来指定如何处理数据中的每一个字段,如将其转化为数字索引,进行分词等。可以用来定义文本字段,标签字段等。 ### 回答2: torchtext是一个基于PyTorch的NLP工具包,其中的data模块包含Field类,用于指定文本数据的预处理方式和形式。Field是一个非常重要的工具类,用于定义文本数据的结构、处理方法和对齐方式。 Field类主要用于指定文本数据中的字段,包括文本、标签、字符、词等。通过指定Field包括文本字段的文本内容、长度、词表、词向量等属性来构建数据集。通过使用Field可以实现对于不同文本字段的不同处理,如分词、统一标准,以便能够让模型更好的学习到数据的特征。 在使用torchtext的过程中,需要先定义Field,然后将数据源标准化为一致的格式。Field提供了一系列参数以控制数据的处理方式,比如指定分词器、是否强制逐字对齐、是否将数字转换成特定的标识符等等。在实际应用中,可以定义多个Field以处理多种文本类型的数据,然后将它们组合成Dataset数据集。 内置的Field类,它支持根据数据语言自动匹配处理方式,支持多种标准化方法和分词器。此外,它还支持多种数据类型和特殊字符的处理,如数字、字符、标签、分隔符等等。总之,Field类是torchtext中非常重要的一个类,通过它我们可以方便地处理文本数据,将数据标准化,使不同的数据格式统一化,有助于模型更好的学习数据特征,使NLP任务更加方便快捷。 ### 回答3: torchtext是一个用于处理文本数据的Python库,它提供了许多有用的工具,使得在自然语言处理任务中使用文本数据变得更加容易和高效。其中最常用的是torchtext.data.field模块,该模块定义了对于文本字段的预处理操作。 在使用torchtext.data.field时,需要确定文本数据处理的属性,例如:是文本还是标签,是否需要进行分词,是否需要将文本转化为小写等等。如下三个最常用的属性: 属性一:sequential。该属性指定该字段是否是一个序列,即一条数据是否由多个序列组成。例如,一篇文章可以被看作是一个由单词组成的序列,每个单词都是一个序列的元素。 属性二:use_vocab。该属性指定该字段是否需要建立词表。如果该属性为False,则不需要建立词表,例如一个标签字段,使用one-hot编码即可。如果该属性为True,则需要建立对应的词表,在使用该字段对数据进行预处理时,需要先将文本序列中的每一个单词映射为该词表中的唯一标识符ID。 属性三:tokenize。该属性指定在对该字段中的文本数据进行预处理时所采用的分词函数。分词函数会将文本数据分割成单词序列,这样才能将单词映射为词表中的唯一标识符ID。 在使用torchtext.data.field时,一般需要进行如下几步操作:首先,定义文本字段preprocessing规则,包括是否需要进行分词,分词函数,以及是否要建立词表。其次,对于原始的文本数据集,需要使用torchtext.data.TabularDataset类,采用相应的字段preprocessing规则进行预处理。最后,将预处理后的数据集分割为training set和test set,使用torchtext提供的Iterator类对数据进行批量读取,即可将数据输入到模型中。 总之,torchtext.data.field模块的使用可以极大简化自然语言处理中文本数据预处理工作,并且提供了丰富的属性,使得文本数据处理更具有灵活性和可维护性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值