Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: module 'torchtext.data' has no attribute 'Field'

Tags:

python

pytorch

I want to run a git project used pytorch and torchtext but when I run it, it raise error:

  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'

torch version = 1.8.0 torchtext version = 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)]
    )
like image 326
abbas hoseini Avatar asked Mar 07 '21 12:03

abbas hoseini


2 Answers

From TorchText 0.9.0 Release Notes

torchtext.data.Field -> torchtext.legacy.data.Field
This means, all features are still available, but within torchtext.legacy instead of torchtext.

torchtext.data.Field has been moved to torchtext.legacy.data.Field

And the imports would change this way:

from torchtext.legacy import data
like image 126
Rishabh Kumar Avatar answered Oct 22 '22 14:10

Rishabh Kumar


Thanks, @Rishabh Kumar answer as well, this works for me!

From TorchText 0.9.0 Release Notes

Based on v0.9 release https://github.com/pytorch/text/releases/tag/v0.9.0-rc5

The current users of the legacy code will experience BC breakage as we have retired the legacy code (#1172, #1181, #1183). The legacy components are placed in torchtext.legacy.data folder as follows:

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

This means, all features are still available, but within torchtext.legacy instead of torchtext.

like image 4
jimcurrywang Avatar answered Oct 22 '22 14:10

jimcurrywang