Skip to content

Commit

Permalink
Document gensim.models.bm25
Browse files Browse the repository at this point in the history
  • Loading branch information
Witiko committed Mar 31, 2022
1 parent be7a0e6 commit 53ec11f
Show file tree
Hide file tree
Showing 7 changed files with 248 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"The next question might be: just how exactly similar are those documents to each other?\nIs there a way to formalize the similarity, so that for a given input document, we can\norder some other set of documents according to their similarity? Similarity queries\nare covered in the next tutorial (`sphx_glr_auto_examples_core_run_similarity_queries.py`).\n\n\nAvailable transformations\n--------------------------\n\nGensim implements several popular Vector Space Model algorithms:\n\n* `Term Frequency * Inverse Document Frequency, Tf-Idf <http://en.wikipedia.org/wiki/Tf%E2%80%93idf>`_\n expects a bag-of-words (integer values) training corpus during initialization.\n During transformation, it will take a vector and return another vector of the\n same dimensionality, except that features which were rare in the training corpus\n will have their value increased.\n It therefore converts integer-valued vectors into real-valued ones, while leaving\n the number of dimensions intact. It can also optionally normalize the resulting\n vectors to (Euclidean) unit length.\n\n .. sourcecode:: pycon\n\n model = models.TfidfModel(corpus, normalize=True)\n\n* `Latent Semantic Indexing, LSI (or sometimes LSA) <http://en.wikipedia.org/wiki/Latent_semantic_indexing>`_\n transforms documents from either bag-of-words or (preferrably) TfIdf-weighted space into\n a latent space of a lower dimensionality. For the toy corpus above we used only\n 2 latent dimensions, but on real corpora, target dimensionality of 200--500 is recommended\n as a \"golden standard\" [1]_.\n\n .. sourcecode:: pycon\n\n model = models.LsiModel(tfidf_corpus, id2word=dictionary, num_topics=300)\n\n LSI training is unique in that we can continue \"training\" at any point, simply\n by providing more training documents. This is done by incremental updates to\n the underlying model, in a process called `online training`. Because of this feature, the\n input document stream may even be infinite -- just keep feeding LSI new documents\n as they arrive, while using the computed transformation model as read-only in the meanwhile!\n\n .. sourcecode:: pycon\n\n model.add_documents(another_tfidf_corpus) # now LSI has been trained on tfidf_corpus + another_tfidf_corpus\n lsi_vec = model[tfidf_vec] # convert some new document into the LSI space, without affecting the model\n\n model.add_documents(more_documents) # tfidf_corpus + another_tfidf_corpus + more_documents\n lsi_vec = model[tfidf_vec]\n\n See the :mod:`gensim.models.lsimodel` documentation for details on how to make\n LSI gradually \"forget\" old observations in infinite streams. If you want to get dirty,\n there are also parameters you can tweak that affect speed vs. memory footprint vs. numerical\n precision of the LSI algorithm.\n\n `gensim` uses a novel online incremental streamed distributed training algorithm (quite a mouthful!),\n which I published in [5]_. `gensim` also executes a stochastic multi-pass algorithm\n from Halko et al. [4]_ internally, to accelerate in-core part\n of the computations.\n See also `wiki` for further speed-ups by distributing the computation across\n a cluster of computers.\n\n* `Random Projections, RP <http://www.cis.hut.fi/ella/publications/randproj_kdd.pdf>`_ aim to\n reduce vector space dimensionality. This is a very efficient (both memory- and\n CPU-friendly) approach to approximating TfIdf distances between documents, by throwing in a little randomness.\n Recommended target dimensionality is again in the hundreds/thousands, depending on your dataset.\n\n .. sourcecode:: pycon\n\n model = models.RpModel(tfidf_corpus, num_topics=500)\n\n* `Latent Dirichlet Allocation, LDA <http://en.wikipedia.org/wiki/Latent_Dirichlet_allocation>`_\n is yet another transformation from bag-of-words counts into a topic space of lower\n dimensionality. LDA is a probabilistic extension of LSA (also called multinomial PCA),\n so LDA's topics can be interpreted as probability distributions over words. These distributions are,\n just like with LSA, inferred automatically from a training corpus. Documents\n are in turn interpreted as a (soft) mixture of these topics (again, just like with LSA).\n\n .. sourcecode:: pycon\n\n model = models.LdaModel(corpus, id2word=dictionary, num_topics=100)\n\n `gensim` uses a fast implementation of online LDA parameter estimation based on [2]_,\n modified to run in `distributed mode <distributed>` on a cluster of computers.\n\n* `Hierarchical Dirichlet Process, HDP <http://jmlr.csail.mit.edu/proceedings/papers/v15/wang11a/wang11a.pdf>`_\n is a non-parametric bayesian method (note the missing number of requested topics):\n\n .. sourcecode:: pycon\n\n model = models.HdpModel(corpus, id2word=dictionary)\n\n `gensim` uses a fast, online implementation based on [3]_.\n The HDP model is a new addition to `gensim`, and still rough around its academic edges -- use with care.\n\nAdding new :abbr:`VSM (Vector Space Model)` transformations (such as different weighting schemes) is rather trivial;\nsee the `apiref` or directly the `Python code <https://github.com/piskvorky/gensim/blob/develop/gensim/models/tfidfmodel.py>`_\nfor more info and examples.\n\nIt is worth repeating that these are all unique, **incremental** implementations,\nwhich do not require the whole training corpus to be present in main memory all at once.\nWith memory taken care of, I am now improving `distributed`,\nto improve CPU efficiency, too.\nIf you feel you could contribute by testing, providing use-cases or code, see the `Gensim Developer guide <https://github.com/RaRe-Technologies/gensim/wiki/Developer-page>`__.\n\nWhat Next?\n----------\n\nContinue on to the next tutorial on `sphx_glr_auto_examples_core_run_similarity_queries.py`.\n\nReferences\n----------\n\n.. [1] Bradford. 2008. An empirical study of required dimensionality for large-scale latent semantic indexing applications.\n\n.. [2] Hoffman, Blei, Bach. 2010. Online learning for Latent Dirichlet Allocation.\n\n.. [3] Wang, Paisley, Blei. 2011. Online variational inference for the hierarchical Dirichlet process.\n\n.. [4] Halko, Martinsson, Tropp. 2009. Finding structure with randomness.\n\n.. [5] \u0158eh\u016f\u0159ek. 2011. Subspace tracking for Latent Semantic Analysis.\n\n"
"The next question might be: just how exactly similar are those documents to each other?\nIs there a way to formalize the similarity, so that for a given input document, we can\norder some other set of documents according to their similarity? Similarity queries\nare covered in the next tutorial (`sphx_glr_auto_examples_core_run_similarity_queries.py`).\n\n\nAvailable transformations\n--------------------------\n\nGensim implements several popular Vector Space Model algorithms:\n\n* `Term Frequency * Inverse Document Frequency, Tf-Idf <http://en.wikipedia.org/wiki/Tf%E2%80%93idf>`_\n expects a bag-of-words (integer values) training corpus during initialization.\n During transformation, it will take a vector and return another vector of the\n same dimensionality, except that features which were rare in the training corpus\n will have their value increased.\n It therefore converts integer-valued vectors into real-valued ones, while leaving\n the number of dimensions intact. It can also optionally normalize the resulting\n vectors to (Euclidean) unit length.\n\n .. sourcecode:: pycon\n\n model = models.TfidfModel(corpus, normalize=True)\n\n* `Okapi Best Matching, Okapi BM25 <https://en.wikipedia.org/wiki/Okapi_BM25>`_\n expects a bag-of-words (integer values) training corpus during initialization.\n During transformation, it will take a vector and return another vector of the\n same dimensionality, except that features which were rare in the training corpus\n will have their value increased. It therefore converts integer-valued\n vectors into real-valued ones, while leaving the number of dimensions intact.\n\n Okapi BM25 is the standard ranking function used by search engines to estimate\n the relevance of documents to a given search query.\n\n .. sourcecode:: pycon\n\n model = models.OkapiBM25Model(corpus)\n\n* `Latent Semantic Indexing, LSI (or sometimes LSA) <http://en.wikipedia.org/wiki/Latent_semantic_indexing>`_\n transforms documents from either bag-of-words or (preferrably) TfIdf-weighted space into\n a latent space of a lower dimensionality. For the toy corpus above we used only\n 2 latent dimensions, but on real corpora, target dimensionality of 200--500 is recommended\n as a \"golden standard\" [1]_.\n\n .. sourcecode:: pycon\n\n model = models.LsiModel(tfidf_corpus, id2word=dictionary, num_topics=300)\n\n LSI training is unique in that we can continue \"training\" at any point, simply\n by providing more training documents. This is done by incremental updates to\n the underlying model, in a process called `online training`. Because of this feature, the\n input document stream may even be infinite -- just keep feeding LSI new documents\n as they arrive, while using the computed transformation model as read-only in the meanwhile!\n\n .. sourcecode:: pycon\n\n model.add_documents(another_tfidf_corpus) # now LSI has been trained on tfidf_corpus + another_tfidf_corpus\n lsi_vec = model[tfidf_vec] # convert some new document into the LSI space, without affecting the model\n\n model.add_documents(more_documents) # tfidf_corpus + another_tfidf_corpus + more_documents\n lsi_vec = model[tfidf_vec]\n\n See the :mod:`gensim.models.lsimodel` documentation for details on how to make\n LSI gradually \"forget\" old observations in infinite streams. If you want to get dirty,\n there are also parameters you can tweak that affect speed vs. memory footprint vs. numerical\n precision of the LSI algorithm.\n\n `gensim` uses a novel online incremental streamed distributed training algorithm (quite a mouthful!),\n which I published in [5]_. `gensim` also executes a stochastic multi-pass algorithm\n from Halko et al. [4]_ internally, to accelerate in-core part\n of the computations.\n See also `wiki` for further speed-ups by distributing the computation across\n a cluster of computers.\n\n* `Random Projections, RP <http://www.cis.hut.fi/ella/publications/randproj_kdd.pdf>`_ aim to\n reduce vector space dimensionality. This is a very efficient (both memory- and\n CPU-friendly) approach to approximating TfIdf distances between documents, by throwing in a little randomness.\n Recommended target dimensionality is again in the hundreds/thousands, depending on your dataset.\n\n .. sourcecode:: pycon\n\n model = models.RpModel(tfidf_corpus, num_topics=500)\n\n* `Latent Dirichlet Allocation, LDA <http://en.wikipedia.org/wiki/Latent_Dirichlet_allocation>`_\n is yet another transformation from bag-of-words counts into a topic space of lower\n dimensionality. LDA is a probabilistic extension of LSA (also called multinomial PCA),\n so LDA's topics can be interpreted as probability distributions over words. These distributions are,\n just like with LSA, inferred automatically from a training corpus. Documents\n are in turn interpreted as a (soft) mixture of these topics (again, just like with LSA).\n\n .. sourcecode:: pycon\n\n model = models.LdaModel(corpus, id2word=dictionary, num_topics=100)\n\n `gensim` uses a fast implementation of online LDA parameter estimation based on [2]_,\n modified to run in `distributed mode <distributed>` on a cluster of computers.\n\n* `Hierarchical Dirichlet Process, HDP <http://jmlr.csail.mit.edu/proceedings/papers/v15/wang11a/wang11a.pdf>`_\n is a non-parametric bayesian method (note the missing number of requested topics):\n\n .. sourcecode:: pycon\n\n model = models.HdpModel(corpus, id2word=dictionary)\n\n `gensim` uses a fast, online implementation based on [3]_.\n The HDP model is a new addition to `gensim`, and still rough around its academic edges -- use with care.\n\nAdding new :abbr:`VSM (Vector Space Model)` transformations (such as different weighting schemes) is rather trivial;\nsee the `apiref` or directly the `Python code <https://github.com/piskvorky/gensim/blob/develop/gensim/models/tfidfmodel.py>`_\nfor more info and examples.\n\nIt is worth repeating that these are all unique, **incremental** implementations,\nwhich do not require the whole training corpus to be present in main memory all at once.\nWith memory taken care of, I am now improving `distributed`,\nto improve CPU efficiency, too.\nIf you feel you could contribute by testing, providing use-cases or code, see the `Gensim Developer guide <https://github.com/RaRe-Technologies/gensim/wiki/Developer-page>`__.\n\nWhat Next?\n----------\n\nContinue on to the next tutorial on `sphx_glr_auto_examples_core_run_similarity_queries.py`.\n\nReferences\n----------\n\n.. [1] Bradford. 2008. An empirical study of required dimensionality for large-scale latent semantic indexing applications.\n\n.. [2] Hoffman, Blei, Bach. 2010. Online learning for Latent Dirichlet Allocation.\n\n.. [3] Wang, Paisley, Blei. 2011. Online variational inference for the hierarchical Dirichlet process.\n\n.. [4] Halko, Martinsson, Tropp. 2009. Finding structure with randomness.\n\n.. [5] \u0158eh\u016f\u0159ek. 2011. Subspace tracking for Latent Semantic Analysis.\n\n"
]
},
{
Expand Down Expand Up @@ -213,4 +213,4 @@
},
"nbformat": 4,
"nbformat_minor": 0
}
}
14 changes: 14 additions & 0 deletions docs/src/auto_examples/core/run_topics_and_transformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,20 @@
#
# model = models.TfidfModel(corpus, normalize=True)
#
# * `Okapi Best Matching, Okapi BM25 <https://en.wikipedia.org/wiki/Okapi_BM25>`_
# expects a bag-of-words (integer values) training corpus during initialization.
# During transformation, it will take a vector and return another vector of the
# same dimensionality, except that features which were rare in the training corpus
# will have their value increased. It therefore converts integer-valued
# vectors into real-valued ones, while leaving the number of dimensions intact.
#
# Okapi BM25 is the standard ranking function used by search engines to estimate
# the relevance of documents to a given search query.
#
# .. sourcecode:: pycon
#
# model = models.OkapiBM25Model(corpus)
#
# * `Latent Semantic Indexing, LSI (or sometimes LSA) <http://en.wikipedia.org/wiki/Latent_semantic_indexing>`_
# transforms documents from either bag-of-words or (preferrably) TfIdf-weighted space into
# a latent space of a lower dimensionality. For the toy corpus above we used only
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
f49c3821bbacdeefdf3945d5dcb5ad01
226db24f9e807e4bbd2a6ef280a75510
14 changes: 14 additions & 0 deletions docs/src/auto_examples/core/run_topics_and_transformations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,20 @@ Gensim implements several popular Vector Space Model algorithms:

model = models.TfidfModel(corpus, normalize=True)

* `Okapi Best Matching, Okapi BM25 <https://en.wikipedia.org/wiki/Okapi_BM25>`_
expects a bag-of-words (integer values) training corpus during initialization.
During transformation, it will take a vector and return another vector of the
same dimensionality, except that features which were rare in the training corpus
will have their value increased. It therefore converts integer-valued
vectors into real-valued ones, while leaving the number of dimensions intact.

Okapi BM25 is the standard ranking function used by search engines to estimate
the relevance of documents to a given search query.

.. sourcecode:: pycon

model = models.OkapiBM25Model(corpus)

* `Latent Semantic Indexing, LSI (or sometimes LSA) <http://en.wikipedia.org/wiki/Latent_semantic_indexing>`_
transforms documents from either bag-of-words or (preferrably) TfIdf-weighted space into
a latent space of a lower dimensionality. For the toy corpus above we used only
Expand Down
14 changes: 14 additions & 0 deletions docs/src/gallery/core/run_topics_and_transformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,20 @@
#
# model = models.TfidfModel(corpus, normalize=True)
#
# * `Okapi Best Matching, Okapi BM25 <https://en.wikipedia.org/wiki/Okapi_BM25>`_
# expects a bag-of-words (integer values) training corpus during initialization.
# During transformation, it will take a vector and return another vector of the
# same dimensionality, except that features which were rare in the training corpus
# will have their value increased. It therefore converts integer-valued
# vectors into real-valued ones, while leaving the number of dimensions intact.
#
# Okapi BM25 is the standard ranking function used by search engines to estimate
# the relevance of documents to a given search query.
#
# .. sourcecode:: pycon
#
# model = models.OkapiBM25Model(corpus)
#
# * `Latent Semantic Indexing, LSI (or sometimes LSA) <http://en.wikipedia.org/wiki/Latent_semantic_indexing>`_
# transforms documents from either bag-of-words or (preferrably) TfIdf-weighted space into
# a latent space of a lower dimensionality. For the toy corpus above we used only
Expand Down
Loading

0 comments on commit 53ec11f

Please sign in to comment.