-
Notifications
You must be signed in to change notification settings - Fork 0
/
emlens_semaxis.py
193 lines (152 loc) · 7.16 KB
/
emlens_semaxis.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
181
182
183
184
185
186
187
188
189
190
191
192
193
import json
import os
import shutil
import numpy as np
from scipy import sparse
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
# Obtained from https://github.com/skojaku/emlens/blob/main/emlens/semaxis.py
class SemAxis:
"""SemAxis Class object.
SemAxis aims to find an interpretable axis in the emebeding spacing using acronym entity groups.
The axis is placed such that it runs through the centeroid of two acronym entity groups, and then all entities are projected to the axis.
Reference:
* [1] An, J., Kwak, H., & Ahn, Y.-Y. (2018). SemAxis: A Lightweight Framework to Characterize Domain-Specific Word Semantics Beyond Sentiment. Proc. the 56th Annual Meeting of the Association for Computational Linguistics, 1, 2450–2461.
.. highlight:: python
.. code-block:: python
>>> import emlens
>>> import numpy as np
>>> emb = np.random.randn(100, 20) # Embedding vectors to ground the SemAxis
>>> group_ids = np.random.choice(2, 100) # Group membership of entities
>>> target = np.random.randn(10, 20) # Vectors we will project onto the SemAxis
>>> model = emlens.SemAxis() # load SemAxis Object
>>> model.fit(emb, group_ids) # Fit the SemAxis
>>> model.transform(target) # Project `target` to the axis
>>> model.save("random-semaxis.sm")
"""
def __init__(self):
self.emb = None
self.prj_emb = None
self.semaxis = None
# private
self._group_ids = None # int ids of group membership
def fit(self, emb, group_ids, group_order=None):
"""Finding the SemAxis from embedding vectors.
:param emb: embedding vectors for locating the SemAxis
:type emb: numpy.ndarray (num_entities, dim)
:param group_ids: group_ids, defaults to None.
:type group_ids: numpy.ndarray (num_entities, dim)
:param group_order: The axis points from group_order[0] to group_order[1]
:type group_order: list, optional
"""
if group_order is None:
self.group_order, self._group_ids = np.unique(
group_ids, return_inverse=True
)
self.group_order = self.group_order.tolist()
self.n_group = len(self.group_order)
else:
self.group_order = group_order
group2cids = {ll: i for i, ll in enumerate(self.group_order)}
self._group_ids = np.array([group2cids[ll] for ll in group_ids])
self.n_group = len(group2cids)
self.emb = emb
return self
def transform(self, target):
"""Project the target vectors onto SemAxis.
:param target: target embedding vectors to project onto the SemAxis.
:type target: numpy.ndarray (num_target, dim)
:return: Projected embedding vectors.
:rtype: numpy.ndarray (num_data,)
"""
left_center = np.mean(self.emb[self._group_ids == 0, :], axis=0)
right_center = np.mean(self.emb[self._group_ids == 1, :], axis=0)
semaxis = right_center - left_center
denom = np.linalg.norm(target, axis=1) * np.linalg.norm(semaxis)
denom = 1 / np.maximum(denom, 1e-20)
prj_target = sparse.diags(denom) @ (target @ semaxis.T)
denom = np.linalg.norm(self.emb, axis=1) * np.linalg.norm(semaxis)
denom = 1 / np.maximum(denom, 1e-20)
self.prj_emb = sparse.diags(denom) @ (self.emb @ semaxis.T)
self.semaxis = semaxis
return prj_target
def save(self, filename):
"""Save the fitted axis.
:param filename: name of file
:type filename: str
.. highlight:: python
.. code-block:: python
>>> import emlens
>>> import numpy as np
>>> emb = np.random.randn(100, 20)
>>> group_ids = np.random.choice(2, 100)
>>> model = emlens.SemAxis().fit(emb, group_ids)
>>> model.save('semspace.sm')
"""
if os.path.exists(filename):
shutil.rmtree(filename)
os.mkdir(filename)
emb_filename = "{dir_name}/emb.npz".format(dir_name=filename)
param_filename = "{dir_name}/param.json".format(dir_name=filename)
np.savez(
emb_filename, emb=self.emb, _group_ids=self._group_ids, semaxis=self.semaxis
)
params = {"n_group": self.n_group, "group_order": self.group_order}
with open(param_filename, "w") as f:
json.dump(params, f)
def load(self, filename):
"""Load SemAxis file.
:param filename: filename
:type filename: str
.. highlight:: python
.. code-block:: python
>>> import emlens
>>> xy = emlens.SemAxis().load('semspace.sm')
"""
emb_filename = "{dir_name}/emb.npz".format(dir_name=filename)
param_filename = "{dir_name}/param.json".format(dir_name=filename)
data = np.load(emb_filename, allow_pickle=True)
self.emb = data["emb"]
self.semaxis = data["semaxis"]
self._group_ids = data["_group_ids"]
with open(param_filename, "r") as f:
params = json.load(f)
for k, v in params.items():
setattr(self, k, v)
return self
class LDASemAxis(SemAxis):
"""SemAxis based on Linear Discriminant Analysis.
A variant of SemAxis that finds the axis based on Linear Discriminant Analysis (LDA). This LDA-based SemAxis separates the given two groups more than the original SemAxis approach. The LDA-based SemAxis can find a "space" that best separates the groups.
See https://en.wikipedia.org/wiki/Linear_discriminant_analysis.
.. highlight:: python
.. code-block:: python
>>> import emlens
>>> import numpy as np
>>> emb = np.random.randn(100, 20) # Embedding vectors to ground the SemAxis
>>> group_ids = np.random.choice(2, 100) # Membership of entities
>>> target = np.random.randn(10, 20) # Vectors we will project onto the SemAxis
>>> model = emlens.LDASemAxis() # load SemAxis Object
>>> model.fit(emb, group_ids) # Fit the SemAxis
>>> model.transform(target, dim = 1) # Project `target` to the axis
>>> model.transform(target, dim = 2) # Project `target` to a 2D space
>>> model.save("random-semaxis.sm") # Save fitted SemAxis object
>>> model = emlens.LDASemAxis().load("random-semaxis.sm") # Load fitted SemAxis object
"""
def __init__(self, **params):
"""Initialize the instnace of SemAxis based on Linear Discriminant
Analysis.
:param mode: type of algorithm, defaults to "fda"
:type mode: str, optional
"""
SemAxis.__init__(self, **params)
def transform(self, target, dim=1, **params):
"""Project the target vectors onto SemAxis.
:param dim: dimension for the projected space, defaults to 1
:type dim: int, optional
:return: Projected embedding vectors.
:rtype: numpy.ndarray (num_data,dim)
"""
lda = LinearDiscriminantAnalysis(n_components=dim, **params)
lda.fit(self.emb, self._group_ids)
prj_target = lda.transform(target)
self.prj_emb = lda.transform(self.emb)
return prj_target