I am trying to import:from torchtext.legacy.data import Field, BucketIterator,Iterator,data, but get error 'No module named 'torchtext.legacy'

Question:

I am trying to execute the following code for a nlp proj

import torchtext
from torchtext.legacy.data import Field, BucketIterator, Iterator
from torchtext.legacy import data


----> 6 from torchtext.legacy.data import Field, BucketIterator, Iterator
      7 from torchtext.legacy import data
      8 

ModuleNotFoundError: No module named 'torchtext.legacy'.

I have tried it on both kaggle notebook and jupyter notebook and found the same error in both.
i even tried to install !pip install -qqq deepmatcher==0.1.1 in kaggle to solve the issue but it still gives the same error.
is there any solution to this?

Asked By: Gourab

||

Answers:

torchtext.legacy is only for pytorch1.9+

for those who use lower version of pytorch (e.g. I use the pytorch1.6 + torchtext 0.6), you can revise the "train_ebr.py" script in the "fairseq" folders.

# old 
from torchtext.legacy import data, datasets
from torchtext.legacy.vocab import Vocab

# new
from torchtext import data, datasets
from torchtext.vocab import Vocab
Answered By: tongye

Before you import torchtext.legacy, you need to !pip install torchtext==0.10.0.

Maybe legacy was removed in version 0.11.0.

Answered By: wangzc

Use torchtext version v0.6 and corresponding compatibility version of torch.

!pip install torchtext==0.6 torch==1.5 (In colab) Restart runtime after excuting this line

Then you can use

from torchtext.data import Field, TabularDataset, BucketIterator, Iterator

Answered By: SomaSekhar
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.