Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add progress bar option when encoding sentences #303

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions top2vec/Top2Vec.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ class Top2Vec:

embedding_batch_size: int (default=32)
Batch size for documents being embedded.

show_progress_bar: bool (default False)
Output a progress bar when encode sentences

split_documents: bool (default False)
If set to True, documents will be split into parts before embedding.
Expand Down Expand Up @@ -374,6 +377,7 @@ def __init__(self,
embedding_model='doc2vec',
embedding_model_path=None,
embedding_batch_size=32,
show_progress_bar=False,
split_documents=False,
document_chunker='sequential',
chunk_length=100,
Expand Down Expand Up @@ -564,6 +568,7 @@ def __init__(self,

self.embed = None
self.embedding_model = embedding_model
self.show_progress_bar = show_progress_bar

self._check_import_status()

Expand Down Expand Up @@ -609,7 +614,7 @@ def return_doc(doc):

# embed words
self.word_indexes = dict(zip(self.vocab, range(len(self.vocab))))
self.word_vectors = self._l2_normalize(np.array(self.embed(self.vocab)))
self.word_vectors = self._l2_normalize(np.array(self.embed(self.vocab, show_progress_bar=self.show_progress_bar)))

# embed documents

Expand Down Expand Up @@ -832,24 +837,24 @@ def _embed_documents(self, train_corpus, batch_size):
extra = len(train_corpus) % batch_size

for ind in range(0, batches):
document_vectors.append(self.embed(train_corpus[current:current + batch_size]))
document_vectors.append(self.embed(train_corpus[current:current + batch_size], show_progress_bar=self.show_progress_bar))
current += batch_size

if extra > 0:
document_vectors.append(self.embed(train_corpus[current:current + extra]))
document_vectors.append(self.embed(train_corpus[current:current + extra], show_progress_bar=self.show_progress_bar))

document_vectors = self._l2_normalize(np.array(np.vstack(document_vectors)))

else:
document_vectors = self.embed(train_corpus, batch_size=batch_size)
document_vectors = self.embed(train_corpus, batch_size=batch_size, show_progress_bar=self.show_progress_bar)

return document_vectors

def _embed_query(self, query):
self._check_import_status()
self._check_model_status()

return self._l2_normalize(np.array(self.embed([query])[0]))
return self._l2_normalize(np.array(self.embed([query], show_progress_bar=self.show_progress_bar)[0]))

def _create_topic_vectors(self, cluster_labels):
unique_labels = set(cluster_labels)
Expand Down Expand Up @@ -1547,7 +1552,7 @@ def add_documents(self,
else:
docs_processed = [tokenizer(doc) for doc in documents]
docs_training = [' '.join(doc) for doc in docs_processed]
document_vectors = self._embed_documents(docs_training, embedding_batch_size)
document_vectors = self._embed_documents(docs_training, embedding_batch_size, show_progress_bar)
self.document_vectors = np.vstack([self.document_vectors, document_vectors])

# update index
Expand Down