-
Notifications
You must be signed in to change notification settings - Fork 2
/
pbos.py
180 lines (159 loc) · 6.21 KB
/
pbos.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import logging
from collections import Counter, defaultdict
from functools import lru_cache, partial
import numpy as np
from utils import normalize_prob
logger = logging.getLogger(__name__)
def get_subword_prob(sub, subword_prob, eps=None, take_root=False):
prob = subword_prob.get(sub, eps if len(sub) == 1 else 0)
if take_root:
prob = prob ** (1 / len(sub))
return prob
def calc_prefix_prob(w, get_subword_prob, backward=False):
w = w[::-1] if backward else w
p = [1]
for i in range(1, len(w) + 1):
p.append(sum(
p[j] * get_subword_prob(w[j:i][::-1] if backward else w[j:i])
for j in range(i)))
return p[::-1] if backward else p
def calc_subword_weights(
w,
*,
subword_vocab,
get_subword_prob=None,
weight_threshold=None,
):
subword_weights = {}
if get_subword_prob:
p_prefix = calc_prefix_prob(w, get_subword_prob)
p_suffix = calc_prefix_prob(w, get_subword_prob, backward=True)
for j in range(1, len(w) + 1):
for i in range(j):
sub = w[i:j]
if sub in subword_vocab:
p_sub = get_subword_prob(sub) * p_prefix[i] * p_suffix[j]
subword_weights.setdefault(sub, 0)
subword_weights[sub] += p_sub
subword_weights = normalize_prob(subword_weights)
if weight_threshold:
subword_weights = {k : v for k, v in subword_weights.items() if v > weight_threshold}
else:
for j in range(1, len(w) + 1):
for i in range(j):
sub = w[i:j]
if sub in subword_vocab:
subword_weights.setdefault(sub, 0)
subword_weights[sub] += 1
subword_weights = normalize_prob(subword_weights)
if len(subword_weights) == 0:
logger.warning(f"no qualified subwords for '{w}'")
return {}
return subword_weights
class PBoS:
def __init__(
self,
subword_embedding=None,
*,
subword_vocab,
embedding_dim=None,
subword_prob=None,
weight_threshold=None,
eps=1e-2,
take_root=False,
normalize_semb=False,
):
"""
Params:
subword_embedding (default: None) - existing subword embeddings.
If None, initialize an empty set of embeddings.
embedding_dim (default: None) - embedding dimensions.
If None, infer from `subword_embedding`.
subword_prob (default: None) - subword probabilities.
Used by probabilistic segmentation to calculate subword weights.
If None, assume uniform probability, i.e. = BoS.
subword_vocab - subword vocabulary.
The set of subwords to maintain subword embeddings.
OOV subwords will be regarded as having zero vector embedding.
weight_threshold (default: None) - minimum subword weight to consider.
Extremely low-weighted subword will be discarded for effiency.
If None, consider subwords with any weights.
eps (default: 1e-2) - the default subword probability if it is not
present in `subword_prob`. This is needed to keep the segmenation
graph connected.
Only effective when `subword_prob` is present.
take_root (default: False) - whether take `** ( 1 / len(sub))` when
getting subword prob.
"""
self.semb = subword_embedding or defaultdict(float)
if embedding_dim is None:
subword_embedding_entry = next(iter(subword_embedding.values()))
embedding_dim = len(subword_embedding_entry)
for w in '<>':
if w in subword_vocab:
del subword_vocab[w]
self._calc_subword_weights = lru_cache(maxsize=32)(partial(
calc_subword_weights,
subword_vocab=subword_vocab,
get_subword_prob=partial(
get_subword_prob,
subword_prob=subword_prob,
eps=eps,
take_root=take_root,
) if subword_prob else None,
weight_threshold=weight_threshold,
))
self.config = dict(
embedding_dim=embedding_dim,
weight_threshold=weight_threshold,
eps=eps,
take_root=take_root,
subword_vocab=subword_vocab,
subword_prob=subword_prob,
normalize_semb=normalize_semb,
)
self._zero_emb = np.zeros(self.config['embedding_dim'])
def dump(self, filename) :
import json, pickle
with open(filename + '.config.json', 'w') as fout:
json.dump(self.config, fout)
with open(filename, 'bw') as bfout :
pickle.dump(self.semb, bfout)
@classmethod
def load(cls, filename) :
import json, pickle
try:
# backward compatibility
with open(filename, 'rb') as bfin:
config, semb = pickle.load(bfin)
except ValueError:
with open(filename, 'rb') as bfin:
semb = pickle.load(bfin)
with open(filename + '.config.json') as fin:
config = json.load(fin)
bos = cls(**config)
bos.semb = semb
return bos
@staticmethod
def _semb_normalized_contrib(w, emb):
norm = np.linalg.norm(emb)
return w * emb / norm if norm > 1e-4 else 0
def embed(self, w):
subword_weights = self._calc_subword_weights(w)
logger.debug(Counter(subword_weights).most_common())
# Will we have performance issue if we put the if check inside sum?
if self.config['normalize_semb']:
wemb = sum(
self._semb_normalized_contrib(w, self.semb[sub])
for sub, w in subword_weights.items()
)
else:
wemb = sum(
w * self.semb[sub]
for sub, w in subword_weights.items()
)
return wemb if isinstance(wemb, np.ndarray) else self._zero_emb
def step(self, w, d):
subword_weights = self._calc_subword_weights(w)
for sub, weight in subword_weights.items():
self.semb[sub] += weight * d