Reputation: 412
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
Reputation: 1
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
Reputation: 40618
You should access torchtext.vocab.Vocab.get_itos
to get the indices->tokens mapping.
>>> itos = vocab.get_itos()
Upvotes: 6