@szha/

gluon-nlp

Python

A simple word similarity example with GluonNLP pre-trained embeddings

fork
loading
Files
  • main.py
  • requirements.txt

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.4019309869036076","path":"main.py","file":{"path":"main.py","content":{"asEncoding":{"base64":"aW1wb3J0IG14bmV0IGFzIG14CmltcG9ydCBnbHVvbm5scCBhcyBubHAKCmdsb3ZlID0gbmxwLmVtYmVkZGluZy5jcmVhdGUoJ2dsb3ZlJywgc291cmNlPSdnbG92ZS42Qi41MGQnKQoKZGVmIGNvc19zaW1pbGFyaXR5KGVtYmVkZGluZywgd29yZDEsIHdvcmQyKToKICAgIHZlYzEsIHZlYzIgPSBlbWJlZGRpbmdbd29yZDFdLCBlbWJlZGRpbmdbd29yZDJdCiAgICByZXR1cm4gbXgubmQuZG90KHZlYzEsIHZlYzIpIC8gKHZlYzEubm9ybSgpICogdmVjMi5ub3JtKCkpCgpwcmludCgnU2ltaWxhcml0eSBiZXR3ZWVuICJiYWJ5IiBhbmQgImluZmFudCI6ICcsIGNvc19zaW1pbGFyaXR5KGdsb3ZlLCAnYmFieScsICdpbmZhbnQnKS5hc251bXB5KClbMF0p"},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
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])