forked from clarkkev/deep-coref
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_properties.py
135 lines (121 loc) · 5.2 KB
/
model_properties.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import util
import directories
import os
from keras.regularizers import l2
from keras.optimizers import RMSprop
class MentionRankingProps:
def __init__(self,
mode='ranking',
# model initialization
load_weights_from=None, weights_file=None,
# neural network architecture
layer_sizes=None, activation='relu', dropout=0.5, freeze_embeddings=False,
# error penalties for ranking objective
FN=0.8, FL=0.5 if directories.CHINESE else 0.4, WL=1.0,
# learning rates
classification_lr=0.002, top_pairs_lr=0.0001, ranking_lr=0.000005,
# which speaker and string-matching features
pair_features=None,
# mention features
use_length=True, use_mention_type=True, use_position=True, use_dep_reln=False,
# distance and genre features
use_distance=True, use_genre=True,
# averaged word embedding features
use_spans=True, use_doc_embedding=True):
if layer_sizes is None:
layer_sizes = [1000, 500, 500]
if pair_features is None:
pair_features=[
# speaker features
"same-speaker",
"antecedent-is-mention-speaker",
"mention-is-antecedent-speaker",
# string-matching features
"relaxed-head-match",
"exact-string-match",
"relaxed-string-match",
]
self.load_weights_from = load_weights_from
self.weights_file = weights_file
self.layer_sizes = layer_sizes
self.activation = activation
self.dropout = dropout
self.freeze_embeddings = freeze_embeddings
self.FN, self.FL, self.WL = FN, FL, WL
self.ranking_lr = ranking_lr
self.top_pairs_lr = top_pairs_lr
self.classification_lr = classification_lr
self.use_length = use_length
self.use_mention_type = use_mention_type
self.use_position = use_position
self.use_dep_reln = use_dep_reln
self.use_distance = use_distance
self.use_genre = use_genre
self.use_spans = use_spans
self.use_doc_embedding = use_doc_embedding
if os.path.exists(directories.MISC + 'pair_feature_names.pkl'):
name_mapping = util.load_pickle(directories.MISC + 'pair_feature_names.pkl')
self.active_pair_features = sorted([name_mapping[f] for f in pair_features])
self.set_mode(mode)
def set_mode(self, mode):
self.mode = mode
self.top_pairs = False
self.ranking = True
self.anaphoricity = True
self.anaphoricity_only = False
self.regularization = 1e-5 if self.top_pairs or self.ranking else 1e-6
if mode == 'ranking':
pass
elif mode == 'ranking_noana':
self.anaphoricity = False
elif mode == 'classification':
self.ranking = False
elif mode == 'top_pairs':
self.ranking = False
self.top_pairs = True
elif mode == 'pairwise':
self.ranking = False
self.anaphoricity = False
elif mode == 'anaphoricity':
self.ranking = False
self.anaphoricity_only = True
else:
raise ValueError("Unkown mode " + mode)
def get_regularizer(self):
return None if self.regularization is None else l2(self.regularization)
def get_optimizer(self):
if self.ranking:
return RMSprop(self.ranking_lr, epsilon=1e-5)
return RMSprop(lr=self.top_pairs_lr if self.top_pairs else self.classification_lr,
epsilon=1e-5)
def write(self, path):
util.write_pickle(self.__dict__, path)
class ClusterRankingProps:
def __init__(self,
# model initialization
load_weights_from=None, weights_file=None, randomize_weights=False,
# network architecture
top_layers=3, learnable_layers=3, pooling='maxavg', risk_objective=True,
# dropout and learning rates
input_dropout=0, dropout=0.0, learning_rate=1e-7):
assert pooling in ['max', 'avg', 'maxavg']
self.load_weights_from = load_weights_from
self.weights_file = weights_file
self.randomize_weights = randomize_weights
self.top_layers = top_layers
self.learnable_layers = learnable_layers
self.pooling = pooling
self.risk_objective = risk_objective
self.input_dropout = input_dropout
self.dropout = dropout
self.learning_rate = learning_rate
self.single_size = 855 if directories.CHINESE else 674
self.pair_size = 1733 if directories.CHINESE else 1370
self.static_layers = top_layers - learnable_layers
if self.static_layers == 0:
self.anaphoricity_input_size = self.single_size
self.pair_input_size = self.pair_size
elif self.static_layers == 1:
self.anaphoricity_input_size = self.pair_input_size = 1000
else:
self.anaphoricity_input_size = self.pair_input_size = 500