diff --git a/gensim/models/atmodel.py b/gensim/models/atmodel.py index 017bf6efd0..9253a6a6d8 100755 --- a/gensim/models/atmodel.py +++ b/gensim/models/atmodel.py @@ -6,30 +6,49 @@ # Licensed under the GNU LGPL v2.1 - http://www.gnu.org/licenses/lgpl.html -""" -Author-topic model in Python. +"""Author-topic model. -This module trains the author-topic model on documents and corresponding author-document -dictionaries. The training is online and is constant in memory w.r.t. the number of -documents. The model is *not* constant in memory w.r.t. the number of authors. +This module trains the author-topic model on documents and corresponding author-document dictionaries. +The training is online and is constant in memory w.r.t. the number of documents. +The model is *not* constant in memory w.r.t. the number of authors. The model can be updated with additional documents after training has been completed. It is also possible to continue training on the existing data. -The model is closely related to Latent Dirichlet Allocation. The AuthorTopicModel class -inherits the LdaModel class, and its usage is thus similar. - -Distributed computation and multiprocessing is not implemented at the moment, but may be -coming in the future. - -The model was introduced by Rosen-Zvi and co-authors in 2004 -(https://mimno.infosci.cornell.edu/info6150/readings/398.pdf). - -A tutorial can be found at -https://github.com/RaRe-Technologies/gensim/tree/develop/docs/notebooks/atmodel_tutorial.ipynb. +The model is closely related to :class:`~gensim.models.ldamodel.LdaModel`. +The :class:`~gensim.models.atmodel.AuthorTopicModel` class inherits :class:`~gensim.models.ldamodel.LdaModel`, +and its usage is thus similar. + +The model was introduced by `Rosen-Zvi and co-authors: "The Author-Topic Model for Authors and Documents" +`_. The model correlates the authorship information with the topics to give a better +insight on the subject knowledge of an author. + +Example +------- +>>> from gensim.models import AuthorTopicModel +>>> from gensim.corpora import mmcorpus +>>> from gensim.test.utils import common_dictionary, datapath, temporary_file + +>>> author2doc = { +... 'john': [0, 1, 2, 3, 4, 5, 6], +... 'jane': [2, 3, 4, 5, 6, 7, 8], +... 'jack': [0, 2, 4, 6, 8] +... } +>>> +>>> corpus = mmcorpus.MmCorpus(datapath('testcorpus.mm')) +>>> +>>> with temporary_file("serialized") as s_path: +... model = AuthorTopicModel( +... corpus, author2doc=author2doc, id2word=common_dictionary, num_topics=4, +... serialized=True, serialization_path=s_path +... ) +... +... model.update(corpus, author2doc) # update the author-topic model with additional documents +>>> +>>> # construct vectors for authors +>>> author_vecs = [model.get_author_topics(author) for author in model.id2author.values()] """ - # TODO: this class inherits LdaModel and overwrites some methods. There is some code # duplication still, and a refactor could be made to avoid this. Comments with "TODOs" # are included in the code where this is the case, for example in the log_perplexity @@ -52,22 +71,25 @@ from six.moves import xrange import six -logger = logging.getLogger('gensim.models.atmodel') +logger = logging.getLogger(__name__) class AuthorTopicState(LdaState): - """ - NOTE: distributed mode not available yet in the author-topic model. This AuthorTopicState - object is kept so that when the time comes to imlement it, it will be easier. - - Encapsulate information for distributed computation of AuthorTopicModel objects. + """Encapsulate information for computation of :class:`~gensim.models.atmodel.AuthorTopicModel`.""" - Objects of this class are sent over the network, so try to keep them lean to - reduce traffic. + def __init__(self, eta, lambda_shape, gamma_shape): + """ - """ + Parameters + ---------- + eta: numpy.ndarray + Dirichlet topic parameter for sparsity. + lambda_shape: (int, int) + Initialize topic parameters. + gamma_shape: int + Initialize topic parameters. - def __init__(self, eta, lambda_shape, gamma_shape): + """ self.eta = eta self.sstats = np.zeros(lambda_shape) self.gamma = np.zeros(gamma_shape) @@ -76,7 +98,21 @@ def __init__(self, eta, lambda_shape, gamma_shape): def construct_doc2author(corpus, author2doc): - """Make a mapping from document IDs to author IDs.""" + """Create a mapping from document IDs to author IDs. + + Parameters + ---------- + corpus: iterable of list of (int, float) + Corpus in BoW format. + author2doc: dict of (str, list of int) + Mapping of authors to documents. + + Returns + ------- + dict of (int, list of str) + Document to Author mapping. + + """ doc2author = {} for d, _ in enumerate(corpus): author_ids = [] @@ -88,8 +124,19 @@ def construct_doc2author(corpus, author2doc): def construct_author2doc(doc2author): - """Make a mapping from author IDs to document IDs.""" + """Make a mapping from author IDs to document IDs. + + Parameters + ---------- + doc2author: dict of (int, list of str) + Mapping of document id to authors. + + Returns + ------- + dict of (str, list of int) + Mapping of authors to document ids. + """ # First get a set of all authors. authors_ids = set() for d, a_doc_ids in doc2author.items(): @@ -107,18 +154,7 @@ def construct_author2doc(doc2author): class AuthorTopicModel(LdaModel): - """ - The constructor estimates the author-topic model parameters based - on a training corpus: - - >>> model = AuthorTopicModel(corpus, num_topics=10, author2doc=author2doc, id2word=id2word) - - The model can be updated (trained) with new documents via - - >>> model.update(other_corpus, other_author2doc) - - Model persistency is achieved through its `load`/`save` methods. - """ + """The constructor estimates the author-topic model parameters based on a training corpus.""" def __init__(self, corpus=None, num_topics=100, id2word=None, author2doc=None, doc2author=None, chunksize=2000, passes=1, iterations=50, decay=0.5, offset=1.0, @@ -126,85 +162,52 @@ def __init__(self, corpus=None, num_topics=100, id2word=None, author2doc=None, d gamma_threshold=0.001, serialized=False, serialization_path=None, minimum_probability=0.01, random_state=None): """ - If the iterable corpus and one of author2doc/doc2author dictionaries are given, - start training straight away. If not given, the model is left untrained - (presumably because you want to call the `update` method manually). - - `num_topics` is the number of requested latent topics to be extracted from - the training corpus. - - `id2word` is a mapping from word ids (integers) to words (strings). It is - used to determine the vocabulary size, as well as for debugging and topic - printing. - - `author2doc` is a dictionary where the keys are the names of authors, and the - values are lists of documents that the author contributes to. - - `doc2author` is a dictionary where the keys are document IDs (indexes to corpus) - and the values are lists of author names. I.e. this is the reverse mapping of - `author2doc`. Only one of the two, `author2doc` and `doc2author` have to be - supplied. - - `passes` is the number of times the model makes a pass over the entire trianing - data. - - `iterations` is the maximum number of times the model loops over each document - (M-step). The iterations stop when convergence is reached. - - `chunksize` controls the size of the mini-batches. - `alpha` and `eta` are hyperparameters that affect sparsity of the author-topic - (theta) and topic-word (lambda) distributions. Both default to a symmetric - 1.0/num_topics prior. - - `alpha` can be set to an explicit array = prior of your choice. It also - support special values of 'asymmetric' and 'auto': the former uses a fixed - normalized asymmetric 1.0/topicno prior, the latter learns an asymmetric - prior directly from your data. - - `eta` can be a scalar for a symmetric prior over topic/word - distributions, or a vector of shape num_words, which can be used to - impose (user defined) asymmetric priors over the word distribution. - It also supports the special value 'auto', which learns an asymmetric - prior over words directly from your data. `eta` can also be a matrix - of shape num_topics x num_words, which can be used to impose - asymmetric priors over the word distribution on a per-topic basis - (can not be learned from data). - - Calculate and log perplexity estimate from the latest mini-batch every - `eval_every` model updates. Set to None to disable perplexity estimation. - - `decay` and `offset` parameters are the same as Kappa and Tau_0 in - Hoffman et al, respectively. `decay` controls how quickly old documents are - forgotten, while `offset` down-weights early iterations. - - `minimum_probability` controls filtering the topics returned for a document (bow). - - `random_state` can be an integer or a numpy.random.RandomState object. Set the - state of the random number generator inside the author-topic model, to ensure - reproducibility of your experiments, for example. - - `serialized` indicates whether the input corpora to the model are simple - in-memory lists (`serialized = False`) or saved to the hard-drive - (`serialized = True`). Note that this behaviour is quite different from - other Gensim models. If your data is too large to fit in to memory, use - this functionality. Note that calling `AuthorTopicModel.update` with new - data may be cumbersome as it requires all the existing data to be - re-serialized. - - `serialization_path` must be set to a filepath, if `serialized = True` is - used. Use, for example, `serialization_path = /tmp/serialized_model.mm` or use your - working directory by setting `serialization_path = serialized_model.mm`. An existing - file *cannot* be overwritten; either delete the old file or choose a different - name. - - Example: - - >>> model = AuthorTopicModel(corpus, num_topics=100, author2doc=author2doc, id2word=id2word) # train model - >>> model.update(corpus2) # update the author-topic model with additional documents - - >>> model = AuthorTopicModel( - ... corpus, num_topics=50, author2doc=author2doc, id2word=id2word, alpha='auto', eval_every=5) + Parameters + ---------- + corpus : iterable of list of (int, float), optional + Corpus in BoW format + num_topics : int, optional + Number of topics to be extracted from the training corpus. + id2word : :class:`~gensim.corpora.dictionary.Dictionary`, optional + A mapping from word ids (integers) to words (strings). + author2doc : dict of (str, list of int), optional + A dictionary where keys are the names of authors and values are lists of document IDs that the author + contributes to. + doc2author : dict of (int, list of str), optional + A dictionary where the keys are document IDs and the values are lists of author names. + chunksize : int, optional + Controls the size of the mini-batches. + passes : int, optional + Number of times the model makes a pass over the entire training data. + iterations : int, optional + Maximum number of times the model loops over each document. + decay : float, optional + Controls how old documents are forgotten. + offset : float, optional + Controls down-weighting of iterations. + alpha : float, optional + Hyperparameters for author-topic model.Supports special values of 'asymmetric' + and 'auto': the former uses a fixed normalized asymmetric 1.0/topicno prior, + the latter learns an asymmetric prior directly from your data. + eta : float, optional + Hyperparameters for author-topic model. + update_every : int, optional + Make updates in topic probability for latest mini-batch. + eval_every : int, optional + Calculate and estimate log perplexity for latest mini-batch. + gamma_threshold : float, optional + Threshold value of gamma(topic difference between consecutive two topics) + until which the iterations continue. + serialized : bool, optional + Indicates whether the input corpora to the model are simple lists + or saved to the hard-drive. + serialization_path : str, optional + Must be set to a filepath, if `serialized = True` is used. + minimum_probability : float, optional + Controls filtering the topics returned for a document (bow). + random_state : {int, numpy.random.RandomState}, optional + Set the state of the random number generator inside the author-topic model. """ # NOTE: this doesn't call constructor of a base class, but duplicates most of this code @@ -287,8 +290,8 @@ def __init__(self, corpus=None, num_topics=100, id2word=None, author2doc=None, d self.random_state = utils.get_random_state(random_state) assert (self.eta.shape == (self.num_terms,) or self.eta.shape == (self.num_topics, self.num_terms)), ( - "Invalid eta shape. Got shape %s, but expected (%d, 1) or (%d, %d)" % - (str(self.eta.shape), self.num_terms, self.num_topics, self.num_terms) + "Invalid eta shape. Got shape %s, but expected (%d, 1) or (%d, %d)" % + (str(self.eta.shape), self.num_terms, self.num_topics, self.num_terms) ) # VB constants @@ -306,14 +309,21 @@ def __init__(self, corpus=None, num_topics=100, id2word=None, author2doc=None, d self.update(corpus, author2doc, doc2author, chunks_as_numpy=use_numpy) def __str__(self): + """Get a string representation of object. + + Returns + ------- + str + String representation of current instance. + + """ return "AuthorTopicModel(num_terms=%s, num_topics=%s, num_authors=%s, decay=%s, chunksize=%s)" % \ (self.num_terms, self.num_topics, self.num_authors, self.decay, self.chunksize) def init_empty_corpus(self): - """ - Initialize an empty corpus. If the corpora are to be treated as lists, simply - initialize an empty list. If serialization is used, initialize an empty corpus - of the class `gensim.corpora.MmCorpus`. + """Initialize an empty corpus. + If the corpora are to be treated as lists, simply initialize an empty list. + If serialization is used, initialize an empty corpus using :class:`~gensim.corpora.mmcorpus.MmCorpus`. """ if self.serialized: @@ -326,11 +336,20 @@ def init_empty_corpus(self): self.corpus = [] def extend_corpus(self, corpus): - """ - Add new documents in `corpus` to `self.corpus`. If serialization is used, - then the entire corpus (`self.corpus`) is re-serialized and the new documents - are added in the process. If serialization is not used, the corpus, as a list - of documents, is simply extended. + """Add new documents from `corpus` to `self.corpus`. + + If serialization is used, then the entire corpus (`self.corpus`) is re-serialized and the new documents + are added in the process. If serialization is not used, the corpus, as a list of documents, is simply extended. + + Parameters + ---------- + corpus : iterable of list of (int, float) + Corpus in BoW format + + Raises + ------ + AssertionError + If serialized == False and corpus isn't list. """ if self.serialized: @@ -354,30 +373,61 @@ def extend_corpus(self, corpus): self.corpus.extend(corpus) def compute_phinorm(self, expElogthetad, expElogbetad): - """Efficiently computes the normalizing factor in phi.""" + """Efficiently computes the normalizing factor in phi. + + Parameters + ---------- + expElogthetad: numpy.ndarray + Value of variational distribution :math:`q(\theta|\gamma)`. + expElogbetad: numpy.ndarray + Value of variational distribution :math:`q(\\beta|\lambda)`. + + Returns + ------- + float + Value of normalizing factor. + + """ expElogtheta_sum = expElogthetad.sum(axis=0) phinorm = expElogtheta_sum.dot(expElogbetad) + 1e-100 return phinorm def inference(self, chunk, author2doc, doc2author, rhot, collect_sstats=False, chunk_doc_idx=None): - """ - Given a chunk of sparse document vectors, update gamma (parameters - controlling the topic weights) for each author corresponding to the - documents in the chunk. - - The whole input chunk of document is assumed to fit in RAM; chunking of - a large corpus must be done earlier in the pipeline. + """Give a `chunk` of sparse document vectors, update gamma for each author corresponding to the `chuck`. - If `collect_sstats` is True, also collect sufficient statistics needed - to update the model's topic-word distributions, and return a 2-tuple - `(gamma_chunk, sstats)`. Otherwise, return `(gamma_chunk, None)`. - `gamma_cunk` is of shape `len(chunk_authors) x self.num_topics`, where - `chunk_authors` is the number of authors in the documents in the - current chunk. + Warnings + -------- + The whole input chunk of document is assumed to fit in RAM, chunking of a large corpus must be done earlier + in the pipeline. Avoids computing the `phi` variational parameter directly using the - optimization presented in **Lee, Seung: Algorithms for non-negative matrix factorization, NIPS 2001**. + optimization presented in `Lee, Seung: "Algorithms for non-negative matrix factorization", NIPS 2001 + `_. + + Parameters + ---------- + chunk : iterable of list of (int, float) + Corpus in BoW format. + author2doc : dict of (str, list of int), optional + A dictionary where keys are the names of authors and values are lists of document IDs that the author + contributes to. + doc2author : dict of (int, list of str), optional + A dictionary where the keys are document IDs and the values are lists of author names. + rhot : float + Value of rho for conducting inference on documents. + collect_sstats : boolean, optional + If True - collect sufficient statistics needed to update the model's topic-word distributions, and return + `(gamma_chunk, sstats)`. Otherwise, return `(gamma_chunk, None)`. `gamma_chunk` is of shape + `len(chunk_authors) x self.num_topics`,where `chunk_authors` is the number of authors in the documents in + the current chunk. + chunk_doc_idx : numpy.ndarray, optional + Assigns the value for document index. + + Returns + ------- + (numpy.ndarray, numpy.ndarray) + gamma_chunk and sstats (if `collect_sstats == True`, otherwise - None) """ try: @@ -435,7 +485,7 @@ def inference(self, chunk, author2doc, doc2author, rhot, collect_sstats=False, c # phi is computed implicitly below, for ai, a in enumerate(authors_d): tilde_gamma[ai, :] = self.alpha + len(self.author2doc[self.id2author[a]])\ - * expElogthetad[ai, :] * np.dot(cts / phinorm, expElogbetad.T) + * expElogthetad[ai, :] * np.dot(cts / phinorm, expElogbetad.T) # Update gamma. # Interpolation between document d's "local" gamma (tilde_gamma), @@ -485,12 +535,30 @@ def inference(self, chunk, author2doc, doc2author, rhot, collect_sstats=False, c return gamma_chunk, sstats def do_estep(self, chunk, author2doc, doc2author, rhot, state=None, chunk_doc_idx=None): - """ - Perform inference on a chunk of documents, and accumulate the collected - sufficient statistics in `state` (or `self.state` if None). + """Performs inference (E-step) on a chunk of documents, and accumulate the collected sufficient statistics. - """ + Parameters + ---------- + chunk : iterable of list of (int, float) + Corpus in BoW format. + author2doc : dict of (str, list of int), optional + A dictionary where keys are the names of authors and values are lists of document IDs that the author + contributes to. + doc2author : dict of (int, list of str), optional + A dictionary where the keys are document IDs and the values are lists of author names. + rhot : float + Value of rho for conducting inference on documents. + state : int, optional + Initializes the state for a new E iteration. + chunk_doc_idx : numpy.ndarray, optional + Assigns the value for document index. + Returns + ------- + float + Value of gamma for training of model. + + """ # TODO: this method is somewhat similar to the one in LdaModel. Refactor if possible. if state is None: state = self.state @@ -503,20 +571,30 @@ def do_estep(self, chunk, author2doc, doc2author, rhot, state=None, chunk_doc_id return gamma def log_perplexity(self, chunk, chunk_doc_idx=None, total_docs=None): - """ - Calculate and return per-word likelihood bound, using the `chunk` of - documents as evaluation corpus. Also output the calculated statistics. incl. - perplexity=2^(-bound), to log at INFO level. + """Calculate per-word likelihood bound, using the `chunk` of documents as evaluation corpus. - """ + Parameters + ---------- + chunk : iterable of list of (int, float) + Corpus in BoW format. + chunk_doc_idx : numpy.ndarray, optional + Assigns the value for document index. + total_docs : int, optional + Initializes the value for total number of documents. + + Returns + ------- + float + Value of per-word likelihood bound. + """ # TODO: This method is very similar to the one in LdaModel. Refactor. if total_docs is None: total_docs = len(chunk) corpus_words = sum(cnt for document in chunk for _, cnt in document) subsample_ratio = 1.0 * total_docs / len(chunk) perwordbound = self.bound(chunk, chunk_doc_idx, subsample_ratio=subsample_ratio) / \ - (subsample_ratio * corpus_words) + (subsample_ratio * corpus_words) logger.info( "%.3f per-word bound, %.1f perplexity estimate based on a corpus of %i documents with %i words", perwordbound, np.exp2(-perwordbound), len(chunk), corpus_words @@ -526,57 +604,67 @@ def log_perplexity(self, chunk, chunk_doc_idx=None, total_docs=None): def update(self, corpus=None, author2doc=None, doc2author=None, chunksize=None, decay=None, offset=None, passes=None, update_every=None, eval_every=None, iterations=None, gamma_threshold=None, 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). `corpus` must be an iterable (repeatable stream of documents), - - 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 Hoffman et al. and is guaranteed to - converge for any `decay` in (0.5, 1.0>. Additionally, for smaller - `corpus` sizes, an increasing `offset` may be beneficial (see + """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). + + 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 + `Hoffman et al. Stochastic Variational Inference + `_ and is guaranteed to converge for any `decay` + in (0.5, 1.0>. Additionally, for smaller `corpus` sizes, an increasing `offset` may be beneficial (see Table 1 in Hoffman et al.) - If update is called with authors that already exist in the model, it will - resume training on not only new documents for that author, but also the - previously seen documents. This is necessary for those authors' topic + If update is called with authors that already exist in the model, it will resume training on not only new + documents for that author, but also the previously seen documents. This is necessary for those authors' topic distributions to converge. - Every time `update(corpus, author2doc)` is called, the new documents are - to appended to all the previously seen documents, and author2doc is - combined with the previously seen authors. + Every time `update(corpus, author2doc)` is called, the new documents are to appended to all the previously seen + documents, and author2doc is combined with the previously seen authors. To resume training on all the data seen by the model, simply call - `update()`. - - It is not possible to add new authors to existing documents, as all - documents in `corpus` are assumed to be new documents. - - Args: - corpus (gensim corpus): The corpus with which the author-topic model should be updated. - - author2doc (dict): author to document mapping corresponding to indexes in input - corpus. - - doc2author (dict): document to author mapping corresponding to indexes in input - corpus. + :meth:`~gensim.models.atmodel.AuthorTopicModel.update`. - chunks_as_numpy (bool): Whether each chunk passed to `.inference` should be a np - array of not. np 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 np - arrays. + It is not possible to add new authors to existing documents, as all documents in `corpus` are assumed to be + new documents. - For other parameter settings, see :class:`AuthorTopicModel` constructor. + Parameters + ---------- + corpus : iterable of list of (int, float) + The corpus in BoW format. + author2doc : dict of (str, list of int), optional + A dictionary where keys are the names of authors and values are lists of document IDs that the author + contributes to. + doc2author : dict of (int, list of str), optional + A dictionary where the keys are document IDs and the values are lists of author names. + chunksize : int, optional + Controls the size of the mini-batches. + decay : float, optional + Controls how old documents are forgotten. + offset : float, optional + Controls down-weighting of iterations. + passes : int, optional + Number of times the model makes a pass over the entire training data. + update_every : int, optional + Make updates in topic probability for latest mini-batch. + eval_every : int, optional + Calculate and estimate log perplexity for latest mini-batch. + iterations : int, optional + Maximum number of times the model loops over each document + gamma_threshold : float, optional + Threshold value of gamma(topic difference between consecutive two topics) + until which the iterations continue. + chunks_as_numpy : bool, optional + Whether each chunk passed to :meth:`~gensim.models.atmodel.AuthorTopicModel.inference` should be a numpy + array of 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 (not supported now) + it may be desirable to keep the chunks as numpy arrays. """ - # use parameters given in constructor, unless user explicitly overrode them if decay is None: decay = self.decay @@ -796,24 +884,40 @@ def rho(): del other def bound(self, chunk, chunk_doc_idx=None, subsample_ratio=1.0, author2doc=None, doc2author=None): - """ - Estimate the variational bound of documents from `corpus`: - E_q[log p(corpus)] - E_q[log q(corpus)] + """Estimate the variational bound of documents from `corpus`. + + :math:`\mathbb{E_{q}}[\log p(corpus)] - \mathbb{E_{q}}[\log q(corpus)]` + Notes + ----- There are basically two use cases of this method: - 1. `chunk` is a subset of the training corpus, and `chunk_doc_idx` is provided, - indicating the indexes of the documents in the training corpus. - 2. `chunk` is a test set (held-out data), and author2doc and doc2author - corrsponding to this test set are provided. There must not be any new authors - passed to this method. `chunk_doc_idx` is not needed in this case. - To obtain the per-word bound, compute: + #. `chunk` is a subset of the training corpus, and `chunk_doc_idx` is provided, + indicating the indexes of the documents in the training corpus. + #. `chunk` is a test set (held-out data), and `author2doc` and `doc2author` corresponding to this test set + are provided. There must not be any new authors passed to this method, `chunk_doc_idx` is not needed + in this case. - >>> corpus_words = sum(cnt for document in corpus for _, cnt in document) - >>> model.bound(corpus, author2doc=author2doc, doc2author=doc2author) / corpus_words + Parameters + ---------- + chunk : iterable of list of (int, float) + Corpus in BoW format. + chunk_doc_idx : numpy.ndarray, optional + Assigns the value for document index. + subsample_ratio : float, optional + Used for calculation of word score for estimation of variational bound. + author2doc : dict of (str, list of int), optinal + A dictionary where keys are the names of authors and values are lists of documents that the author + contributes to. + doc2author : dict of (int, list of str), optional + A dictionary where the keys are document IDs and the values are lists of author names. - """ + Returns + ------- + float + Value of variational bound score. + """ # TODO: enable evaluation of documents with new authors. One could, for example, make it # possible to pass a list of documents to self.inference with no author dictionaries, # assuming all the documents correspond to one (unseen) author, learn the author's @@ -907,13 +1011,19 @@ def bound(self, chunk, chunk_doc_idx=None, subsample_ratio=1.0, author2doc=None, return total_score def get_document_topics(self, word_id, minimum_probability=None): - """ - This method overwrites `LdaModel.get_document_topics` and simply raises an - exception. `get_document_topics` is not valid for the author-topic model, - use `get_author_topics` instead. + """Override :meth:`~gensim.models.ldamodel.LdaModel.get_document_topics` and simply raises an exception. - """ + Warnings + -------- + This method invalid for model, use :meth:`~gensim.models.atmodel.AuthorTopicModel.get_author_topics` or + :meth:`~gensim.models.atmodel.AuthorTopicModel.get_new_author_topics` instead. + + Raises + ------ + NotImplementedError + Always. + """ raise NotImplementedError( 'Method "get_document_topics" is not valid for the author-topic model. ' 'Use the "get_author_topics" method.' @@ -927,7 +1037,7 @@ def get_new_author_topics(self, corpus, minimum_probability=None): Parameters ---------- - corpus : iterable of iterable of (int, int) + corpus : iterable of list of (int, float) Corpus in BoW format. minimum_probability : float, optional Ignore topics with probability below this value, if None - 1e-8 is used. @@ -992,16 +1102,47 @@ def rollback_new_author_chages(): return new_author_topics def get_author_topics(self, author_name, minimum_probability=None): - """ - Return topic distribution the given author. + """Get topic distribution the given author. - Input as as a list of - (topic_id, topic_probability) 2-tuples. - Ignore topics with very low probability (below `minimum_probability`). - Obtaining topic probabilities of each word, as in LDA (via `per_word_topics`), - is not supported. - """ + Parameters + ---------- + author_name : str + Name of the author for which the topic distribution needs to be estimated. + minimum_probability : float, optional + Sets the minimum probability value for showing the topics of a given author, topics with probability < + `minimum_probability` will be ignored. + + Returns + ------- + list of (int, float) + Topic distribution of an author. + Example + ------- + >>> from gensim.models import AuthorTopicModel + >>> from gensim.corpora import mmcorpus + >>> from gensim.test.utils import common_dictionary, datapath, temporary_file + + >>> author2doc = { + ... 'john': [0, 1, 2, 3, 4, 5, 6], + ... 'jane': [2, 3, 4, 5, 6, 7, 8], + ... 'jack': [0, 2, 4, 6, 8] + ... } + >>> + >>> corpus = mmcorpus.MmCorpus(datapath('testcorpus.mm')) + >>> + >>> with temporary_file("serialized") as s_path: + ... model = AuthorTopicModel( + ... corpus, author2doc=author2doc, id2word=common_dictionary, num_topics=4, + ... serialized=True, serialization_path=s_path + ... ) + ... + ... model.update(corpus, author2doc) # update the author-topic model with additional documents + >>> + >>> # construct vectors for authors + >>> author_vecs = [model.get_author_topics(author) for author in model.id2author.values()] + + """ author_id = self.author2id[author_name] if minimum_probability is None: @@ -1018,13 +1159,20 @@ def get_author_topics(self, author_name, minimum_probability=None): return author_topics def __getitem__(self, author_names, eps=None): - """ - Return topic distribution for input author as a list of - (topic_id, topic_probabiity) 2-tuples. + """Get topic distribution for input `author_names`. - Ingores topics with probaility less than `eps`. + Parameters + ---------- + author_names : {str, list of str} + Name(s) of the author for which the topic distribution needs to be estimated. + eps : float, optional + The minimum probability value for showing the topics of a given author, topics with probability < `eps` + will be ignored. - Do not call this method directly, instead use `model[author_names]`. + Returns + ------- + list of (int, float) **or** list of list of (int, float) + Topic distribution for the author(s), type depends on type of `author_names`. """ if isinstance(author_names, list):