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

Proper usage of update_every in LdaMulticore #3140

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions gensim/models/atmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ def __init__(self, corpus=None, num_topics=100, id2word=None, author2doc=None, d
* 'symmetric': (default) Uses a fixed symmetric prior of `1.0 / num_topics`,
* 'auto': Learns an asymmetric prior from the corpus.
update_every : int, optional
Make updates in topic probability for latest mini-batch.
Number of chunks to be iterated through before each M step of EM.
Set to 0 for batch learning, > 1 for online iterative learning.
eval_every : int, optional
Calculate and estimate log perplexity for latest mini-batch.
gamma_threshold : float, optional
Expand Down Expand Up @@ -803,7 +804,7 @@ def update(self, corpus=None, author2doc=None, doc2author=None, chunksize=None,
self.state.numdocs += lencorpus

if update_every:
updatetype = "online"
updatetype = "online (single-pass)" if self.passes == 1 else "online (multi-pass)"
updateafter = min(lencorpus, update_every * self.numworkers * chunksize)
else:
updatetype = "batch"
Expand Down
10 changes: 3 additions & 7 deletions gensim/models/ldamodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ def __init__(self, corpus=None, num_topics=100, id2word=None,
passes : int, optional
Number of passes through the corpus during training.
update_every : int, optional
Number of documents to be iterated through for each update.
Number of chunks to be iterated through before each M step of EM.
Set to 0 for batch learning, > 1 for online iterative learning.
alpha : {float, numpy.ndarray of float, list of float, str}, optional
A-priori belief on document-topic distribution, this can be:
Expand Down Expand Up @@ -932,11 +932,7 @@ def update(self, corpus, chunksize=None, decay=None, offset=None,
self.state.numdocs += lencorpus

if update_every:
updatetype = "online"
if passes == 1:
updatetype += " (single-pass)"
else:
updatetype += " (multi-pass)"
updatetype = "online (single-pass)" if self.passes == 1 else "online (multi-pass)"
updateafter = min(lencorpus, update_every * self.numworkers * chunksize)
else:
updatetype = "batch"
Expand Down Expand Up @@ -1053,7 +1049,7 @@ def do_mstep(self, rho, other, extra_pass=False):
----------
rho : float
Learning rate.
other : :class:`~gensim.models.ldamodel.LdaModel`
other : :class:`~gensim.models.ldamodel.LdaState`
The model whose sufficient statistics will be used to update the topics.
extra_pass : bool, optional
Whether this step required an additional pass over the corpus.
Expand Down
29 changes: 15 additions & 14 deletions gensim/models/ldamulticore.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class LdaMulticore(LdaModel):

"""
def __init__(self, corpus=None, num_topics=100, id2word=None, workers=None,
chunksize=2000, passes=1, batch=False, alpha='symmetric',
chunksize=2000, passes=1, update_every=1, alpha='symmetric',
eta=None, decay=0.5, offset=1.0, eval_every=10, iterations=50,
gamma_threshold=0.001, random_state=None, minimum_probability=0.01,
minimum_phi_value=0.01, per_word_topics=False, dtype=np.float32):
Expand All @@ -133,6 +133,9 @@ def __init__(self, corpus=None, num_topics=100, id2word=None, workers=None,
Number of documents to be used in each training chunk.
passes : int, optional
Number of passes through the corpus during training.
update_every : int, optional
Number of chunks to be iterated through before each M step of EM.
Set to 0 for batch learning, > 1 for online iterative learning.
alpha : {float, numpy.ndarray of float, list of float, str}, optional
A-priori belief on document-topic distribution, this can be:
* scalar for a symmetric prior over document-topic distribution,
Expand Down Expand Up @@ -178,24 +181,20 @@ def __init__(self, corpus=None, num_topics=100, id2word=None, workers=None,

"""
self.workers = max(1, cpu_count() - 1) if workers is None else workers
self.batch = batch

if isinstance(alpha, str) and alpha == 'auto':
raise NotImplementedError("auto-tuning alpha not implemented in LdaMulticore; use plain LdaModel.")

super(LdaMulticore, self).__init__(
corpus=corpus, num_topics=num_topics,
id2word=id2word, chunksize=chunksize, passes=passes, alpha=alpha, eta=eta,
corpus=corpus, num_topics=num_topics, id2word=id2word, distributed=False, # not distributed across machines
chunksize=chunksize, passes=passes, update_every=update_every, alpha=alpha, eta=eta,
decay=decay, offset=offset, eval_every=eval_every, iterations=iterations,
gamma_threshold=gamma_threshold, random_state=random_state, minimum_probability=minimum_probability,
gamma_threshold=gamma_threshold, minimum_probability=minimum_probability, random_state=random_state,
minimum_phi_value=minimum_phi_value, per_word_topics=per_word_topics, dtype=dtype,
)

def update(self, corpus, chunks_as_numpy=False):
"""Train the model with new documents, by EM-iterating over `corpus` until the topics converge
(or until the maximum number of allowed iterations is reached).

Train the model with new documents, by EM-iterating over the corpus until the topics converge, or until
"""Train the model with new documents, by EM-iterating over the corpus until the topics converge, or until
the maximum number of allowed iterations is reached. `corpus` must be an iterable. The E step is distributed
into the several processes.

Expand Down Expand Up @@ -231,14 +230,16 @@ def update(self, corpus, chunks_as_numpy=False):

self.state.numdocs += lencorpus

if self.batch:
# Same as in LdaModel but self.workers (processes) is used instead of self.numworkers (machines)
if self.update_every:
updatetype = "online (single-pass)" if self.passes == 1 else "online (multi-pass)"
updateafter = min(lencorpus, self.update_every * self.workers * self.chunksize)
else:
updatetype = "batch"
updateafter = lencorpus
else:
updatetype = "online"
updateafter = self.chunksize * self.workers

eval_every = self.eval_every or 0
evalafter = min(lencorpus, eval_every * updateafter)
evalafter = min(lencorpus, eval_every * self.workers * self.chunksize)

updates_per_pass = max(1, lencorpus / updateafter)
logger.info(
Expand Down