-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpoincare_viz.py
290 lines (250 loc) · 9.56 KB
/
poincare_viz.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import division
import logging
from collections import Counter
import numpy as np
import plotly.graph_objs as go
import plotly.plotly as py
from gensim.models.poincare import PoincareKeyedVectors
def space_title(s):
for cut in [100, 200, 300, 400]:
if len(s) > cut:
x = s.find(';', cut) + 1
if x > 0:
s = s[:x] + '<br>' + s[x:]
return s
logger = logging.getLogger(__name__)
def create_animation(figure_title):
figure = {'data': [],
'layout': {'xaxis': {'range': [-1, 1.3], 'autorange': False, 'zeroline' :False, 'showgrid' :False},
'yaxis': {'range': [-1, 1.3], 'autorange': False, 'zeroline' :False, 'showgrid' :False},
'title': space_title(figure_title),
'width': 1200,
'height': 1200,
'showlegend': False,
'hovermode': 'closest',
'updatemenus': [{
'type': 'buttons',
'buttons': [
{'label': 'Play',
'method': 'animate',
'args': [None]},
{
'args': [[None], {'frame': {'duration': 0, 'redraw': False},
'mode': 'immediate',
'transition': {'duration': 0}}],
'label': 'Pause',
'method': 'animate'
}
]}]
},
'frames': []}
return figure
def poincare_2d_visualization(
model,
animation,
epoch,
eval_result,
avg_loss,
avg_pos_loss,
avg_neg_loss,
tree,
figure_title,
num_nodes=50,
show_node_labels=()):
"""Create a 2-d plot of the nodes and edges of a 2-d poincare embedding.
Parameters
----------
model : :class:`~hyperbolic.dag_emb_model.DAGEmbeddingModel`
The model to visualize, model size must be 2.
tree : list
Set of tuples containing the direct edges present in the original dataset.
figure_title : str
Title of the plotted figure.
num_nodes : int or None
Number of nodes for which edges are to be plotted.
If `None`, all edges are plotted.
Helpful to limit this in case the data is too large to avoid a messy plot.
show_node_labels : iterable
Iterable of nodes for which to show labels by default.
Returns
-------
:class:`plotly.graph_objs.Figure`
Plotly figure that contains plot.
"""
vectors = model.kv.syn0
if vectors.shape[1] != 2:
raise ValueError('Can only plot 2-D vectors')
node_labels = model.kv.index2word
nodes_x = list(vectors[:, 0])
nodes_y = list(vectors[:, 1])
nodes = dict(
x=nodes_x, y=nodes_y,
mode='markers',
marker=dict(color='rgb(30, 100, 200)'),
text=node_labels,
textposition='bottom'
)
nodes_x, nodes_y, node_labels = [], [], []
for node in show_node_labels:
if node in model.kv:
vector = model.kv[node]
nodes_x.append(vector[0])
nodes_y.append(vector[1])
node_labels.append(node)
nodes_with_labels = dict(
x=nodes_x, y=nodes_y,
mode='markers+text',
marker=dict(color='rgb(200, 100, 200)'),
text=node_labels,
textfont=dict(
family='sans serif',
size=18,
color='#ff7f0e' # orange
),
textposition='bottom'
)
node_out_degrees = Counter(hypernym_pair[1] for hypernym_pair in tree)
if num_nodes is None:
chosen_nodes = list(node_out_degrees.keys())
else:
chosen_nodes = list(sorted(node_out_degrees.keys(), key=lambda k: -node_out_degrees[k]))[:num_nodes]
edges_x = []
edges_y = []
for u, v in tree:
if not(u in chosen_nodes or v in chosen_nodes):
continue
vector_u = model.kv[u]
vector_v = model.kv[v]
edges_x += [vector_u[0], vector_v[0], None]
edges_y += [vector_u[1], vector_v[1], None]
edges = dict(
x=edges_x, y=edges_y, mode="line", hoverinfo=False,
line=dict(color='rgb(50,50,50)', width=1))
layout = go.Layout(
title=figure_title, showlegend=False, hovermode='closest', width=1500, height=1500,
xaxis={'range': [-1, 1.3], 'autorange': False},
yaxis={'range': [-1, 1.3], 'autorange': False},
updatemenus= [{'type': 'buttons',
'buttons': [
{'label': 'Play',
'method': 'animate',
'args': [None]
},
{
'args': [[None], {'frame': {'duration': 0, 'redraw': False},
'mode': 'immediate',
'transition': {'duration': 0}}],
'label': 'Pause',
'method': 'animate'
}
]}]
)
epoch_sticker = dict(
x=[0.5], y = [1.2], mode='text', text=['Epoch : ' + str(epoch)],
textfont=dict(
family='sans serif',
size=20,
color='rgb(200,0,0)'
),
)
result_str = str(eval_result) + '<br>'
result_str += 'loss = %.2f; pos loss = %.2f; neg loss = %.2f' % (avg_loss, avg_pos_loss, avg_neg_loss)
eval_result_sticker = dict(
x=[0.5], y = [1.1],
mode='text',
text=[result_str],
textfont=dict(
family='sans serif',
size=20,
color='rgb(0,0,200)'
),
)
# Add a new frame into the animation
frame = {'data': [], 'name': str(epoch)}
frame['data'].append(edges)
frame['data'].append(nodes_with_labels)
frame['data'].append(eval_result_sticker)
frame['data'].append(epoch_sticker)
animation['frames'].append(frame)
if epoch == 0:
animation['data'].append(edges)
animation['data'].append(nodes_with_labels)
animation['data'].append(eval_result_sticker)
animation['data'].append(epoch_sticker)
return go.Figure(data=[edges, nodes, nodes_with_labels, eval_result_sticker, epoch_sticker], layout=layout)
def poincare_distance_heatmap(origin_point, x_range=(-1.0, 1.0), y_range=(-1.0, 1.0), num_points=100):
"""Create a heatmap of Poincare distances from `origin_point` for each point (x, y),
where x and y lie in `x_range` and `y_range` respectively, with `num_points` points chosen uniformly in both ranges.
Parameters
----------
origin_point : tuple (int, int)
(x, y) from which distances are to be measured and plotted.
x_range : tuple (int, int)
Range for x-axis from which to choose `num_points` points.
y_range : tuple (int, int)
Range for y-axis from which to choose `num_points` points.
num_points : int
Number of points to choose from `x_range` and `y_range`.
Notes
-----
Points outside the unit circle are ignored, since the Poincare distance is defined
only for points inside the circle boundaries (exclusive of the boundary).
Returns
-------
:class:`plotly.graph_objs.Figure`
Plotly figure that contains plot
"""
epsilon = 1e-8 # Can't choose (-1.0, -1.0) or (1.0, 1.0), distance undefined
x_range, y_range = list(x_range), list(y_range)
if x_range[0] == -1.0 and y_range[0] == -1.0:
x_range[0] += epsilon
y_range[0] += epsilon
if x_range[0] == 1.0 and y_range[0] == 1.0:
x_range[0] -= epsilon
y_range[0] -= epsilon
x_axis_values = np.linspace(x_range[0], x_range[1], num=num_points)
y_axis_values = np.linspace(x_range[0], x_range[1], num=num_points)
x, y = np.meshgrid(x_axis_values, y_axis_values)
all_points = np.dstack((x, y)).swapaxes(1, 2).swapaxes(0, 1).reshape(2, num_points ** 2).T
norms = np.linalg.norm(all_points, axis=1)
all_points = all_points[norms < 1]
origin_point = np.array(origin_point)
all_distances = PoincareKeyedVectors.poincare_dists(origin_point, all_points)
distances = go.Scatter(
x=all_points[:, 0],
y=all_points[:, 1],
mode='markers',
marker=dict(
size='9',
color=all_distances,
colorscale='Viridis',
showscale=True,
colorbar=go.ColorBar(
title='Poincare Distance'
),
),
text=[
'Distance from (%.2f, %.2f): %.2f' % (origin_point[0], origin_point[1], d)
for d in all_distances],
name='', # To avoid the default 'trace 0'
)
origin = go.Scatter(
x=[origin_point[0]],
y=[origin_point[1]],
name='Distance from (%.2f, %.2f)' % (origin_point[0], origin_point[1]),
mode='markers+text',
marker=dict(
size='10',
color='rgb(200, 50, 50)'
)
)
layout = go.Layout(
width=900,
height=800,
showlegend=False,
title='Poincare Distances from (%.2f, %.2f)' % (origin_point[0], origin_point[1]),
hovermode='closest',
)
return go.Figure(data=[distances, origin], layout=layout)