5

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)
aab
  • 10,858
  • 22
  • 38
Emrul
  • 51
  • 1
  • 4

2 Answers2

0

I addressed the same issue by updating the torchtext.

pip install torchtext==0.9

Tigers
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 26 '22 at 12:55
0

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>
Ndaruga
  • 11
  • 2