'unk_init' of Vocab class doesn't work
Hi, I'm trying to load pretrained embeddings for my vocabulary.
The Vocab class has an unk_init argument which enables us initialize out-of-vocabulary word vectors with a callback function like torch.Tensor.zero_. However, I found that it didn't work as I expected. Here is an example:
# Default
vectors = GloVe(name='twitter.27B', dim='25')
c = Counter({'hello': 4, 'world': 3, 'ᑌᑎIᑕOᗪᕮ_Tᕮ᙭T': 5, 'freq_too_low': 2})
v = Vocab(c, min_freq=3, specials=['<unk>', '<pad>', '<bos>'],
vectors=vectors)
for w, vec in zip(v.stoi.keys(), v.vectors):
print(w, vec[:5].view(1,-1))
and the output:
<unk>
0 0 0 0 0
[torch.FloatTensor of size 1x5]
<pad>
0 0 0 0 0
[torch.FloatTensor of size 1x5]
<bos>
0 0 0 0 0
[torch.FloatTensor of size 1x5]
ᑌᑎIᑕOᗪᕮ_Tᕮ᙭T
0 0 0 0 0
[torch.FloatTensor of size 1x5]
hello
-0.7707 0.1283 0.3314 0.0051 -0.4760
[torch.FloatTensor of size 1x5]
world
0.1030 0.0957 -0.1479 -0.2238 -0.1478
[torch.FloatTensor of size 1x5]
Now I wish to initialize out-of-vocabulary tokens with a normal distribution:
# Default
vectors = GloVe(name='twitter.27B', dim='25')
c = Counter({'hello': 4, 'world': 3, 'ᑌᑎIᑕOᗪᕮ_Tᕮ᙭T': 5, 'freq_too_low': 2})
v = Vocab(c, min_freq=3, specials=['<unk>', '<pad>', '<bos>'],
vectors=vectors, unk_init=torch.Tensor.normal_)
for w, vec in zip(v.stoi.keys(), v.vectors):
print(w, vec[:5].view(1,-1))
And the output:
<unk>
0 0 0 0 0
[torch.FloatTensor of size 1x5]
<pad>
0 0 0 0 0
[torch.FloatTensor of size 1x5]
<bos>
0 0 0 0 0
[torch.FloatTensor of size 1x5]
ᑌᑎIᑕOᗪᕮ_Tᕮ᙭T
0 0 0 0 0
[torch.FloatTensor of size 1x5]
hello
-0.7707 0.1283 0.3314 0.0051 -0.4760
[torch.FloatTensor of size 1x5]
world
0.1030 0.0957 -0.1479 -0.2238 -0.1478
[torch.FloatTensor of size 1x5]
I expected the vector of ᑌᑎIᑕOᗪᕮ_Tᕮ᙭T to be a non-zero vector.
I know how to do it manually, but still I wonder how I can use unk_init correctly.
Thank you!
Your code will work if you pass the unk_init to the GloVe constructor, and not the Vocab one:
vectors = GloVe(name='twitter.27B', dim='25', unk_init=torch.Tensor.normal_)
I guess that if you are manually loading the vectors via a specific class (like GloVe) and then providing them to the Vocab constructor, you should set the callback for the unknown tokens right now and there.
On the other hand, the Vocab constructor also takes the pretrained embeddings' alias (in your case glove.twitter.27B.25d), in which case you should pass the callback there:
vectors = "glove.twitter.27B.25d"
c = Counter({'hello': 4, 'world': 3, 'ᑌᑎIᑕOᗪᕮ_Tᕮ᙭T': 5, 'freq_too_low': 2})
v = Vocab(c, min_freq=3, specials=['<unk>', '<pad>', '<bos>'],
vectors=vectors, unk_init=torch.Tensor.normal_)
I am not entirely sure whether this is intended, though. It seems related to #263 .
@domaala Thanks for your reply! Your code works .
I compared your code and mine
# Mine
vectors = GloVe(name='twitter.27B', dim='25')
c = Counter({'hello': 4, 'world': 3, 'ᑌᑎIᑕOᗪᕮ_Tᕮ᙭T': 5, 'freq_too_low': 2})
v = Vocab(c, min_freq=3, specials=['<unk>', '<pad>', '<bos>'],
vectors=vectors, unk_init=torch.Tensor.normal_)
# Yours
vectors = "glove.twitter.27B.25d"
c = Counter({'hello': 4, 'world': 3, 'ᑌᑎIᑕOᗪᕮ_Tᕮ᙭T': 5, 'freq_too_low': 2})
v = Vocab(c, min_freq=3, specials=['<unk>', '<pad>', '<bos>'],
vectors=vectors, unk_init=torch.Tensor.normal_)
It seems that the only difference is that you specify the pretrained vectors by a string and I did it with the GloVe class. It's quite strange to me because I would expect them to do the same thing.
You could just move the unk_init to the GloVe constructor as well if you like that one better (as mentioned above). But yes, it is maybe a bit counter-intuitive.
Thank you!