{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "$$\n", "\\newcommand{\\Xs}{\\mathcal{X}}\n", "\\newcommand{\\Ys}{\\mathcal{Y}}\n", "\\newcommand{\\y}{\\mathbf{y}}\n", "\\newcommand{\\repr}{\\mathbf{f}}\n", "\\newcommand{\\repry}{\\mathbf{g}}\n", "\\newcommand{\\x}{\\mathbf{x}}\n", "\\newcommand{\\vocab}{V}\n", "\\newcommand{\\params}{\\boldsymbol{\\theta}}\n", "\\newcommand{\\param}{\\theta}\n", "\\DeclareMathOperator{\\perplexity}{PP}\n", "\\DeclareMathOperator{\\argmax}{argmax}\n", "\\DeclareMathOperator{\\argmin}{argmin}\n", "\\newcommand{\\train}{\\mathcal{D}}\n", "\\newcommand{\\counts}[2]{\\#_{#1}(#2) }\n", "\\newcommand{\\indi}{\\mathbb{I}}\n", "$$" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%%capture\n", "%load_ext autoreload\n", "%autoreload 2\n", "%matplotlib inline\n", "import sys\n", "sys.path.append(\"..\")\n", "import statnlpbook.util as util\n", "import matplotlib\n", "import matplotlib.pyplot as plt\n", "matplotlib.rcParams['figure.figsize'] = (10.0, 6.0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Structured Prediction\n", "\n", "In general there seems to be no emerging unified _theory of NLP_ to emerge, and most textbooks and courses explain NLP as \n", "\n", "> a collection of problems, techniques, ideas, frameworks, etc. that really are not tied together in any reasonable way other than the fact that they have to do with NLP.\n", ">\n", "> -- [Hal Daume](http://nlpers.blogspot.co.uk/2012/12/teaching-intro-grad-nlp.html)\n", "\n", "That's not to say though that they aren't cross-cutting patterns, general best practices and recipes that reoccur frequently. One such reoccurring pattern I found in many NLP papers and systems is what I like to refer to as the *[structured prediction](http://en.wikipedia.org/wiki/Structured_prediction) recipe*.\n", "\n", "The general goal we address with this recipe is the following. We like to, given some input structure $\\x \\in \\Xs$, predict a suitable output structure $\\y \\in \\Ys$. In the context of NLP $\\Xs$ may be the set of documents, and $\\Ys$ a set of document classes (e.g. sports and business). $\\Xs$ may also be the set of French sentences, and $\\Ys$ the set of English sentences. In this case each $\\y \\in \\Ys$ is a _structured_ object (hence the use of bold face). This structured **output** aspect of the problem has profound consequences on the methods used to address it (as opposed to structure in the input, which one can deal with relatively straight-forwardly). Generally we are also given some _training set_ $\\train$ which may contain input-output pairs $(\\x,\\y)_i$, but possibly also just input data (in unsupervised learning), annotated data but for a different task (multi-task, distant, weak supervision, etc.), or some mixture of it. \n", "\n", "\n", "With the above ingredients the recipe goes as follows: \n", "\n", " 1. Define a parametrized _model_ or _scoring function_ \\\\(s_\\params(\\x,\\y)\\\\) that measures the _match_ of a given \\\\(\\x\\\\) and \\\\(\\y\\\\). This model incorporates the background knowledge we have about the task, and usually involves _representations_ $\\repr_\\params(\\x)$ and $\\repry_\\params(\\y)$ that capture the character of the input and output relevant to the task---in the past representations were mostly handcrafted but with the advent of deep learning they tend to be learned from data now. The model is also controlled by a set of real-valued parameters \\\\(\\params\\\\).\n", " 1. _Learn_ the parameters \\\\(\\params\\\\) from the training data \\\\(\\train\\\\), ideally such that performance on the task of choice is optimized. This learning step usually involves some _continuous optimization problem_ that serves as a surrogate for the task performance we like to maximize. This problem defines a _loss function_ we have to minimise to optimise task performance. \n", " 1. Given an input \\\\(\\x\\\\) _predict_ the highest-scoring (and hence best-matching) output structure $$ \\y^* = \\argmax_{\\y\\in\\Ys} s(\\x,\\y) $$ to serve as the prediction of the model. Given that most of the structures we care about in NLP are discrete, this task usually involves some _discrete optimization problem_ and is important not just at _test time_ when the model is applied, but often also during training (as, intuitively, we like to train the model such that it _predicts well_). \n", " \n", "You will see examples of this recipe throughout the book, as well as frameworks and methods that make this recipe possible. It's worthwhile noting that good NLPers usually combine three skills in accordance with this recipe: 1. modelling, 2. continuous optimization and 3. discrete optimization. For the second and third some basic mathematical background is generally useful, for the first some understanding of the language phenomena you seek to model can be helpful. It's probably fair to say that modelling is the most important bit, and in practice this often shows through the fact that clever features (part of the model) beat clever optimization quite often. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## A Simple Example\n", "\n", "It is difficult to show this recipe in action without going into too much depth. We will do so by introducing a very simple structured prediction example that illustrates all aspects of the recipe. We consider the task of machine translation from English to German, and make the following (extremely unrealistic) assumptions:\n", "* There are only 4 target German sentences we care about. All English sentences we encounter can be translated to one of these 4 sentences. \n", "* The lengths of the source English and target German sentence are sufficient representations of the problem. That is, one can decide which German sentence is the correct translation be comparing its lengths to the length of the English sentence. \n", "\n", "It is important to remember that while this is a toy example, the main steps and ingredients are found in every statistical NLP application. \n", "\n", "### Input and Output Spaces\n", "First we define our input and output spaces $\\Xs$ and $\\Ys$. " ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
I ate an apple Ich aß einen Apfel
I ate a red apple Ich aß einen roten Apfel
" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x_space = ['I ate an apple', \n", " 'I ate a red apple', \n", " 'Yesterday I ate a red apple', \n", " 'Yesterday I ate a red apply with a friend']\n", "y_space = ['Ich aß einen Apfel',\n", " 'Ich aß einen roten Apfel',\n", " 'Gestern aß ich einen roten Apfel',\n", " 'Gestern aß ich einen roten Apfel mit einem Freund']\n", "data = list(zip(x_space,y_space))\n", "train = data[:2]\n", "test = data[2:]\n", "util.Table(train)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Model\n", "Modelling consists of two steps: finding suitable representations of $\\x$ and $\\y$, and measuring their compatibility using a scoring function. \n", "#### Representation\n", "We incorporate the assumptions that only sentence lengths matter when translating by defining $\\repr(\\x)=|\\x|$ and $\\repry(\\y)=|\\y|$ where $|\\cdot|$ counts the number of characters in the sentence." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def f(x):\n", " \"\"\"Calculate a representation of the input `x`.\"\"\"\n", " return len(x)\n", "def g(y):\n", " \"\"\"Calculate a representation of the output `y`.\"\"\"\n", " return len(y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Scoring Function\n", "The scoring function only cares about the difference in length between the sentences when assessing the compatibility between the English and German sentence. We assume that German words are always a little longer than English words, and hence we cannot directly compare lengths but must scale the English length linearly:\n", "\n", "$$\n", "s_\\param(\\x,\\y) = -| \\param \\repr(\\x) - \\repry(\\y)|\n", "$$\n", "\n", "The single parameter $\\param\\in \\mathbb{R}$ determines how the English length needs to be scaled to be close to its German counterpart. " ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
I ate an apple Ich aß einen Apfel 14 18 -4.0
I ate a red apple Ich aß einen roten Apfel 17 24 -7.0
Yesterday I ate a red apple Gestern aß ich einen roten Apfel 27 32 -5.0
Yesterday I ate a red apply with a friend Gestern aß ich einen roten Apfel mit einem Freund 41 49 -8.0
" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import math\n", "def s(theta,x,y):\n", " \"\"\"Measure the compatibility of sentences `x` and `y` using parameter `theta`\"\"\"\n", " return -abs(theta * f(x) - g(y))\n", "\n", "util.Table([(x,y,f(x),g(y),s(1.0,x,y)) for x,y in data])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Loss Function\n", "We want to _programmatically_ find out what the optimal $\\param$ is. We do so by providing a simple loss function: for every training instance we receive a penalty of $1$ if the highest scoring translation is not the correct one. We can formalise this as follows:\n", "\n", "$$\n", "l(\\param)=\\sum_{(\\x,\\y) \\in \\train} \\indi(\\y\\neq\\y'_{\\param})\n", "$$\n", "where $\\y'_{\\param} \\in \\Ys$ is the highest scoring German sentence \n", "$$\\y'_{\\param}=\\argmax_\\y s_\\param(\\x,\\y).$$\n", "\n", "In python this can be implemented as follows:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.0" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def loss(theta, data):\n", " \"\"\"Measure the total number of errors made when predicting with parameter `theta` on training set `data`\"\"\"\n", " total = 0.0\n", " for x,y in data:\n", " max_score = -math.inf\n", " result = None\n", " for y_guess in y_space:\n", " score = s(theta,x,y_guess)\n", " if score > max_score:\n", " result = y_guess\n", " max_score = score\n", " if result != y:\n", " total += 1.0\n", " return total\n", "\n", "loss(1.0, train)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Learning\n", "With the model and loss defined, we can now _learn_ the model parameters. Learning simply amounts to finding the parameter with minimal loss:\n", "\n", "$$\n", "\\param^* = \\argmin_{\\param} l(\\param) \n", "$$\n", "\n", "Let us focus our attention on $\\param \\in [0,2]$ and investigate the loss landscape." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" }, { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAl0AAAFpCAYAAACmgZ0NAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3X+Q3Hd93/HX6+72pDsj79pIEGJblmnUgmnAkKtJwFNM\nSIxMG4tMMh158sNQGHUSTJKmkxlIZuyO+SdtZppOJiRGkyiEFGyIE6dKa2I8MZQW12CZGP8CgxBO\nLJXWxmJXsnet3bt794/97nl9utOtpL3v93vfz/Mxc6Pd7/e7e5+P9s56+fN5fz8fR4QAAACwviaK\nbgAAAEAKCF0AAAA5IHQBAADkgNAFAACQA0IXAABADghdAAAAOSB0AQAA5IDQBQAAkANCFwAAQA4I\nXQAAADmYKroBK9m6dWvs2LGj6GYAAACs6cEHH/xeRGxb67pShq4dO3bo4MGDRTcDAABgTbb/fpTr\nmF4EAADIAaELAAAgB4QuAACAHBC6AAAAckDoAgAAyAGhCwAAIAeELgAAgBwQugAAAHJA6AIAAMjB\nmqHL9iW2P2/7cduP2f7VFa6x7d+zfc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_data" } ], "source": [ "import numpy as np\n", "thetas = np.linspace(0.0, 2.0, num=50)\n", "plt.plot(thetas, [loss(theta,train) for theta in thetas])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We notice that there exist a set of optimal parameters for which the loss is zero and we achieve perfect prediction accuracy on the training set. We can find one such optimal parameter by sheer brute force: " ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.2653061224489794" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "theta_star = thetas[np.argmin([loss(theta,train) for theta in thetas])]\n", "theta_star" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Prediction\n", "Now that we have a trained model we can use it to predict translations on a test set. This amounts to finding \n", "\n", "$$\\y'_{\\param}=\\argmax_\\y s_\\param(\\x,\\y).$$ \n", "\n", "Notice that we solved this problem already within the calculation of the loss. This is a common pattern in structured prediction: when minimising a training loss we often have to solve the prediction problem in the inner loop. In many cases this is the core bottleneck of computation. \n", "\n", "For completeness, we show how one can find the highest scoring German sentence $\\y$ for a given English sentence $\\x$ but note that this code can already be found the in loss code above. In practice one would try to reuse the prediction code below in the loss function above." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Yesterday I ate a red apple Gestern aß ich einen roten Apfel
Yesterday I ate a red apply with a friend Gestern aß ich einen roten Apfel mit einem Freund
" ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def predict(theta, x):\n", " \"\"\"Find the most compatible output sentence given the input sentence `x` and parameter `theta`\"\"\"\n", " max_score = -math.inf\n", " result = None\n", " for y_guess in y_space:\n", " score = s(theta,x,y_guess)\n", " if score > max_score:\n", " result = y_guess\n", " max_score = score\n", " return result\n", "\n", "# We can now predict the test set translation:\n", "util.Table([(x,predict(theta_star, x)) for x,_ in test])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### In Practice\n", "In practice one needs to extend the above approach along several axes to be useful:\n", "\n", "1. Usually the feature representations and scoring functions are more elaborate. They may involve several non-linear transformations of both input and output. \n", "\n", "1. The space of parameters is usually multi-dimensional (with possibly millions of dimensions). It is hence impossible to exhaustively search through this space as we have done above. Instead you will find numeric optimisation algorithms, often variants of Stochastic Gradient Descent. \n", "\n", "1. The size of the output space is often exponentional with respect to the input problem size. For example, in machine translation we have to search through *all* German sentences, not just 4. This means that we cannot exhaustively search this space either. Dynamic Programming, Greedy algorithms and integer linear programming are common approaches to solve this issue. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Learn to Predict\n", "\n", "The above recipe can also be described as \"learn how to score\". Here we learn a scoring function, and then find the highest scoring solution using a discrete search algorithm. An alternative approach is to learn a prediction or search *algorithm* directly, without a scoring function. In this setting, structured prediction is an instance of \"learning programs\" in the sense that a model predicts a sequence of actions or steps. We will see an instance of this approach in the [dependency parsing](/notebooks/chapters/Transition-based%20dependency%20parsing.ipynb) chapter. As we are learning to choose the right actions, this approach is tightly connected to reinforcement learning, in particular, [imitation learning](https://www.cs.cmu.edu/~sross1/publications/Ross-AIStats11-NoRegret.pdf). " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Background Reading\n", "\n", "* Noah Smith, [Linguistic Structure Prediction](http://www.cs.cmu.edu/~nasmith/LSP/). Notice that his book can be downloaded for free when logging in through UCL. " ] } ], "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.2" } }, "nbformat": 4, "nbformat_minor": 1 }