Files
  • main.py
  • requirements.txt
main.py
1
2
3
4
5
6
7
8
9
10
11
import mxnet as mx
import gluonnlp as nlp

glove = nlp.embedding.create('glove', source='glove.6B.50d')

def cos_similarity(embedding, word1, word2):
    vec1, vec2 = embedding[word1], embedding[word2]
    return mx.nd.dot(vec1, vec2) / (vec1.norm() * vec2.norm())

print('Similarity between "baby" and "infant": ',
      cos_similarity(glove, 'baby', 'infant').asnumpy()[0])
Python 3.6.1 (default, Dec 2015, 13:05:11) [GCC 4.8.2] on linux