{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Considering the Environment \n",
"\n",
"So far, we have worked through a number of hands-on implementations\n",
"fitting machine learning models to a variety of datasets.\n",
"And yet, until now we skated over the matter \n",
"of where are data comes from in the first place,\n",
"and what we plan to ultimately *do* with the outputs from our models.\n",
"Too often in the practice of machine learning, \n",
"developers rush ahead with the development of models\n",
"tossing these fundamental considerations aside.\n",
"\n",
"Many failed machine learning deployments can be traced back to this situation. \n",
"Sometimes the model does well as evaluated by test accuracy\n",
"only to fail catastrophically in the real world\n",
"when the distribution of data suddenly shifts.\n",
"More insidiously, sometimes the very deployment of a model\n",
"can be the catalyst which perturbs the data distribution.\n",
"Say for example that we trained a model to predict loan defaults,\n",
"finding that the choice of footware was associated with risk of default \n",
"(Oxfords indicate repayment, sneakers indicate default).\n",
"We might be inclined to thereafter grant loans \n",
"to all applicants wearing Oxfords\n",
"and to deny all appplicants wearing sneakers.\n",
"But our ill-conceived leap from pattern recognition to decision-making \n",
"and our failure to think critically about the environment\n",
"might have disastrous consequences. \n",
"For starters, as soon as we began making decisions based on footware,\n",
"customers would catch on and change their behavior. \n",
"Before long, all applicants would be wearing Oxfords, \n",
"and yet there would be no coinciding improvement in credit-worthiness.\n",
"Think about this deeply because similar issues abound in the application of machine learning: by introducing our model-based decisions to the environnment,\n",
"we might break the model.\n",
"\n",
"In this chapter, we describe some common concerns\n",
"and aim to get you started acquiring the critical thinking \n",
"that you will need in order to detect these situations early,\n",
"mitigate the damage, and use machine learning responsibly. \n",
"Some of the solutions are simple (ask for the 'right' data) \n",
"some are technically difficult (implement a reinforcement learning system),\n",
"and others require that we entre the realm of philosophy\n",
"and grapple with difficult questions concerning ethics and informed consent.\n",
"\n",
"\n",
"## Distribution Shift\n",
"\n",
"To begin, we return to the observartional setting,\n",
"putting aside for now the impacts of our actions \n",
"on the environment.\n",
"In the following sections, we take a deeper look \n",
"at the various ways that data distributions might shift,\n",
"and what might be done to salvage model performance. \n",
"From the outset, we should warn that if \n",
"the data-generating distribution $p(\\mathbf{x},y)$\n",
"can shift in arbitrary ways at any point in time,\n",
"then learning a robust classifier is impossible.\n",
"In the most pathological case, if the label defnitions themselves\n",
"can change at a moments notice: if suddenly\n",
"what we called \"cats\" are now dogs\n",
"and what we previously called \"dogs\" are now in fact cats,\n",
"without any perceptible change in the distribution of inputs $p(\\mathbf{x})$,\n",
"then there is nothing we could do to detect the change \n",
"or to correct our classifier at test time.\n",
"Fortunately, under some restricted assumptions \n",
"on the ways our data might change in the future,\n",
"principled algorithms can detect shift and possibly even\n",
"adapt, achieving higher accuracy \n",
"than if we naively continued to rely on our original classifer. \n",
"\n",
"\n",
"\n",
"\n",
"### Covariate Shift\n",
"\n",
"One of the best-studied forms of distribution shift is *covariate shift*.\n",
"Here we assume that although the distirbution of inputs may change over time,\n",
"the labeling function, i.e., the conditional distribution $p(y|\\mathbf{x})$\n",
"does not change. \n",
"While this problem is easy to understand \n",
"its also easy to overlook it in practice. \n",
"Consider the challenge of distinguishing cats and dogs. \n",
"Our training data consists of images of the following kind:\n",
"\n",
"|cat|cat|dog|dog|\n",
"|:---------------:|:---------------:|:---------------:|:---------------:|\n",
"|![](../img/cat3.jpg)|![](../img/cat2.jpg)|![](../img/dog1.jpg)|![](../img/dog2.jpg)|\n",
"\n",
"At test time we are asked to classify the following images:\n",
"\n",
"|cat|cat|dog|dog|\n",
"|:---------------:|:---------------:|:---------------:|:---------------:|\n",
"|![](../img/cat-cartoon1.png)|![](../img/cat-cartoon2.png)|![](../img/dog-cartoon1.png)|![](../img/dog-cartoon2.png)|\n",
"\n",
"Obviously this is unlikely to work well. \n",
"The training set consists of photos, \n",
"while the test set contains only cartoons. \n",
"The colors aren't even realistic. \n",
"Training on a dataset that looks substantially different from the test set \n",
"without some plan for how to adapt to the new domain is a bad idea. \n",
"Unfortunately, this is a very common pitfall. \n",
"Statisticians call this *covariate shift*\n",
"because the root of the problem owed to \n",
"a shift in the distribution of features (i.e., of *covariates*).\n",
"Mathematically, we could say that $p(\\mathbf{x})$ changes \n",
"but that $p(y|\\mathbf{x})$ remains unchanged.\n",
"Although its usefulness is not restricted to this setting,\n",
"when we believe $\\mathbf{x}$ causes $y$, covariate shift is usually \n",
"the right assumption to be working with.\n",
"\n",
"### Label Shift\n",
"\n",
"The converse problem emerges when we believe that what drives the shift\n",
"is a change in the marginal distribution over the labels $p(y)$\n",
"but that the class-conditional distributions are invariant $p(\\mathbf{x}|y)$.\n",
"Label shift is a reasonable assumption to make \n",
"when we beleive that $y$ causes $\\mathbf{x}$. \n",
"For example, commonly we want to predict a diagnosis given its manifestations.\n",
"In this case we beleive that the diagnosis causes the manifestations,\n",
"i.e., diseases cause symptoms.\n",
"Sometimes the label shift and covariate shift assumptions \n",
"can hold simultaneously.\n",
"For example, when the true labeling function is deterministic and unchanging,\n",
"then covariate shift will always hold, \n",
"including if label shift holds too.\n",
"Interestingly, when we expect both label shift and covariate shift hold,\n",
"it's often advantageous to work with the methods \n",
"that flow from the label shift assumption.\n",
"That's because these methods tend to involve manipulating objects \n",
"that look like the label, \n",
"which (in deep learning) tends to be comparatively easy\n",
"compared to working with the objects that look like the input,\n",
"which tends (in deep learning) to be a high-dimensional object.\n",
"\n",
"\n",
"\n",
"### Concept Shift\n",
"\n",
"One more related problem arises in *concept shift*, \n",
"the situation in which the very label definitions change. \n",
"This sounds weird—after all, a *cat* is a *cat*.\n",
"Indeed the definition of a cat might not change, \n",
"but can we say the same about soft drinks?\n",
"It turns out that if we navigate aroudn the United States,\n",
"shifting the source of our data by geography,\n",
"we'll find considerable concept shift regaring \n",
"the definition of even this simple term:\n",
"\n",
"![](../img/popvssoda.png)\n",
"\n",
"If we were to build a machine translation system, \n",
"the distribution $p(y|x)$ might be different \n",
"depending on our location. \n",
"This problem can be tricky to spot. \n",
"A saving grace is that often the $p(y|x)$ only shifts gradually. \n",
"Before we go into further detail and discuss remedies, \n",
"we can discuss a number of situations where covariate and concept shift \n",
"may not be so obvious.\n",
"\n",
"\n",
"\n",
"### Examples\n",
"\n",
"#### Medical Diagnostics\n",
"\n",
"Imagine that you want to design an algorithm to detect cancer.\n",
"You collect data from healthy and sick people and you train your algorithm. \n",
"It works fine, giving you high accuracy \n",
"and you conclude that you’re ready\n",
"for a successful career in medical diagnostics. \n",
"Not so fast...\n",
"\n",
"Many things could go wrong. \n",
"In particular, the distributions that you work with \n",
"for training and those that you encounter in the wild \n",
"might differ considerably.\n",
"This happened to an unfortunate startup,\n",
"that Alex had the opportunity to consult for many years ago. \n",
"They were developing a blood test for a disease \n",
"that affects mainly older men and they’d managed to obtain \n",
"a fair amount of blood samples from patients. \n",
"It is considerably more difficult, though, \n",
"to obtain blood samples from healthy men (mainly for ethical reasons).\n",
"To compensate for that, they asked a large number of students on campus \n",
"to donate blood and they performed their test. \n",
"Then they asked me whether I could help them \n",
"build a classifier to detect the disease.\n",
"I told them that it would be very easy to distinguish \n",
"between both datasets with near-perfect accuracy.\n",
"After all, the test subjects differed in age, hormone levels, \n",
"physical activity, diet, alcohol consumption, \n",
"and many more factors unrelated to the disease. \n",
"This was unlikely to be the case with real patients: \n",
"Their sampling procedure made it likely that \n",
"an extreme case of covariate shift would arise\n",
"between the *source* and *target* distributions,\n",
"and at that, one that could not be corrected by conventional means. \n",
"In other words, training and test data were so different \n",
"that nothing useful could be done \n",
"and they had wasted significant amounts of money.\n",
"\n",
"#### Self Driving Cars\n",
"\n",
"Say a company wanted to build a machine learning system for self-driving cars. \n",
"One of the key components is a roadside detector. \n",
"Since real annotated data is expensive to get, \n",
"they had the (smart and questionable) idea \n",
"to use synthetic data from a game rendering engine \n",
"as additional training data. \n",
"This worked really well on 'test data' drawn from the rendering engine. \n",
"Alas, inside a real car it was a disaster. \n",
"As it turned out, the roadside had been rendered \n",
"with a very simplistic texture. \n",
"More importantly, *all* the roadside had been rendered \n",
"with the *same* texture and the roadside detector \n",
"learned about this 'feature' very quickly.\n",
"\n",
"A similar thing happened to the US Army \n",
"when they first tried to detect tanks in the forest. \n",
"They took aerial photographs of the forest without tanks, \n",
"then drove the tanks into the forest and took another set of pictures. \n",
"The so-trained classifier worked 'perfectly'. \n",
"Unfortunately, all it had learned was to distinguish trees \n",
"with shadows from trees without shadows—the \n",
"first set of pictures was taken in the early morning, the second one at noon.\n",
"\n",
"#### Nonstationary distributions\n",
"\n",
"A much more subtle situation arises when the distribution changes slowly\n",
"and the model is not updated adequately. \n",
"Here are some typical cases:\n",
"\n",
"* We train a computational advertising model and then fail to update it frequently (e.g. we forget to incorporate that an obscure new device called an iPad was just launched).\n",
"* We build a spam filter. It works well at detecting all spam that we've seen so far. But then the spammers wisen up and craft new messages that look unlike anything we've seen before.\n",
"* We build a product recommendation system. It works throughout the winter... but then it keeps on recommending Santa hats long after Christmas.\n",
"\n",
"#### More Anecdotes\n",
"\n",
"* We build a face detector. It works well on all benchmarks. Unfortunately it fails on test data - the offending examples are close-ups where the face fills the entire image (no such data was in the training set).\n",
"* We build a web search engine for the USA market and want to deploy it in the UK.\n",
"* We train an image classifier by compiling a large dataset where each among a large set of classes is equally represented in the dataset, say 1000 categories, represented by 1000 images each. Then we deploy the system in the real world, where the actual label distirbution of photographs is decidedly non-uniform.\n",
"\n",
"In short, there are many cases where training and test distributions \n",
"$p(\\mathbf{x}, y)$ are different. \n",
"In some cases, we get lucky and the models work \n",
"despite covariate, label, or concept shift. \n",
"In other cases, we can do better by employing \n",
"principled strategies to cope with the shift. \n",
"The remainder of this section grows considerably more technical. \n",
"The impatient reader could continue on to the next section \n",
"as this material is not prerequisite to subsequent concepts.\n",
"\n",
"### Covariate Shift Correction\n",
"\n",
"Assume that we want to estimate some dependency $p(y|\\mathbf{x})$ \n",
"for which we have labeled data $(\\mathbf{x}_i,y_i)$. \n",
"Unfortunately, the observations $x_i$ are drawn \n",
"from some *target* distribution $q(\\mathbf{x})$ \n",
"rather than the *source* distribution $p(\\mathbf{x})$. \n",
"To make progress, we need to reflect about what exactly \n",
"is happening during training: \n",
"we iterate over training data and associated labels \n",
"$\\{(\\mathbf{x}_1, y_1), \\ldots (\\mathbf{x}_n, y_n)\\}$\n",
"and update the weight vectors of the model after every minibatch.\n",
"We sometimes additionally apply some penalty to the parameters, \n",
"using weight decay, dropout, or some other related technique. \n",
"This means that we largely minimize the loss on the training.\n",
"\n",
"$$\n",
"\\mathop{\\mathrm{minimize}}_w \\frac{1}{n} \\sum_{i=1}^n l(x_i, y_i, f(x_i)) + \\mathrm{some~penalty}(w)\n",
"$$\n",
"\n",
"Statisticians call the first term an *empirical average*, \n",
"i.e., an average computed over the data drawn from $p(x) p(y|x)$. \n",
"If the data is drawn from the 'wrong' distribution $q$, \n",
"we can correct for that by using the following simple identity:\n",
"\n",
"$$\n",
"\\begin{aligned}\n",
"\\int p(\\mathbf{x}) f(\\mathbf{x}) dx & = \\int p(\\mathbf{x}) f(\\mathbf{x}) \\frac{q(\\mathbf{x})}{p(\\mathbf{x})} dx \\\\\n",
"& = \\int q(\\mathbf{x}) f(\\mathbf{x}) \\frac{p(\\mathbf{x})}{q(\\mathbf{x})} dx\n",
"\\end{aligned}\n",
"$$\n",
"\n",
"In other words, we need to re-weight each instance \n",
"by the ratio of probabilities \n",
"that it would have been drawn from the correct distribution \n",
"$\\beta(\\mathbf{x}) := p(\\mathbf{x})/q(\\mathbf{x})$. \n",
"Alas, we do not know that ratio, \n",
"so before we can do anything useful we need to estimate it. \n",
"Many methods are available, \n",
"including some fancy operator-theoretic approaches\n",
"that attempt to recalibrate the expectation operator directly \n",
"using a minimum-norm or a maximum entropy principle. \n",
"Note that for any such approach, we need samples \n",
"drawn from both distributions—the 'true' $p$, e.g.,\n",
"by access to training data, and the one used \n",
"for generating the training set $q$ (the latter is trivially available).\n",
"Note however, that we only need samples $\\mathbf{x} \\sim q(\\mathbf{x})$;\n",
"we do not to access labels labels $y \\sim q(y)$.\n",
"\n",
"In this case, there exists a very effective approach \n",
"that will give almost as good results: logistic regression. \n",
"This is all that is needed to compute estimate probability ratios.\n",
"We learn a classifier to distinguish between data drawn from $p(\\mathbf{x})$ \n",
"and data drawn from $q(x)$. \n",
"If it is impossible to distinguish between the two distributions \n",
"then it means that the associated instances are equally likely \n",
"to come from either one of the two distributions. \n",
"On the other hand, any instances that can be well discriminated \n",
"should be significantly over/underweighted accordingly. \n",
"For simplicity’s sake assume that we have an equal number of instances \n",
"from both distributions, denoted by $\\mathbf{x}_i \\sim p(\\mathbf{x})$ \n",
"and $\\mathbf{x}_i′ \\sim q(\\mathbf{x})$ respectively. \n",
"Now denote by $z_i$ labels which are 1 \n",
"for data drawn from $p$ and -1 for data drawn from $q$. \n",
"Then the probability in a mixed dataset is given by\n",
"\n",
"$$p(z=1|\\mathbf{x}) = \\frac{p(\\mathbf{x})}{p(\\mathbf{x})+q(\\mathbf{x})} \\text{ and hence } \\frac{p(z=1|\\mathbf{x})}{p(z=-1|\\mathbf{x})} = \\frac{p(\\mathbf{x})}{q(\\mathbf{x})}$$\n",
"\n",
"Hence, if we use a logistic regression approach where $p(z=1|\\mathbf{x})=\\frac{1}{1+\\exp(−f(\\mathbf{x}))}$ it follows that\n",
"\n",
"$$\n",
"\\beta(\\mathbf{x}) = \\frac{1/(1 + \\exp(-f(\\mathbf{x})))}{\\exp(-f(\\mathbf{x})/(1 + \\exp(-f(\\mathbf{x})))} = \\exp(f(\\mathbf{x}))\n",
"$$\n",
"\n",
"As a result, we need to solve two problems: \n",
"first one to distinguish between data drawn from both distributions, \n",
"and then a reweighted minimization problem \n",
"where we weigh terms by $\\beta$, e.g. via the head gradients. \n",
"Here's a prototypical algorithm for that purpose \n",
"which uses an unlabeled training set $X$ and test set $Z$:\n",
"\n",
"1. Generate training set with $\\{(\\mathbf{x}_i, -1) ... (\\mathbf{z}_j, 1)\\}$\n",
"1. Train binary classifier using logistic regression to get function $f$\n",
"1. Weigh training data using $\\beta_i = \\exp(f(\\mathbf{x}_i))$ or better $\\beta_i = \\min(\\exp(f(\\mathbf{x}_i)), c)$\n",
"1. Use weights $\\beta_i$ for training on $X$ with labels $Y$\n",
"\n",
"Note that this method relies on a crucial assumption.\n",
"For this scheme to work, we need that each data point \n",
"in the tartget (test time)distribution \n",
"had nonzero probability of occuring at training time.\n",
"If we find a point where $q(\\mathbf{x}) > 0$ but $p(\\mathbf{x}) = 0$,\n",
"then the corresponding importance weight should be infinity.\n",
"\n",
"**Generative Adversarial Networks** \n",
"use a very similar idea to that described above \n",
"to engineer a *data generator* that outputs data \n",
"that cannot be distinguished \n",
"from examples sampled from a reference dataset. \n",
"In these approaches, we use one network, $f$ \n",
"to distinguish real versus fake data \n",
"and a second network $g$ that tries to fool the discriminator $f$ \n",
"into accepting fake data as real. \n",
"We will discuss this in much more detail later.\n",
"\n",
"\n",
"### Label Shift Correction\n",
"\n",
"For the discussion of label shift, \n",
"we'll assume for now that we are dealing\n",
"with a $k$-way multiclass classification task.\n",
"When the distribution of labels shifts over time $p(y) \\neq q(y)$\n",
"but the class-conditional distributions stay the same \n",
"$p(\\mathbf{x})=q(\\mathbf{x})$,\n",
"our importance weights will correspond to the \n",
"label likelihood ratios $q(y)/p(y)$.\n",
"One nice thing about label shift is that \n",
"if we have a reasonably good model (on the source distribution) \n",
"then we can get consistent estimates of these weights \n",
"without ever having to deal with the ambient dimension \n",
"(in deep learning, the inputs are often high-dimensional perceptual objects like images, while the labels are often easier to work, \n",
"say vectors whose length corresponds to the number of classes).\n",
"\n",
"To estimate calculate the target label distribution,\n",
"we first take our reasonably good off the shelf classifier \n",
"(typically trained on the training data)\n",
"and compute its confusion matrix using the validation set\n",
"(also from the training distribution).\n",
"The confusion matrix C, is simply a $k \\times k$ matrix\n",
"where each column corresponsd to the *actual* label\n",
"and each row corresponds to our model's predicted label.\n",
"Each cell's value $c_{ij}$ is the fraction of predictions \n",
"where the true label was $j$ *and* our model predicted $y$.\n",
"\n",
"Now we can't calculate the confusion matrix\n",
"on the target data directly,\n",
"because we don't get to see the labels for the examples \n",
"that we see in the wild,\n",
"unless we invest in a complex real-time annotation pipeline.\n",
"What we can do, however, is average all of our models predictions \n",
"at test time together, yielding the mean model output $\\mu_y$.\n",
"\n",
"It turns out that under some mild conditions—\n",
"if our classifier was reasonably accurate in the first place,\n",
"if the target data contains only classes of images that we've seen before,\n",
"and if the label shift assumption holds in the first place \n",
"(far the strongest assumption here),\n",
"then we can recover the test set label distribution by solving\n",
"a simple linear system $C \\cdot q(y) = \\mu_y$.\n",
"If our classifier is sufficiently accurate to begin with,\n",
"then the confusion C will be invertible, \n",
"and we get a solution $q(y) = C^{-1} \\mu_y$.\n",
"Here we abuse notation a bit, using $q(y)$ \n",
"to denote the vector of label frequencies.\n",
"Because we observe the labels on the source data,\n",
"it's easy to estimate the distribution $p(y)$.\n",
"Then for any training example $i$ with label $y$, \n",
"we can take the ratio of our estimates $\\hat{q}(y)/\\hat{p}(y)$\n",
"to calculate the weight $w_i$,\n",
"and plug this into the weighted risk minimization algorithm above.\n",
"\n",
"\n",
"### Concept Shift Correction\n",
"\n",
"Concept shift is much harder to fix in a principled manner. \n",
"For instance, in a situation where suddenly the problem changes \n",
"from distinguishing cats from dogs to one of \n",
"distinguishing white from black animals, \n",
"it will be unreasonable to assume \n",
"that we can do much better than just collecting new labels\n",
"and training from scratch. \n",
"Fortunately, in practice, such extreme shifts are rare. \n",
"Instead, what usually happens is that the task keeps on changing slowly. \n",
"To make things more concrete, here are some examples:\n",
"\n",
"* In computational advertising, new products are launched, old products become less popular. This means that the distribution over ads and their popularity changes gradually and any click-through rate predictor needs to change gradually with it.\n",
"* Traffic cameras lenses degrade gradually due to environmental wear, affecting image quality progressively.\n",
"* News content changes gradually (i.e. most of the news remains unchanged but new stories appear).\n",
"\n",
"In such cases, we can use the same approach that we used for training networks to make them adapt to the change in the data. In other words, we use the existing network weights and simply perform a few update steps with the new data rather than training from scratch.\n",
"\n",
"## A Taxonomy of Learning Problems\n",
"\n",
"Armed with knowledge about how to deal with changes in $p(x)$ and in $p(y|x)$, we can now consider some other aspects of machine learning problems formulation. \n",
"\n",
"\n",
"* **Batch Learning.** Here we have access to training data and labels $\\{(x_1, y_1), \\ldots (x_n, y_n)\\}$, which we use to train a network $f(x,w)$. Later on, we deploy this network to score new data $(x,y)$ drawn from the same distribution. This is the default assumption for any of the problems that we discuss here. For instance, we might train a cat detector based on lots of pictures of cats and dogs. Once we trained it, we ship it as part of a smart catdoor computer vision system that lets only cats in. This is then installed in a customer's home and is never updated again (barring extreme circumstances).\n",
"* **Online Learning.** Now imagine that the data $(x_i, y_i)$ arrives one sample at a time. More specifically, assume that we first observe $x_i$, then we need to come up with an estimate $f(x_i,w)$ and only once we've done this, we observe $y_i$ and with it, we receive a reward (or incur a loss), given our decision. Many real problems fall into this category. E.g. we need to predict tomorrow's stock price, this allows us to trade based on that estimate and at the end of the day we find out whether our estimate allowed us to make a profit. In other words, we have the following cycle where we are continuously improving our model given new observations.\n",
"\n",
"$$\n",
"\\mathrm{model} ~ f_t \\longrightarrow\n",
"\\mathrm{data} ~ x_t \\longrightarrow\n",
"\\mathrm{estimate} ~ f_t(x_t) \\longrightarrow\n",
"\\mathrm{observation} ~ y_t \\longrightarrow\n",
"\\mathrm{loss} ~ l(y_t, f_t(x_t)) \\longrightarrow\n",
"\\mathrm{model} ~ f_{t+1}\n",
"$$\n",
"\n",
"* **Bandits.** They are a *special case* of the problem above. While in most learning problems we have a continuously parametrized function $f$ where we want to learn its parameters (e.g. a deep network), in a bandit problem we only have a finite number of arms that we can pull (i.e. a finite number of actions that we can take). It is not very surprising that for this simpler problem stronger theoretical guarantees in terms of optimality can be obtained. We list it mainly since this problem is often (confusingly) treated as if it were a distinct learning setting.\n",
"* **Control (and nonadversarial Reinforcement Learning).** In many cases the environment remembers what we did. Not necessarily in an adversarial manner but it'll just remember and the response will depend on what happened before. E.g. a coffee boiler controller will observe different temperatures depending on whether it was heating the boiler previously. PID (proportional integral derivative) controller algorithms are a [popular choice](http://pidkits.com/alexiakit.html) there. Likewise, a user's behavior on a news site will depend on what we showed him previously (e.g. he will read most news only once). Many such algorithms form a model of the environment in which they act such as to make their decisions appear less random (i.e. to reduce variance).\n",
"* **Reinforcement Learning.** In the more general case of an environment with memory, we may encounter situations where the environment is trying to *cooperate* with us (cooperative games, in particular for non-zero-sum games), or others where the environment will try to *win*. Chess, Go, Backgammon or StarCraft are some of the cases. Likewise, we might want to build a good controller for autonomous cars. The other cars are likely to respond to the autonomous car's driving style in nontrivial ways, e.g. trying to avoid it, trying to cause an accident, trying to cooperate with it, etc.\n",
"\n",
"One key distinction between the different situations above is that the same strategy that might have worked throughout in the case of a stationary environment, might not work throughout when the environment can adapt. For instance, an arbitrage opportunity discovered by a trader is likely to disappear once he starts exploiting it. The speed and manner at which the environment changes determines to a large extent the type of algorithms that we can bring to bear. For instance, if we *know* that things may only change slowly, we can force any estimate to change only slowly, too. If we know that the environment might change instantaneously, but only very infrequently, we can make allowances for that. These types of knowledge are crucial for the aspiring data scientist to deal with concept shift, i.e. when the problem that he is trying to solve changes over time.\n",
"\n",
"\n",
"## Fairness, Accountability, and Transparency in machine Learning\n",
"\n",
"Finally, it's important to remember \n",
"that when you deploy machine learning systems\n",
"you aren't simply minimizing negative log likelihood\n",
"or maximizing accuracy—you are automating some kind of decision process.\n",
"Often the automated decision-making systems that we deploy\n",
"can have consequences for those subject to its decisions.\n",
"If we are deploying a medical diagnostic system,\n",
"we need to know for which populations it may work and which it may not.\n",
"Overlooking forseeable risks to the welfare of a subpopulation\n",
"would run afoul of basic ethical principles.\n",
"Moreover, \"accuracy\" is seldom the right metric.\n",
"When translating predictions in to actions\n",
"we'll often want to take into account the potential cost sensitivity\n",
"of erring in various ways. \n",
"If one way that you might classify an image could be perceived as a racial sleight, while misclassification to a different category\n",
"would be harmless, then you might want to adjust your thresholds\n",
"accordingly, accounting for societal values \n",
"in designing the decision-making protocol.\n",
"We also want to be careful about how prediction systems \n",
"can lead to feedback loops. \n",
"For example, if prediction systems are applied naively to predictive policing,\n",
"allocating patrol officers accordingly, a vicious cycle might emerge.\n",
"Neighborhoods that have more crimes, get more patrols, get more crimes discovered, get more training data, get yet more confident predictions, leading to even more patrols, even more crimes discovered, etc. \n",
"Additionally, we want to be careful about whether we are addressing the right problem in the first place. Predictive algorithms now play an outsize role in mediating the dissemination of information.\n",
"Should what news someone is exposed to be determined by which Facebook pages they have *Liked*? These are just a few among the many profound ethical dilemmas that you might encounter in a career in machine learning.\n",
"\n",
"\n",
"\n",
"## Summary\n",
"\n",
"* In many cases training and test set do not come from the same distribution. This is called covariate shift.\n",
"* Covariate shift can be detected and corrected if the shift isn't too severe. Failure to do so leads to nasty surprises at test time.\n",
"* In some cases the environment *remembers* what we did and will respond in unexpected ways. We need to account for that when building models.\n",
"\n",
"## Exercises\n",
"\n",
"1. What could happen when we change the behavior of a search engine? What might the users do? What about the advertisers?\n",
"1. Implement a covariate shift detector. Hint - build a classifier.\n",
"1. Implement a covariate shift corrector.\n",
"1. What could go wrong if training and test set are very different? What would happen to the sample weights?\n",
"\n",
"## Scan the QR Code to [Discuss](https://discuss.mxnet.io/t/2347)\n",
"\n",
"![](../img/qr_environment.svg)"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}