属性错误:模块“torchtext.data”没有“Field”属性。

31

我想运行一个使用pytorch和torchtext的git项目,但当我运行时,它报错了:

  File "main.py", line 60, in <module>
    main()
  File "main.py", line 50, in main
    train_iters, dev_iters, test_iters, vocab = load_dataset(config)
  File "/home/esmailza/style transfer/style-transformer/data.py", line 23, in load_dataset
    TEXT = data.Field(batch_first=True, eos_token='<eos>')
AttributeError: module 'torchtext.data' has no attribute 'Field'

PyTorch版本 = 1.8.0 torchtext版本 = 0.9


def load_dataset(config, train_pos='train.pos', train_neg='train.neg',
                 dev_pos='dev.pos', dev_neg='dev.neg',
                 test_pos='test.pos', test_neg='test.neg'):

    root = config.data_path
    TEXT = data.Field(batch_first=True, eos_token='<eos>')
    
    dataset_fn = lambda name: data.TabularDataset(
        path=root + name,
        format='tsv',
        fields=[('text', TEXT)]
    )

这是BucketIterator中的问题吗? - OctopuSS7
@OctopuSS7 是的,实际上当他定义Field时就会出现错误,但是这个field将在BucketIterator中使用。 - abbas hoseini
3个回答

47

从 TorchText 0.9.0 发布说明

torchtext.data.Field -> torchtext.legacy.data.Field
这意味着,所有功能仍然可用,但在 torchtext.legacy 而不是 torchtext 中。

torchtext.data.Field 已经被移动到 torchtext.legacy.data.Field

导入方式将会变成这样:

from torchtext.legacy import data

4

感谢 @Rishabh Kumar 的回答,这对我有用!

TorchText 0.9.0 发行说明

基于 v0.9 发布版本 https://github.com/pytorch/text/releases/tag/v0.9.0-rc5

现有的遗留代码用户将会遇到兼容性问题,因为我们已经弃用了旧版代码 (#1172, #1181, #1183)。旧版组件被放在 torchtext.legacy.data 文件夹中,如下所示:

torchtext.data.Pipeline -> torchtext.legacy.data.Pipeline
torchtext.data.Batch -> torchtext.legacy.data.Batch
torchtext.data.Example -> torchtext.legacy.data.Example
torchtext.data.Field -> torchtext.legacy.data.Field
torchtext.data.Iterator -> torchtext.legacy.data.Iterator
torchtext.data.Dataset -> torchtext.legacy.data.Dataset

这意味着所有功能仍然可用,但在torchtext.legacy而不是torchtext中。

1
使用版本为0.6的torchtext库。
pip install torchtext==0.6.0 并且重新启动你的内核。

谢谢,这个版本支持torchtext.data.Field。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接