
    c D                         d Z ddlZddlZddlmZmZmZ ddlZddl	m
Z
 ddlmZmZ  ej        e          Z G d de          Zd ZdS )	a   Online Latent Dirichlet Allocation (LDA) in Python, using all CPU cores to parallelize and speed up model training.

The parallelization uses multiprocessing; in case this doesn't work for you for some reason,
try the :class:`gensim.models.ldamodel.LdaModel` class which is an equivalent, but more straightforward and single-core
implementation.

The training algorithm:

* is **streamed**: training documents may come in sequentially, no random access required,
* runs in **constant memory** w.r.t. the number of documents: size of the
  training corpus does not affect memory footprint, can process corpora larger than RAM

Wall-clock `performance on the English Wikipedia <http://radimrehurek.com/gensim/wiki.html>`_ (2G corpus positions,
3.5M documents, 100K features, 0.54G non-zero entries in the final bag-of-words matrix), requesting 100 topics:


====================================================== ==============
 algorithm                                             training time
====================================================== ==============
 LdaMulticore(workers=1)                               2h30m
 LdaMulticore(workers=2)                               1h24m
 LdaMulticore(workers=3)                               1h6m
 old LdaModel()                                        3h44m
 simply iterating over input corpus = I/O overhead     20m
====================================================== ==============

(Measured on `this i7 server <http://www.hetzner.de/en/hosting/produkte_rootserver/ex40ssd>`_
with 4 physical cores, so that optimal `workers=3`, one less than the number of cores.)

This module allows both LDA model estimation from a training corpus and inference of topic distribution on new,
unseen documents. The model can also be updated with new documents for online training.

The core estimation code is based on the `onlineldavb.py script
<https://github.com/blei-lab/onlineldavb/blob/master/onlineldavb.py>`_, by
Matthew D. Hoffman, David M. Blei, Francis Bach:
`'Online Learning for Latent Dirichlet Allocation', NIPS 2010`_.

.. _'Online Learning for Latent Dirichlet Allocation', NIPS 2010: online-lda_
.. _'Online Learning for LDA' by Hoffman et al.: online-lda_
.. _online-lda: https://papers.neurips.cc/paper/2010/file/71f6278d140af599e06ad9bf1ba03cb0-Paper.pdf

Usage examples
--------------
The constructor estimates Latent Dirichlet Allocation model parameters based on a training corpus

.. sourcecode:: pycon

    >>> from gensim.test.utils import common_corpus, common_dictionary
    >>>
    >>> lda = LdaMulticore(common_corpus, id2word=common_dictionary, num_topics=10)

Save a model to disk, or reload a pre-trained model

.. sourcecode:: pycon

    >>> from gensim.test.utils import datapath
    >>>
    >>> # Save model to disk.
    >>> temp_file = datapath("model")
    >>> lda.save(temp_file)
    >>>
    >>> # Load a potentially pretrained model from disk.
    >>> lda = LdaModel.load(temp_file)

Query, or update the model using new, unseen documents

.. sourcecode:: pycon

    >>> other_texts = [
    ...     ['computer', 'time', 'graph'],
    ...     ['survey', 'response', 'eps'],
    ...     ['human', 'system', 'computer']
    ... ]
    >>> other_corpus = [common_dictionary.doc2bow(text) for text in other_texts]
    >>>
    >>> unseen_doc = other_corpus[0]
    >>> vector = lda[unseen_doc]  # get topic probability distribution for a document
    >>>
    >>> # Update the model by incrementally training on the new corpus.
    >>> lda.update(other_corpus)  # update the LDA model with additional documents

    N)PoolQueue	cpu_count)utils)LdaModelLdaStatec                   \     e Zd ZdZddddddddddd	d
ddddddej        f fd	ZddZ xZS )LdaMulticorezAn optimized implementation of the LDA algorithm, able to harness the power of multicore CPUs.
    Follows the similar API as the parent class :class:`~gensim.models.ldamodel.LdaModel`.

    Nd   i     F	symmetricg      ?g      ?
   2   gMbP?g{Gz?c                 ,   |t          dt                      dz
            n|| _        || _        t	          |t
                    r|dk    rt          d          t          t          |           	                    |||||||	|
|||||||||           dS )a*  

        Parameters
        ----------
        corpus : {iterable of list of (int, float), scipy.sparse.csc}, optional
            Stream of document vectors or sparse matrix of shape (`num_documents`, `num_terms`).
            If not given, the model is left untrained (presumably because you want to call
            :meth:`~gensim.models.ldamodel.LdaModel.update` manually).
        num_topics : int, optional
            The number of requested latent topics to be extracted from the training corpus.
        id2word : {dict of (int, str),  :class:`gensim.corpora.dictionary.Dictionary`}
            Mapping from word IDs to words. It is used to determine the vocabulary size, as well as for
            debugging and topic printing.
        workers : int, optional
            Number of workers processes to be used for parallelization. If None all available cores
            (as estimated by `workers=cpu_count()-1` will be used. **Note** however that for
            hyper-threaded CPUs, this estimation returns a too high number -- set `workers`
            directly to the number of your **real** cores (not hyperthreads) minus one, for optimal performance.
        chunksize :  int, optional
            Number of documents to be used in each training chunk.
        passes : int, optional
            Number of passes through the corpus during training.
        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,
                * 1D array of length equal to num_topics to denote an asymmetric user defined prior for each topic.

            Alternatively default prior selecting strategies can be employed by supplying a string:
                * 'symmetric': (default) Uses a fixed symmetric prior of `1.0 / num_topics`,
                * 'asymmetric': Uses a fixed normalized asymmetric prior of `1.0 / (topic_index + sqrt(num_topics))`.
        eta : {float, numpy.ndarray of float, list of float, str}, optional
            A-priori belief on topic-word distribution, this can be:
                * scalar for a symmetric prior over topic-word distribution,
                * 1D array of length equal to num_words to denote an asymmetric user defined prior for each word,
                * matrix of shape (num_topics, num_words) to assign a probability for each word-topic combination.

            Alternatively default prior selecting strategies can be employed by supplying a string:
                * 'symmetric': (default) Uses a fixed symmetric prior of `1.0 / num_topics`,
                * 'auto': Learns an asymmetric prior from the corpus.
        decay : float, optional
            A number between (0.5, 1] to weight what percentage of the previous lambda value is forgotten
            when each new document is examined. Corresponds to :math:`\kappa` from
            `'Online Learning for LDA' by Hoffman et al.`_
        offset : float, optional
            Hyper-parameter that controls how much we will slow down the first steps the first few iterations.
            Corresponds to :math:`\tau_0` from `'Online Learning for LDA' by Hoffman et al.`_
        eval_every : int, optional
            Log perplexity is estimated every that many updates. Setting this to one slows down training by ~2x.
        iterations : int, optional
            Maximum number of iterations through the corpus when inferring the topic distribution of a corpus.
        gamma_threshold : float, optional
            Minimum change in the value of the gamma parameters to continue iterating.
        minimum_probability : float, optional
            Topics with a probability lower than this threshold will be filtered out.
        random_state : {np.random.RandomState, int}, optional
            Either a randomState object or a seed to generate one. Useful for reproducibility.
            Note that results can still vary due to non-determinism in OS scheduling of the worker processes.
        minimum_phi_value : float, optional
            if `per_word_topics` is True, this represents a lower bound on the term probabilities.
        per_word_topics : bool
            If True, the model also computes a list of topics, sorted in descending order of most likely topics for
            each word, along with their phi values multiplied by the feature length (i.e. word count).
        dtype : {numpy.float16, numpy.float32, numpy.float64}, optional
            Data-type to use during calculations inside model. All inputs are also converted.

        Nr   autozFauto-tuning alpha not implemented in LdaMulticore; use plain LdaModel.)corpus
num_topicsid2word	chunksizepassesalphaetadecayoffset
eval_every
iterationsgamma_thresholdrandom_stateminimum_probabilityminimum_phi_valueper_word_topicsdtype)
maxr   workersbatch
isinstancestrNotImplementedErrorsuperr
   __init__)selfr   r   r   r$   r   r   r%   r   r   r   r   r   r   r   r   r   r    r!   r"   	__class__s                       :lib/python3.11/site-packages/gensim/models/ldamulticore.pyr*   zLdaMulticore.__init__m   s    N 3:Ns1ikkAo...w
eS!! 	pevo 	p%&nooolD!!**jyuRU:*+,\o/X] 	+ 	
 	
 	
 	
 	
    c                 N    	 t          |          nC# t          $ r6 t                              d           t	          d |D                       Y nw xY wdk    rt                              d           dS  j        xj        z  c_         j        rd}nd} j         j	        z   j
        pdt          z            }t          dz            }t                              d	| j         j        | j         j        	  	         | j        z  d
k     rt                              d           t%          d j	        z            }t%                       fdd f
d	}t                              d j	                   t'           j	        t(          | f          }t+           j                  D ]Bdgdc}	t-           j         j        j        j                  t5          j        | j        |          }
t9          |
          D ]\  }|	t                    z  }		 	 |                    | j        fd           dxx         dz  cc<   t                              d|| j        z  t                    z   d                    n # t<          j        $ r  |             Y nw xY w |             d         dk    r |d           d         dk    |	k    rtA          d          D|!                                 dS )aS  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
        the maximum number of allowed iterations is reached. `corpus` must be an iterable. The E step is distributed
        into the several processes.

        Notes
        -----
        This update also supports updating an already trained model (`self`) with new documents from `corpus`;
        the two models are then merged in proportion to the number of old vs. new documents.
        This feature is still experimental for non-stationary input streams.

        For stationary input (no topic drift in new documents), on the other hand,
        this equals the online update of `'Online Learning for LDA' by Hoffman et al.`_
        and is guaranteed to converge for any `decay` in (0.5, 1].

        Parameters
        ----------
        corpus : {iterable of list of (int, float), scipy.sparse.csc}, optional
            Stream of document vectors or sparse matrix of shape (`num_documents`, `num_terms`) used to update the
            model.
        chunks_as_numpy : bool
            Whether each chunk passed to the inference step should be a np.ndarray or not. Numpy can in some settings
            turn the term IDs into floats, these will be converted back into integers in inference, which incurs a
            performance hit. For distributed computing it may be desirable to keep the chunks as `numpy.ndarray`.

        z4input corpus stream has no len(); counting documentsc              3      K   | ]}d V  dS )r   N ).0_s     r-   	<genexpr>z&LdaMulticore.update.<locals>.<genexpr>   s"      ..!A......r.   r   z1LdaMulticore.update() called with an empty corpusNr%   onliner   zrunning %s LDA training, %s topics, %i passes over the supplied corpus of %i documents, updating every %i documents, evaluating every ~%i documents, iterating %ix with a convergence threshold of %fr   zxtoo few updates, training might not converge; consider increasing the number of passes or iterations to improve accuracy   )maxsizec                  `    t          j         z   j        j        z  z   j                   S )N)powr   num_updatesr   r   )pass_r+   s   r-   rhoz LdaMulticore.update.<locals>.rho  s0    t{U*d.>.OPSWS]R]^^^r.   Fc                   
 d}                                 sM                                                               dxx         dz  cc<   d}                                 M| r|rd         dk    sj        k    rk
                     	            dk                                                dk    r,| s
j        z  z  dk    r
                               dS dS dS dS )z
            Clear the result queue, merging all intermediate results, and update the
            LDA model if necessary.

            Fr   r   T)
total_docsN)emptymergegetnumdocsdo_mstepresetr:   log_perplexity)force
merged_newchunkr   	lencorpusotherr;   
queue_sizeresult_queuer<   r+   updateafters     r-   process_result_queuez1LdaMulticore.update.<locals>.process_result_queue  sB    J"((** "L,,..///1"!
 #((** "
  E* EA!); ER]A] EcceeUEAI666> Eu E1AK1OS]0]ab0b E'')'DDDDDE EE EE Er.   z%training LDA model using %i processes)as_numpyT)blockz[PROGRESS: pass %i, dispatched chunk #%i = documents up to #%i/%i, outstanding queue size %i)rF   zIinput corpus size changed during training (don't use generators as input)F)"len	TypeErrorloggerwarningsumstaterB   r%   r   r$   r   minr#   infor   r   r   r   r   r   worker_e_stepranger   r   sstatsshaper   grouper	enumerateputqueueFullRuntimeError	terminate)r+   r   chunks_as_numpy
updatetype	evalafterupdates_per_pass	job_queuerN   poolreallenchunk_streamchunk_norH   r   rI   rJ   r;   rK   rL   r<   rM   s   `           @@@@@@@@@r-   updatezLdaMulticore.update   s   :	/FII 	/ 	/ 	/NNQRRR..v.....III	/ > 	NNNOOOF
i': 	8 J#KK!J.4<7K_)
	:#;<<	q)k"9::? it(<	
 	
 	
 dk)B. 	NN]  
 !dl"2333	ww
	_ 	_ 	_ 	_ 	_ 	_	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E$ 	;T\JJJDL-)\41PQQ4;''  	p  	pE#$#qJTXtz'8'>??E =/ZZZL#,\#:#: ' '%3u::%//!x
&C5QQQ"1*8!8X-FU-SU^`jkl`m  
  : / / / -,.....// %$&&&& Q-!# 1$$40000 Q-!# 1 )# p"#nooop 	s"    =AA>A,J++KKrQ   )	__name__
__module____qualname____doc__npfloat32r*   rn   __classcell__)r,   s   @r-   r
   r
   h   s          #sD$kSRB!&Tt#'bj	S
 S
 S
 S
 S
 S
j@ @ @ @ @ @ @ @r.   r
   c                 .   t                               d           	 t                               d           |                                 \  }}}t                               d|t          |                     ||_        |                                 |j                                         |                    |           ~t                               d           |                    |j                   d|_        t                               d           )ak  Perform E-step for each job.

    Parameters
    ----------
    input_queue : queue of (int, list of (int, float), :class:`~gensim.models.lda_worker.Worker`)
        Each element is a job characterized by its ID, the corpus chunk to be processed in BOW format and the worker
        responsible for processing it.
    result_queue : queue of :class:`~gensim.models.ldamodel.LdaState`
        After the worker finished the job, the state of the resulting (trained) worker model is appended to this queue.
    worker_lda : :class:`~gensim.models.ldamulticore.LdaMulticore`
        LDA instance which performed e step
    z#worker process entering E-step loopTzgetting a new jobz$processing chunk #%i of %i documentsz#processed chunk, queuing the resultNz
result put)	rT   debugrA   rR   rW   
sync_staterD   do_estepr`   )input_queuerL   
worker_ldarm   rH   w_states         r-   rZ   rZ   E  s     LL6777#()))#.??#4#4 %;Xs5zzRRR"
   E""":;;;)***
\"""#r.   )rr   loggingra   multiprocessingr   r   r   numpyrs   gensimr   gensim.models.ldamodelr   r   	getLoggerro   rT   r
   rZ   r1   r.   r-   <module>r      s   Q Qf   2 2 2 2 2 2 2 2 2 2           5 5 5 5 5 5 5 5 
	8	$	$Z Z Z Z Z8 Z Z Zz# # # # #r.   