-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanalogy.py
292 lines (264 loc) · 10.9 KB
/
analogy.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
"""
Vector implementation of analogy-making
"""
from __future__ import print_function
import sys
import pickle
from pathlib import Path
import tensorflow.compat.v1 as tf
import tensorflow.keras as K
import numpy as np
from scipy.special import factorial
class VectorAnalogy:
"""
Vector analogy
"""
def __construct_recode_mat_aux(self, slot_no, state):
"""
Auxiliary function
"""
if slot_no >= self._n_slots:
yield state
for slot_i in [x for x in range(self._n_slots) if not x in state]:
state_copy = np.copy(state)
state_copy[slot_no] = slot_i
for state_t in self.__construct_recode_mat_aux(
slot_no + 1,
state_copy):
yield state_t
def __construct_recode_mat(self):
"""
Construct a new recoding matrix
"""
diag_mat = np.diagflat(np.ones(shape=(self._n_slots)))
recode_mat = np.zeros(
shape=(self._n_states, self._n_slots, self._n_slots),
dtype=np.float32)
state = np.full(shape=self._n_slots, fill_value=-1, dtype=np.int)
i = 0
for positions in self.__construct_recode_mat_aux(0, state):
recode_mat[i] = diag_mat[:][positions]
i += 1
return recode_mat
def __build_graph(self):
"""
Build TF graph for analogy-making
"""
self.__sem_target = tf.placeholder(
shape=[self._n_slots, self._sem_dim],
dtype=tf.float32)
self.__struct_target = tf.placeholder(
shape=[self._max_arity, self._n_slots, self._n_slots],
dtype=tf.float32)
self.__sem_base = tf.placeholder(\
shape=[None, self._n_slots, self._sem_dim],
dtype=tf.float32)
self.__struct_base = tf.placeholder(
shape=[None, self._max_arity, self._n_slots, self._n_slots],
dtype=tf.float32)
#Construct recoding matrix
if Path("recode/recode_mat.{}.pickle".format(self._n_slots)).is_file():
#a recoding matrix with given parameters is already
# created and serialized, load it
print("Loading recoding matix...", end="")
sys.stdout.flush()
with open(
"recode/recode_mat.{}.pickle".format(self._n_slots),
"rb") as file_h:
recode_mat = tf.constant(pickle.load(file_h))
print("Done.")
sys.stdout.flush()
else:
#create recoding matrix and serialize it to a file
recode_mat = tf.constant(self.__construct_recode_mat())
with open(
"recode/recode_mat.{}.pickle".format(self._n_slots),
"wb") as file_h:
with tf.Session():
pickle.dump(recode_mat.eval(), file_h)
#generate all possible states of the semantics of the target
sem_targets = tf.reshape(
tf.matmul(
tf.reshape(
recode_mat,
[self._n_states * self._n_slots,self._n_slots]),
self.__sem_target),
[1, self._n_states, self._n_slots * self._sem_dim])
#generate all possible states of the structure of the target
struct_targets = tf.reshape(
tf.transpose(
tf.reshape(
tf.concat(
[
tf.matmul(
tf.reshape(
tf.matmul(
tf.reshape(
recode_mat,
[
self._n_states * self._n_slots,
self._n_slots
]),
self.__struct_target[a_i],
),
[
self._n_states,
self._n_slots,
self._n_slots
]),
tf.transpose(recode_mat, [0, 2, 1]),
)
for a_i in range(self._max_arity)
], 0
),
[
self._max_arity,
self._n_states,
self._n_slots * self._n_slots
]),
[1, 0, 2]),
[
1,
self._n_states,
self._max_arity * self._n_slots * self._n_slots
])
#computer number of bases
n_bases = tf.shape(self.__sem_base)[0]
#reshapoe bases
sem_base = tf.reshape(
tf.tile(
tf.reshape(
self.__sem_base,
[n_bases, self._n_slots * self._sem_dim]),
[1, self._n_states]),
[n_bases, self._n_states, self._n_slots * self._sem_dim])
struct_base = tf.reshape(
tf.tile(
tf.reshape(
self.__struct_base,
[
n_bases,
self._max_arity * self._n_slots * self._n_slots
]),
[1, self._n_states]),
[
n_bases,
self._n_states,
self._max_arity * self._n_slots * self._n_slots
])
#compute semantics denominator for cosine similarity
denom_sem = tf.multiply(
tf.sqrt(
tf.reduce_sum(
tf.multiply(sem_targets, sem_targets),
axis=[2])),
tf.sqrt(
tf.reduce_sum(
tf.multiply(sem_base, sem_base),
axis=[2])))
#compute numerator
num_sem = tf.reduce_sum(tf.multiply(sem_targets, sem_base), axis=[2])
#compute cosine similarity
sem_cos = -K.losses.cosine_similarity(sem_targets, sem_base, axis=[2])
#tf.add(tf.multiply(tf.divide(num_sem, denom_sem), 0.5), 0.5)
print(sem_cos)
#compute structure denominator for cosine similarity
denom_struct = tf.multiply(
tf.sqrt(
tf.reduce_sum(
tf.multiply(struct_targets, struct_targets),
axis=[2])),
tf.sqrt(
tf.reduce_sum(
tf.multiply(struct_base, struct_base),
axis=[2])))
#compute numerator
num_struct = tf.reduce_sum(
tf.multiply(struct_targets, struct_base), axis=[2])
#compute cosine similarity
struct_cos = -K.losses.cosine_similarity(struct_targets, struct_base, axis=[2])
#tf.divide(num_struct, denom_struct)
similarities = tf.add(
tf.multiply(sem_cos, 1 - self._sigma),
tf.multiply(struct_cos, self._sigma))
self._sem_cos = sem_cos
self._struct_cos = struct_cos
#get maximum similarity
base_max_similarities = tf.reduce_max(similarities, axis=[1])
#get index of base with max similarity
self.__best_base_index = tf.argmax(base_max_similarities)
#get the index of the recoding which lead to the max similarity
best_recoding_no = \
tf.argmax(
tf.reshape(
tf.slice(
similarities,
[self.__best_base_index, 0], [1, self._n_states]),
[self._n_states]))
#maximum similarity value
self.__best_base_similarity = tf.reduce_max(base_max_similarities)
#best recoding
self.__best_recoding = tf.slice(
recode_mat,
[best_recoding_no, 0, 0],
[1, self._n_slots, self._n_slots])
#best recoding of semantics
self.__best_target_sem_recoding = tf.reshape(
tf.slice(
sem_targets,
[0, best_recoding_no, 0],
[1, 1, self._n_slots * self._sem_dim]),
[self._n_slots, self._sem_dim])
#best recoding of structure
self.__best_target_struct_recoding = tf.reshape(
tf.slice(
struct_targets,
[0, best_recoding_no, 0],
[1, 1, self._max_arity * self._n_slots * self._n_slots]
),
[self._max_arity, self._n_slots, self._n_slots]
)
def make(self, target, bases):
"""
Returns the predicate type id which corresponds to the given semantic vector.
Use to recover the predicate type from a vector representation
"""
if not type(bases) is list:
sem_bases = [bases[0]]
struct_bases = [bases[1]]
else:
sem_bases = np.zeros(
shape=(len(bases), self._n_slots, self._sem_dim),
dtype=np.float32)
struct_bases = np.zeros(
shape=(len(bases),
self._max_arity, self._n_slots, self._n_slots),
dtype=np.float32)
for i in range(len(bases)):
sem_bases[i] = bases[i][0]
struct_bases[i] = bases[i][1]
with tf.Session() as session:
results = session.run(
[
self.__best_base_similarity,
self.__best_base_index,
self.__best_recoding,
self._sem_cos,
self._struct_cos
],
feed_dict={
self.__sem_target: target[0],
self.__struct_target: target[1],
self.__sem_base: sem_bases,
self.__struct_base: struct_bases
})
return (results[0], results[1], results[2])
def __init__(self, sigma, n_slots, max_arity, sem_dim):
self._sigma = sigma
self._n_slots = n_slots
self._max_arity = max_arity
self._sem_dim = sem_dim
#number of possible states of the representations
self._n_states = int(factorial(n_slots))
tf.disable_eager_execution()
self.__build_graph()