Skip to content

Instantly share code, notes, and snippets.

@oborchers
Created June 8, 2019 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oborchers/2782a623e89c95b7a29ca69e9b653727 to your computer and use it in GitHub Desktop.
Save oborchers/2782a623e89c95b7a29ca69e9b653727 to your computer and use it in GitHub Desktop.
def sif_embeddings(sentences, model):
cdef int size = model.vector_size
cdef float[:,:] vectors = model.wv.sif_vectors
cdef int sentence_index, word_index, d, count = 0
cdef float inv = 1.
np_sum = np.sum
output = np.zeros((len(sentences), size), dtype=np.float32)
cdef float[:,:] sv = output
for sentence_index, sentence in enumerate(sentences):
if len(sentence) > 0:
count = 0
for word_index in sentence:
count += 1
for d in range(size):
sv[sentence_index, d] += vectors[word_index, d]
inv = (1./ <float>count)
for d in range(size):
sv[sentence_index, d] *= inv
return output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment