@@ -27,27 +27,20 @@ class Model:
27
27
Coreference neural model
28
28
'''
29
29
def __init__ (self , model_path ):
30
- self .antecedent_matrix = np .load (model_path + "antecedent_matrix.npy" )
31
- self .anaphor_matrix = np .load (model_path + "anaphor_matrix.npy" )
32
- self .pair_features_matrix = np .load (model_path + "pair_features_matrix.npy" )
33
- self .pairwise_first_layer_bias = np .load (model_path + "pairwise_first_layer_bias.npy" )
34
- self .anaphoricity_model = []
35
- weights = []
36
- biases = []
30
+ weights , biases = [], []
37
31
for file in sorted (os .listdir (model_path )):
38
- if file .startswith ("anaphoricity_model_weights " ):
32
+ if file .startswith ("single_mention_weights " ):
39
33
weights .append (np .load (os .path .join (model_path , file )))
40
- if file .startswith ("anaphoricity_model_bias " ):
34
+ if file .startswith ("single_mention_bias " ):
41
35
biases .append (np .load (os .path .join (model_path , file )))
42
- self .anaphoricity_model = list (zip (weights , biases ))
43
- weights = []
44
- biases = []
36
+ self .single_mention_model = list (zip (weights , biases ))
37
+ weights , biases = [], []
45
38
for file in sorted (os .listdir (model_path )):
46
- if file .startswith ("pairwise_model_weights " ):
39
+ if file .startswith ("pair_mentions_weights " ):
47
40
weights .append (np .load (os .path .join (model_path , file )))
48
- if file .startswith ("pairwise_model_bias " ):
41
+ if file .startswith ("pair_mentions_bias " ):
49
42
biases .append (np .load (os .path .join (model_path , file )))
50
- self .pairwise_model = list (zip (weights , biases ))
43
+ self .pair_mentions_model = list (zip (weights , biases ))
51
44
52
45
def _score (self , features , layers ):
53
46
for weights , bias in layers :
@@ -56,18 +49,16 @@ def _score(self, features, layers):
56
49
features = np .maximum (features , 0 ) # ReLU
57
50
return np .sum (features )
58
51
59
- def get_anaphoricity_score (self , mention_embedding , anaphoricity_features ):
60
- ''' Anaphoricity score for an anaphor '''
61
- first_layer_output = np . concatenate ([ mention_embedding , anaphoricity_features ], axis = 0 )[:, np .newaxis ]
62
- return self ._score (first_layer_output , self .anaphoricity_model )
52
+ def get_single_mention_score (self , mention_embedding , anaphoricity_features ):
53
+ first_layer_input = np . concatenate ([ mention_embedding ,
54
+ anaphoricity_features ], axis = 0 )[:, np .newaxis ]
55
+ return self ._score (first_layer_input , self .single_mention_model )
63
56
64
- def get_pairwise_score (self , antecedent , mention , pair_features ):
65
- antecedent_embedding = np .matmul (self .antecedent_matrix , antecedent .embedding )
66
- anaphor_embedding = np .matmul (self .anaphor_matrix , mention .embedding )
67
- first_layer_output = antecedent_embedding + anaphor_embedding \
68
- + np .matmul (self .pair_features_matrix , pair_features ) + self .pairwise_first_layer_bias
69
- first_layer_output = np .maximum (first_layer_output , 0 )[:, np .newaxis ] # ReLU
70
- return self ._score (first_layer_output , self .pairwise_model )
57
+ def get_pair_mentions_score (self , antecedent , mention , pair_features ):
58
+ first_layer_input = np .concatenate ([antecedent .embedding ,
59
+ mention .embedding ,
60
+ pair_features ], axis = 0 )[:, np .newaxis ]
61
+ return self ._score (first_layer_input , self .pair_mentions_model )
71
62
72
63
73
64
class Algorithm :
@@ -159,15 +150,15 @@ def run_coref_on_mentions(self, mentions):
159
150
for mention_idx , ant_list in self .data .get_candidate_pairs (mentions , self .max_dist , self .max_dist_match ):
160
151
mention = self .data [mention_idx ]
161
152
feats_ , ana_feats = self .data .get_anaphoricity_features (mention )
162
- anaphoricity_score = self .coref_model .get_anaphoricity_score (mention .embedding , ana_feats )
153
+ anaphoricity_score = self .coref_model .get_single_mention_score (mention .embedding , ana_feats )
163
154
self .mentions_single_scores [mention_idx ] = anaphoricity_score
164
155
self .mentions_single_features [mention_idx ] = {"spansEmbeddings" : mention .spans_embeddings_ , "wordsEmbeddings" : mention .words_embeddings_ , "features" : feats_ }
165
156
166
157
best_score = anaphoricity_score - 50 * (self .greedyness - 0.5 )
167
158
for ant_idx in ant_list :
168
159
antecedent = self .data [ant_idx ]
169
160
feats_ , pwf = self .data .get_pair_features (antecedent , mention )
170
- score = self .coref_model .get_pairwise_score (antecedent , mention , pwf )
161
+ score = self .coref_model .get_pair_mentions_score (antecedent , mention , pwf )
171
162
self .mentions_pairs_scores [mention_idx ][ant_idx ] = score
172
163
self .mentions_pairs_features [mention_idx ][ant_idx ] = {"pairFeatures" : feats_ , "antecedentSpansEmbeddings" : antecedent .spans_embeddings_ ,
173
164
"antecedentWordsEmbeddings" : antecedent .words_embeddings_ ,
0 commit comments