Skip to content

Commit

Permalink
GH-61: used sklearn
Browse files Browse the repository at this point in the history
  • Loading branch information
Duncan Blythe committed Aug 13, 2018
1 parent 25b311a commit 503c1d1
Show file tree
Hide file tree
Showing 4 changed files with 2,066 additions and 0 deletions.
1 change: 1 addition & 0 deletions flair/visual/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .tsne import tSNE
31 changes: 31 additions & 0 deletions flair/visual/tsne.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from sklearn.manifold import TSNE
import tqdm
import numpy


class tSNE:
def __init__(self, embeddings):

self.embeddings = embeddings

self.transform = \
TSNE(n_components=2, verbose=1, perplexity=40, n_iter=300)

def _prepare(self, sentences):

X = []

print('computing embeddings')
for sentence in tqdm.tqdm(sentences):
self.embeddings.embed(sentence)

for token in sentence:
X.append(token.embedding.detach().numpy()[None, :])

X = numpy.concatenate(X, 0)

return X

def fit(self, sentences):

return self.transform.fit_transform(self._prepare(sentences))
Loading

0 comments on commit 503c1d1

Please sign in to comment.