AttributeError: module 'torchtext' has no attribute 'legacy'

7k Views Asked by At

I am trying to use torchtext to process test data, however, I get the error: "AttributeError: module 'torchtext' has no attribute 'legacy'", when I run the following code. Can anyone please guide me what the issue here? I am using python 3.10.4. Thanks

import pandas as pd
import torch
import torchtext
import spacy


def prep_data(file_path):

    TEXT=torchtext.legacy.data.Field(tokenize='spacy', tokenizer_language='en_core_web_sm')
    LABEL=torchtext.legacy.data.LabelField(dtype=torch.long)

    fields=[('clean_text', TEXT), ('label',LABEL)]
    dataset = torchtext.legacy.data.TabularDataset(
    path=file_path, format='csv',
    skip_header=True, fields=fields)

    print(dataset.examples[0])


   if __name__=="__main__":
       train_path='./data/train.csv'
       test_path='./data/test.csv'
       prep_data(train_path)
2

There are 2 best solutions below

1
On

I addressed the same issue by updating the torchtext.

pip install torchtext==0.9

0
On

I also had the same issue. I solved my problem by using a pytorch stable version You are probably using versions 0.10, and 0.11. These were the versions using legacy. Please update to the latest versions 0.13 and 0.14.

pip install torchtext==<version>