{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\nFastText Model\n==============\n\nIntroduces Gensim's fastText model and demonstrates its use on the Lee Corpus.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import logging\nlogging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here, we'll learn to work with fastText library for training word-embedding\nmodels, saving & loading them and performing similarity operations & vector\nlookups analogous to Word2Vec.\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When to use fastText?\n---------------------\n\nThe main principle behind `fastText `_ is that the\nmorphological structure of a word carries important information about the meaning of the word.\nSuch structure is not taken into account by traditional word embeddings like Word2Vec, which\ntrain a unique word embedding for every individual word.\nThis is especially significant for morphologically rich languages (German, Turkish) in which a\nsingle word can have a large number of morphological forms, each of which might occur rarely,\nthus making it hard to train good word embeddings.\n\n\nfastText attempts to solve this by treating each word as the aggregation of its subwords.\nFor the sake of simplicity and language-independence, subwords are taken to be the character ngrams\nof the word. The vector for a word is simply taken to be the sum of all vectors of its component char-ngrams.\n\n\nAccording to a detailed comparison of Word2Vec and fastText in\n`this notebook `__,\nfastText does significantly better on syntactic tasks as compared to the original Word2Vec,\nespecially when the size of the training corpus is small. Word2Vec slightly outperforms fastText\non semantic tasks though. The differences grow smaller as the size of the training corpus increases.\n\n\nfastText can obtain vectors even for out-of-vocabulary (OOV) words, by summing up vectors for its\ncomponent char-ngrams, provided at least one of the char-ngrams was present in the training data.\n\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Training models\n---------------\n\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For the following examples, we'll use the Lee Corpus (which you already have if you've installed Gensim) for training our model.\n\n\n\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from pprint import pprint as print\nfrom gensim.models.fasttext import FastText\nfrom gensim.test.utils import datapath\n\n# Set file names for train and test data\ncorpus_file = datapath('lee_background.cor')\n\nmodel = FastText(vector_size=100)\n\n# build the vocabulary\nmodel.build_vocab(corpus_file=corpus_file)\n\n# train the model\nmodel.train(\n corpus_file=corpus_file, epochs=model.epochs,\n total_examples=model.corpus_count, total_words=model.corpus_total_words,\n)\n\nprint(model)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Training hyperparameters\n^^^^^^^^^^^^^^^^^^^^^^^^\n\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Hyperparameters for training the model follow the same pattern as Word2Vec. FastText supports the following parameters from the original word2vec:\n\n- model: Training architecture. Allowed values: `cbow`, `skipgram` (Default `cbow`)\n- vector_size: Dimensionality of vector embeddings to be learnt (Default 100)\n- alpha: Initial learning rate (Default 0.025)\n- window: Context window size (Default 5)\n- min_count: Ignore words with number of occurrences below this (Default 5)\n- loss: Training objective. Allowed values: `ns`, `hs`, `softmax` (Default `ns`)\n- sample: Threshold for downsampling higher-frequency words (Default 0.001)\n- negative: Number of negative words to sample, for `ns` (Default 5)\n- epochs: Number of epochs (Default 5)\n- sorted_vocab: Sort vocab by descending frequency (Default 1)\n- threads: Number of threads to use (Default 12)\n\n\nIn addition, fastText has three additional parameters:\n\n- min_n: min length of char ngrams (Default 3)\n- max_n: max length of char ngrams (Default 6)\n- bucket: number of buckets used for hashing ngrams (Default 2000000)\n\n\nParameters ``min_n`` and ``max_n`` control the lengths of character ngrams that each word is broken down into while training and looking up embeddings. If ``max_n`` is set to 0, or to be lesser than ``min_n``\\ , no character ngrams are used, and the model effectively reduces to Word2Vec.\n\n\n\nTo bound the memory requirements of the model being trained, a hashing function is used that maps ngrams to integers in 1 to K. For hashing these character sequences, the `Fowler-Noll-Vo hashing function `_ (FNV-1a variant) is employed.\n\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Note:** You can continue to train your model while using Gensim's native implementation of fastText.\n\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Saving/loading models\n---------------------\n\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Models can be saved and loaded via the ``load`` and ``save`` methods, just like\nany other model in Gensim.\n\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Save a model trained via Gensim's fastText implementation to temp.\nimport tempfile\nimport os\nwith tempfile.NamedTemporaryFile(prefix='saved_model_gensim-', delete=False) as tmp:\n model.save(tmp.name, separately=[])\n\n# Load back the same model.\nloaded_model = FastText.load(tmp.name)\nprint(loaded_model)\n\nos.unlink(tmp.name) # demonstration complete, don't need the temp file anymore" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The ``save_word2vec_format`` is also available for fastText models, but will\ncause all vectors for ngrams to be lost.\nAs a result, a model loaded in this way will behave as a regular word2vec model.\n\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Word vector lookup\n------------------\n\n\nAll information necessary for looking up fastText words (incl. OOV words) is\ncontained in its ``model.wv`` attribute.\n\nIf you don't need to continue training your model, you can export & save this `.wv`\nattribute and discard `model`, to save space and RAM.\n\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "wv = model.wv\nprint(wv)\n\n#\n# FastText models support vector lookups for out-of-vocabulary words by summing up character ngrams belonging to the word.\n#\nprint('night' in wv.key_to_index)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print('nights' in wv.key_to_index)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(wv['night'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(wv['nights'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Similarity operations\n---------------------\n\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Similarity operations work the same way as word2vec. **Out-of-vocabulary words can also be used, provided they have at least one character ngram present in the training data.**\n\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(\"nights\" in wv.key_to_index)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(\"night\" in wv.key_to_index)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(wv.similarity(\"night\", \"nights\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Syntactically similar words generally have high similarity in fastText models, since a large number of the component char-ngrams will be the same. As a result, fastText generally does better at syntactic tasks than Word2Vec. A detailed comparison is provided `here `_.\n\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Other similarity operations\n^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\nThe example training corpus is a toy corpus, results are not expected to be good, for proof-of-concept only\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(wv.most_similar(\"nights\"))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(wv.n_similarity(['sushi', 'shop'], ['japanese', 'restaurant']))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(wv.doesnt_match(\"breakfast cereal dinner lunch\".split()))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(wv.most_similar(positive=['baghdad', 'england'], negative=['london']))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(wv.evaluate_word_analogies(datapath('questions-words.txt')))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Word Movers distance\n^^^^^^^^^^^^^^^^^^^^\n\nYou'll need the optional ``pyemd`` library for this section, ``pip install pyemd``.\n\nLet's start with two sentences:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "sentence_obama = 'Obama speaks to the media in Illinois'.lower().split()\nsentence_president = 'The president greets the press in Chicago'.lower().split()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Remove their stopwords.\n\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from gensim.parsing.preprocessing import STOPWORDS\nsentence_obama = [w for w in sentence_obama if w not in STOPWORDS]\nsentence_president = [w for w in sentence_president if w not in STOPWORDS]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Compute the Word Movers Distance between the two sentences.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "distance = wv.wmdistance(sentence_obama, sentence_president)\nprint(f\"Word Movers Distance is {distance} (lower means closer)\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "That's all! You've made it to the end of this tutorial.\n\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimg = mpimg.imread('fasttext-logo-color-web.png')\nimgplot = plt.imshow(img)\n_ = plt.axis('off')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.5" } }, "nbformat": 4, "nbformat_minor": 0 }