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

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?

3 Answers

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

Maybe legacy was removed in version 0.11.0.

1

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

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like