Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-61: added visualization and word/char visual
tSNE makes sense on the word level. Bidirectional character embeddings don't make much sense.
- Loading branch information
Showing
5 changed files
with
223 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,3 +104,6 @@ venv.bak/ | |
|
||
# mypy | ||
.mypy_cache/ | ||
|
||
# data directory | ||
resources/data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
from .tsne import tSNE | ||
from .manifold import tSNE, uMap, show, prepare_word_embeddings, prepare_char_embeddings, word_contexts, char_contexts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
from sklearn.manifold import TSNE | ||
from umap import UMAP | ||
import tqdm | ||
import numpy | ||
import blessings | ||
|
||
|
||
t = blessings.Terminal() | ||
|
||
|
||
def prepare_word_embeddings(embeddings, sentences): | ||
X = [] | ||
|
||
print('computing embeddings') | ||
for sentence in tqdm.tqdm(sentences): | ||
embeddings.embed(sentence) | ||
|
||
for i, token in enumerate(sentence): | ||
X.append(token.embedding.detach().numpy()[None, :]) | ||
|
||
X = numpy.concatenate(X, 0) | ||
|
||
return X | ||
|
||
|
||
def word_contexts(sentences): | ||
contexts = [] | ||
|
||
for sentence in sentences: | ||
|
||
strs = [x.text for x in sentence.tokens] | ||
|
||
for i, token in enumerate(strs): | ||
prop = '<b><font color="red"> {token} </font></b>'.format( | ||
token=token) | ||
|
||
prop = ' '.join(strs[max(i - 4, 0):i]) + prop | ||
prop = prop + ' '.join(strs[i + 1:min(len(strs), i + 5)]) | ||
|
||
contexts.append('<p>' + prop + '</p>') | ||
|
||
return contexts | ||
|
||
|
||
def prepare_char_embeddings(embeddings, sentences): | ||
|
||
X = [] | ||
|
||
print('computing embeddings') | ||
for sentence in tqdm.tqdm(sentences): | ||
|
||
sentence = ' '.join([x.text for x in sentence]) | ||
|
||
hidden = embeddings.lm.get_representation(sentence) | ||
X.append(hidden.squeeze().detach().numpy()) | ||
|
||
X = numpy.concatenate(X, 0) | ||
|
||
return X | ||
|
||
|
||
def char_contexts(sentences): | ||
|
||
contexts = [] | ||
|
||
for sentence in sentences: | ||
sentence = ' '.join([token.text for token in sentence]) | ||
|
||
for i, char in enumerate(sentence): | ||
|
||
context = '<span style="background-color: yellow"><b>{}</b></span>'.format(char) | ||
context = ''.join(sentence[max(i - 30, 0):i]) + context | ||
context = context + ''.join(sentence[i + 1:min(len(sentence), i + 30)]) | ||
|
||
contexts.append(context) | ||
|
||
return contexts | ||
|
||
|
||
class _Transform: | ||
def __init__(self): | ||
pass | ||
|
||
def fit(self, X): | ||
return self.transform.fit_transform(X) | ||
|
||
|
||
class tSNE(_Transform): | ||
def __init__(self): | ||
|
||
super().__init__() | ||
|
||
self.transform = \ | ||
TSNE(n_components=2, verbose=1, perplexity=40, n_iter=300) | ||
|
||
|
||
class uMap(_Transform): | ||
def __init__(self): | ||
|
||
super().__init__() | ||
|
||
self.transform = UMAP( | ||
n_neighbors = 5, | ||
min_dist = 0.3, | ||
metric = 'correlation', | ||
) | ||
|
||
|
||
def show(X, contexts): | ||
import matplotlib.pyplot | ||
import mpld3 | ||
|
||
fig, ax = matplotlib.pyplot.subplots() | ||
|
||
ax.grid(True, alpha=0.3) | ||
|
||
points = ax.plot(X[:, 0], X[:, 1], 'o', color='b', | ||
mec='k', ms=5, mew=1, alpha=.6) | ||
|
||
ax.set_xlabel('x') | ||
ax.set_ylabel('y') | ||
ax.set_title('Hover mouse to reveal context', size=20) | ||
|
||
tooltip = mpld3.plugins.PointHTMLTooltip( | ||
points[0], | ||
contexts, | ||
voffset=10, | ||
hoffset=10 | ||
) | ||
|
||
mpld3.plugins.connect(fig, tooltip) | ||
|
||
mpld3.show() |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters