text icon indicating copy to clipboard operation
text copied to clipboard

ImportError: cannot import name 'Field' from 'torchtext.data'

Open MrMoe830 opened this issue 2 years ago • 2 comments

❓ Questions and Help

Description I'm using pytorch2.0.0, the version of torchtext is 0.15.2, when I import "Field" and "BucketIterator" in the code(from torchtext.data import Field, BucketIterator), I got an error from this sentence: ImportError: cannot import name 'Field' from ' torchtext.data' (D:\ML_Pytorch\venv\lib\site-packages\torchtext\data\__init__.py)

May I ask where did the Field go? ? If Field disappears, is there any other similar functionality that can be imported?

MrMoe830 avatar Jun 19 '23 11:06 MrMoe830

It seems like they were deprecated in favour of torch text datasets and vocab classes, which have a much simpler API.

Now you can do something like:

tokenizer = get_tokenizer('spacy', language='en')

# Function to yield list of tokens
def yield_tokens(data_iter: Iterable) -> List[str]:
    for text, _ in data_iter:
        yield tokenizer(text)

# Load dataset
train_iter, val_iter, test_iter = Multi30k()

# Build the vocab. <unk> is special token
vocab = build_vocab_from_iterator(yield_tokens(train_iter), specials=["<unk>"])
vocab.set_default_index(vocab["<unk>"])

# Data as tensors
text_pipeline = lambda x: vocab(tokenizer(x))
label_pipeline = lambda x: int(x) - 1

You should still be able to import Field using from torchtext.legacy.data import Field as well

ankitbatra22 avatar Jun 27 '23 16:06 ankitbatra22

@MrMoe830 Field is deprecated. You can check one of my repo for the updated version of torchtext. Link: https://github.com/Kousik-Sasmal/experiment-with-pytorch-torchtext

Kousik-Sasmal avatar Aug 20 '23 06:08 Kousik-Sasmal