Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add doc embedding implementation during inference #339

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion neuralcoref/neuralcoref.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ cdef class NeuralCoref(object):
# if debug: print("Build single features and pair features arrays")
# ''' Build single features and pair features arrays '''
doc_c = doc.c
doc_embedding = numpy.zeros(SIZE_EMBEDDING, dtype='float32') # self.embeds.get_average_embedding(doc.c, 0, doc.length + 1, self.hashes.puncts)
doc_embedding = self.get_doc_embedding(doc)
doc_embed = doc_embedding
for i in range(n_mentions):
s_inp_arr[i, :SGNL_FEATS_0] = self.get_mention_embeddings(mentions[i], doc_embedding) # Set embeddings
Expand Down Expand Up @@ -872,6 +872,14 @@ cdef class NeuralCoref(object):
def normalize(self, Token token):
return self.hashes.digit_word if token.is_digit else token.lower

def get_doc_embedding(self, Doc doc):
embed_arr = numpy.zeros(self.static_vectors.shape[1], dtype='float32')
for sent in doc.sents:
utt_embed = self.get_average_embedding(sent)
embed_arr += utt_embed
embed_arr = numpy.divide(embed_arr, float(max(len(list(doc.sents)), 1)))
return embed_arr

def get_static(self, hash_t word):
return self.static_vectors[word] if word in self.static_vectors else self.static_vectors[self.hashes.unknown_word]

Expand Down