Uros Pocek
Uros Pocek

Reputation: 412

'Vocab' object has no attribute 'itos'

I am building ML models for NLP with pytorch, but as I define vocabulary for tekenized words in my text with "vacab" and try to use vocab.itos I get: 'Vocab' object has no attribute 'itos' error.

This is my vocab:

vocab = torchtext.vocab.vocab(counter, min_freq=1)

How can I solve this problem?

Upvotes: 2

Views: 4340

Answers (2)

The attributes or methods change, depending on the dataset (actually the class) you are using. I also tried to get 'itos' from my d2l.torch.Vocab object, but it did not work. I check the attributes and methods of the instance with dir.

data = d2l.MTFraEng(batch_size=128)
source_data = data.src_vocab
print(dir(source_data)

Among the responses, there is an attribute implemented called idx_to_token. So I only acceded to it.

print(source_data.idx_to_token)

And get the data :)

['!',',','.','','','','?','a','agree', ...,'works','write','you']

Upvotes: 0

Ivan
Ivan

Reputation: 40618

You should access torchtext.vocab.Vocab.get_itos to get the indices->tokens mapping.

>>> itos = vocab.get_itos()

Upvotes: 6

Related Questions