S EQUENTIAL C OVARIATE S HIFT D ETECTION U SING C LASSIFIER T WO -S AMPLE T ESTS
Anonymous authors
Paper under double-blind review
A BSTRACT
A standard assumption in supervised learning is that the training data and test data are from the same distribution.
However, this assumption often fails to hold in practice, which can cause the learned model to perform poorly.
We consider the problem of detecting covariate shift , where the covariate distribution shifts but the conditional distribution of labels given covariates remains the same.
This problem can naturally be solved using a two-sample test— i.e. , test whether the current test distribution of covariates equals the training distribution of covariates.
Our algorithm builds on classiﬁer tests , which train a discriminator to distinguish train and test covariates, and then use the accuracy of this discriminator as a test statistic.
A key challenge is that classiﬁer tests assume given a ﬁxed set of test covariates.
In practice, test covariates often arrive sequentially over time—e.g., a self-driving car observes a stream of images while driving.
Furthermore, covariate shift can occur multiple times— i.e. , shift and then shift back later or gradually shift over time.
To address these challenges, our algorithm trains the discriminator online.
Furthermore, it evaluates test accuracy using each new covariate before taking a gradient step; this strategy avoids constructing a held-out test set, which can reduce sample efﬁciency.
We prove that this optimization preserves the correctness— i.e. , our algorithm achieves a desired bound on the false positive rate.
In our experiments, we show that our algorithm efﬁciently detects covariate shifts on ImageNet.
I NTRODUCTION
A key challenge facing deep neural networks is their sensitivity to changes in the data distribution.
In particular, supervised learning traditionally assumes that the training and test data are from the same distribution (Vapnik, 1998), but this assumption often fails in practice.
For example, an autonomous car using perception to identify obstacles needs to be robust to shifts such as changes in the weather and lighting conditions.
We focus on covariate shift (Shimodaira, 2000), where there is a shift in the covariate distribution p ( x ) , and the conditional label distribution p ( y | x ) remains unchanged.
Covariate shift can reduce model performance (Sugiyama & M¨uller, 2005), invalidate uncertainty estimates (Ovadia et al., 2019; Park et al., 2020), and affect model selection (Sugiyama et al., 2007).
One strategy is to devise an algorithm to detect covariate shift; if detected, the algorithm can alert the user that predictions may be unreliable.
Covariate shift detection can be formulated as two-sample hypothesis test (Gretton et al., 2012a;
Rabanser et al., 2018; Liu et al., 2020), where the goal is to determine whether two sets of examples are from the same distribution.
To test for covariate shift, we choose the ﬁrst sample to be the data used to train the model and the second sample to be recent test data given as input to the model.
Then, the detector returns “covariate shift” if the hypothesis test indicates that the two samples are from different distributions and “no shift” otherwise.
We propose a detection algorithm based on classiﬁer tests (Lopez-Paz & Oquab, 2017; Cheng &
Cloninger, 2019; Kim et al., 2021), which use the accuracy of a classiﬁer trained to distinguish the two samples as the test statistic.
In particular, if the two samples are from the same distribution, then the accuracy should be 1 / 2 ; otherwise, it should be > 1 / 2 .
Since the test statistic follows a binomial distribution, we use the Clopper-Pearson interval (Clopper & Pearson, 1934) (an exact conﬁdence interval for the unknown success probability of the Binomial distribution) to derive the cutoff.
In contrast, prior work relies on asymptotics to derive the cutoff, which results in approximations.
A key challenge is that the test examples are typically obtained over time—e.g., an autonomous robot continuously perceives its environment, and we want to detect if its distribution of observations shifts at any time.
There are two key challenges to leveraging classiﬁer tests in this setting.
First, they rely on training a classiﬁer to distinguish training and test examples; doing so on every step would be computationally intractable.
Second, they rely on a held-out test set to estimate the test statistic, but constructing such a set online would reduce sample efﬁciency.
Rather than train a classiﬁer at each step, our proposed algorithm trains a model online using stochastic gradient descent.
Then, rather than construct a held-out test set, our algorithm evaluates the accuracy of the model online using each example before taking a gradient step on that example.
We prove that this strategy results in an unbiased estimate of the model accuracy; as a consequence, the ﬁnite-sample guarantees on the false positive rate provided by the sequential test continue to hold.
In addition, we prove bounds on the false negative rate under mild conditions on the classiﬁer (i.e., it achieves nontrivial accuracy distinguishing the two distributions).
We evaluate our approach on both synthetic and natural shifts on the ImageNet (Russakovsky et al., 2015) dataset.
In particular, we demonstrate that our approach achieves better sample efﬁciency than baseline algorithms; furthermore, it uniformly satisﬁes the desired false positive rate.
Thus, our algorithm is an effective strategy for sequential covariate shift detection.
Contributions.
We formulate (sequential) covariate shift detection as a two-sample test, and propose a novel algorithm to solve this problem (Section 3).
Then, we prove ﬁnite sample bounds on false positive rate and false negative rate achieved by our algorithm (Section 4).
Finally, we empirically demonstrate that our algorithm effectively detects shifts on ImageNet (Section 5).
Sequential detection vs. sequential tests.
While we consider the sequential setting, we deliberately choose not use a sequential hypothesis test, since the covariate shift may occur after a delay or gradually over time.
A sequential test only applies if all of the test data is shifted.
Furthermore, since we are not using sequential tests, the false positive rate bound only holds per-step, not uniformly across all steps.
This is necessary: we cannot guarantee that we detect a covariate shift occurring at a later point in time if we constrain the false positive to be bounded uniformly across all steps.
In our experiments, we show that the rate of false alarms remains manageable while enabling our algorithm to detect covariate shift in a number of interesting scenarios.
R ELATED W ORK
Covariate shift.
There has been work on training models in the presence of covariate shift.
In particular, in the unsupervised domain adaptation setting (Ben-David et al., 2007; Bickel et al., 2007; Ganin et al., 2016), the algorithm has access to labeled examples from the source domain but only unlabeled examples from the target domain, and the goal is to train a model that achieves good performance on the target domain.
One strategy is to use importance weighting to upweight source examples that are more similar to target examples (Bickel et al., 2007).
Another strategy is to ﬁrst learn an invariant representation (Ganin et al., 2016), which is an embedding space where the source and target examples are similar, and then train a model on this embedding space using the source examples.
If we detect covariate shift, one solution is to retrain the model using these techniques.
Two-sample tests.
We focus on classiﬁer two-sample tests (C2ST) .
In this approach, the idea is to train a binary classiﬁer to distinguish source and target samples, compute a real-valued score based on this classiﬁer as the test statistic, and then use a univariate two-sample test to determine the cutoff for rejecting the null hypothesis (Friedman, 2004).
A natural test statistic is the classiﬁer’s accuracy on a held-out test set (Kim et al., 2021; Lopez-Paz & Oquab, 2017), or the differences in the classiﬁer’s logits (Cheng & Cloninger, 2019); in this work, we use the former.
One way to compute the cutoff is to use the asymptotic distribution of the test statistic (Lopez-Paz & Oquab, 2017).
Nonparametric tests such as permutation tests can also be used (Kim et al., 2021).
Another kind of two-sample test is called a kernel two-sample test .
In this approach, the idea is to use the maximum mean discrepancy (MMD) between the two samples according to a given kernel embedding as the test statistic (Gretton et al., 2012a;
Chwialkowski et al., 2015; Jitkrittum et al., 2016).
The key design decision is the choice of kernel.
One strategy is to use a nonparametric kernel such as Gaussian radial basis functions (Gretton et al., 2012a); alternatively, the kernel can also be optimized to minimize the false negative rate of the resulting test (Gretton et al., 2012b).
In addition, recent work has shown how to ﬁrst learn a kernel function in the form of a deep neural network, and then evaluate the MMD distance on a held-out test set (Liu et al., 2020).
The test statistic can be chosen based on ﬁnite sample bounds or based on its asymptotic distribution (Gretton et al., 2012a).
Nonparametric permutation tests can also be used (Liu et al., 2020) with this approach.
Concept Drift.
In the context of concept drift (Gama et al., 2014), there has been work detecting shifts in p ( x, y ) (Gonc¸alves Jr et al., 2014).
However, this work assumes that ground truth labels are provided for test examples, whereas our approach only requires unlabeled test examples.
The former is substantially easier, since it sufﬁces to check for drift in the distribution of prediction errors, which is usually very simple (e.g., a Bernoulli distribution for the 0-1 loss), making it easy to test for drift.
In contrast, our approach checks for drift in high-dimensional covariates distribution.
Sequential hypothesis testing.
A closely related problem is sequential hypothesis testing, which adaptively decides whether to reject the null hypothesis as samples become available (Wald, 1945).
These approaches can also applied to two-sample testing (Balsubramani & Ramdas, 2015; Lh´eritier & Cazals, 2018; 2019; Manole & Ramdas, 2021).
However, as discussed above, they assume that the each distribution of the two samples does not change over time.
In contrast, we are interested in the setting where the test examples might initially be from the same distribution as the training examples, but then shift at a later point in time.
Sequential tests are not applicable to this setting.
S EQUENTIAL C OVARIATE S HIFT D ETECTION 3. P ROBLEM F ORMULATION
Let X be the covariate space, S be the source distribution over X , and T t 1 : t 2 = ( T t 1 , T t 1 +1 , . .
, T t 2 ) be a sequence of target distributions over X from time steps t 1 to t 2 .
On time step t , we consider samples x t ∼ S and x (cid:48)
t ∼ T t ; in practice, S can be taken to be the uniform distribution over the training set.
We let S w,t
=
( x t − w +1 , x t − w +2 , . . .
, x t ) and T w,t = ( x (cid:48) t − w +1 , x (cid:48)
t − w +2 , . . .
, x (cid:48) t ) denote the recent examples in a time window of a given size w ∈ N .
Our goal is to detect covariate shift at any step t .
More precisely, we want to determine whether
S (cid:54) =
¯ T w,t , where ¯ T w,t = (cid:80) tk = t − w +1 T k /w , i.e. , whether the average target distributions over the previous w steps is shifted compared to S .
For a ﬁxed step t , this problem is a two-sample hypothesis test (Lehmann & Romano, 2006), where the null hypothesis is H 0 : S
= ¯ T w,t , and the alternative hypothesis is H 1 : S (cid:54) =
¯ T w,t .
That is, a two-sample test ˆ f is designed to compute
[Equation]
Our goal is to design a two-sample test ˆ f for detecting covariate shift with this data stream.
While we can in principle use any two-sample test, our goal is to design one that is both sample and computationally efﬁcient while achieving high accuracy for high-dimensional data such as images.
In addition, we want the test ˆ f to come with ﬁnite sample guarantees on the false positive rate.
In particular, given α ∈ R > 0 , if S = ¯ T w,t , we want to ensure
[Equation]
Ideally, we also want to provide ﬁnite sample bounds on the false negative rate; however, for classiﬁer tests, we can only do so under additional assumptions about the model family used to try and distinguish S and ¯ T w,t .
Intuitively, we assume that (i) the model family has bounded complexity (e.g., Rademacher complexity), and (ii) some model exists in the family that achieves nontrivial accuracy at distinguishing S and ¯ T w,t .
Then, our goal is to ensure that if S (cid:54) = ¯ T w,t , we have [Equation]
[Equation]
for some function M ( α, w ) that depends on the model family.
3. A LGORITHM O VERVIEW
Next, we describe our two-sample test.
We build on classiﬁer two-sample test (C2ST) (Lopez-Paz & Oquab, 2017; Kim et al., 2021).
The idea is to train a classiﬁer ˆ g t to try and distinguish S w,t
Input t do 3:
Draw examples x t ∼ S , x (cid:48)
t ∼ T t 4:
Predict ˆ y t
= ˆ g t ( x t ) and ˆ y (cid:48) t = ˆ g t x t )
Source prediction 5:
Detect covariate shift if 0 .
Θ CP µ w t ,
Calibrated covariate shift detection 6:
Update ˆ g t using ( x t , 0) ( x t update 7: end for from T w,t .
Intuitively, if S and ¯ T w,t are different distributions, then ˆ g t should achieve nontrivial accuracy at distinguishing S w,t from T w,t (assuming the model family is sufﬁciently expressive). Alternatively, if S = ¯ T w,t , then ˆ g t necessarily achieves a trivial expected accuracy of 1 / 2 . In particular, the accuracy of ˆ g t can be used as a test statistic for the two-sample test. To choose the cutoff for rejecting the null hypothesis, we use the Clopper-Pearson (CP) interval (Clopper &
Pearson, 1934) to construct an interval that contains the true accuracy ˆ g t with high probability based on the accuracy of ˆ g t on a test set.
More precisely, the CP interval is an exact conﬁdence interval around the empirical estimate of the mean of a Bernoulli random variable.
Letting z 1 , ..., z n ∼
Bernoulli ( µ ∗ ) be i.i.d.
samples from a Bernoulli distribution with true mean µ ∗ , the (unnormalized) estimate of its mean n · ˆ µ ( z 1: n ) =
(cid:80) ni
=
z i has distribution Binomial ( n, µ ∗ ) .
Then, the CP interval Θ CP ( s, n ; α ) ⊆
[0 , 1] is an interval around ˆ µ containing µ ∗ with probability at least 1 − α , i.e. ,
[Equation]
where the probability is taken over s , α is a given conﬁdence level, and Θ CP is a function of the Binomial random variable s = n · ˆ µ ( z 1: n ) .
The CP interval is concretely deﬁned by
[Equation]
where F ( s ; n, θ ) is the cumulative distribution function (CDF) of Binomial ( n, θ ) .
To compute the
CP interval, we can use the following equivalent formula:
[Equation]
where Q ( p, a, b ) is the p th quantile of a Beta distribution with parameters a, b (Hartley & Fitch, 1951; Brown et al., 2001).
Our algorithm uses the CP interval to determine whether the accuracy of ˆ g t is nontrivial, i.e. , > 1 / 2 .
In particular, the accuracy of ˆ g t is the mean of the Bernoulli random variable 1 (ˆ g t ( x ) =
y ) , where y is the ground truth indicating whether x is from S or ¯ T w,t .
Then, our algorithm rejects if the CP interval does not contain 1 / 2 , since this condition implies that the accuracy of ˆ g t does not equal 1 / 2 with high probability.
We describe this step in detail below.
The key challenge is what data to use as the test dataset to estimate the accuracy of ˆ g t .
The traditional strategy is to split the available data into two parts: one to train ˆ g t and a second held-out test set to estimate its accuracy (Lopez-Paz & Oquab, 2017;
Kim et al., 2021).
However, this approach reduces sample efﬁciency, which is problematic in our setting since we often want to w to be small.
To address this challenge, our algorithm exploits the conditional independence structure of classiﬁer predictions.
In particular, as described below, our algorithm uses each example x t to evaluate the accuracy of ˆ g t before using it to train ˆ g t .
In the next section, we prove that this strategy maintains the independence of our estimate of the accuracy of ˆ g t (Lemma 1), and that as a consequence, our algorithm satisﬁes the desired false positive rate (for a single step t ).
3. A LGORITHM D ETAILS
Sequential detection algorithm.
At each time step t , we observe a source sample x t ∼ S and a target sample x (cid:48)
t ∼ T t .
By using these current samples and previous samples, we detect covariate shifts by updating the source-target classiﬁer in online learning.
In particular, our algorithm consists of three steps: (1) source-target prediction, (2) covariate shift detection, and (3) online source-target classiﬁer update.
The following and Algorithm 1 include details.
Step 1.
Source-target prediction.
We predict source-target labels on the current samples x t and x (cid:48) t using the current source-target classiﬁer ˆ g t .
In particular, we denote prediction on the source sample x t by ˆ y t , i.e. , ˆ y t
= ˆ g t ( x t ) , and denote prediction on the target sample x (cid:48) t by ˆ y (cid:48)
t , i.e. , ˆ y (cid:48) t
= ˆ g t ( x (cid:48) t ) .
These predictions and previous predictions are used in covariate shift detection in the following step.
Step 2.
Calibrated covariate shift detection.
Let Q w,t be a distribution over X × { 0 , 1 } , where
[Equation]
Then, z = 1 (ˆ g t ( x ) =
y ) is a Bernoulli random variable with distribution Bernoulli ( µ ∗ w,t ) , where
[Equation]
is the accuracy of ˆ g at distinguishing whether an example x is from distribution S or ¯ T w,t .
The unbiased empirical estimate of this accuracy is denoted by
[Equation]
In fact, 2 w ˆ µ w,t is a Binomial random variable with Binomial (2 w, µ ∗ w,t ) ; thus, the accuracy µ ∗ w,t can be estimated by the Clopper-Pearson (CP) interval Θ CP (2 w ˆ µ w,t , 2 w ; α ) that includes the unknown parameter µ ∗ w,t with high probability, i.e. , P [ µ ∗ ∈ Θ CP (2 w ˆ µ w,t , 2 w ; α )] ≥
[Equation]
− α. This property can be used for checking the accuracy of ˆ g t might be 1 / 2 .
In particular, our algorithm returns “covariate shift” if 1 / 2 (cid:54)∈ Θ CP (2 w ˆ µ w,t , 2 w ; α ) , and “no covariate shift” otherwise, i.e. ˆ f ( S w,t , [Equation] w,t
[Equation]
Here, the Clopper-Pearson interval calibrates the empirical accuracy ˆ µ w,t using the property of the
Binomial distribution.
Step 3.
Online source-target classiﬁer update.
Finally, we update a binary classiﬁer ˆ g t using new training examples based on the source and target samples, i.e. , ( x t , 0) and ( x (cid:48) t , 1) .
In general, ˆ g t can be any model; we consider it to be a neural network, in which case we can update its parameters using stochastic gradient descent with respect to the cross entropy loss.
T HEORETICAL G UARANTEES
In this section, we describe our ﬁnite sample bounds on the false positive and false negative rates of our covariate shift detector ˆ f ; the key to have valid bounds is proving the independence on predictions ˆ y 1 , . . .
, ˆ y t (and ˆ y (cid:48) 1 , . . .
, ˆ y (cid:48) t ) to have a valid Clopper-Pearson interval, since they are seemingly dependent through the online learned classiﬁer ˆ g t .
First, our key result shows that our estimate of the accuracy of ˆ g t is valid— i.e. , the predictions
ˆ
y i , . . . , ˆ y j are conditionally independent (see Appendix A.1 for a proof), thus the accuracy is the parameter of the Binomial distribution:
Lemma 1.
If x i , . . , x j are independent for any i, j ∈ N where i < j , ˆ y i , . . .
, ˆ y j are conditionally independent given ˆ g i , . . . ˆ g j − 1 .
Our next result says that our algorithm ensures the desired bound α on the false positive rate (i.e., ˆ f says “covariate shift” when there is no covariate shift).
To this end, we exploit the following observation that any source-target classiﬁer makes the expected accuracy of 1 / 2 if there is no covariate shift.
Intuitively, if S = ¯ T w,t , source-target classiﬁcation is impossible (Lopez-Paz & Oquab, 2017;
Liu et al., 2020); we include this lemma for completeness (see Appendix A.2 for a proof):
Lemma 2.
If S = ¯ T w,t , we have µ ∗ w,t = 1 / 2 for any source-target classiﬁer ˆ g t .
Since the expected accuracy of ˆ g t is 1 / 2 regardless of how we design and learn ˆ g t , and how many samples are used to learn ˆ g t , the Clopper-Pearson interval includes the true accuracy with high probability; thus the false positive rate of the proposed covariate shift detector ˆ f is effectively controlled by the conﬁdence level of the Clopper-Pearson interval, as follows (see Appendix A.3 for a proof):
Theorem 3 (Bound on false positive rate) .
If S = ¯ T w,t , then for any ˆ g t , we have
[Equation]
Our next result provides a bound on the false negative rate; we ﬁrst observe that the Clopper-Pearson interval is included in the interval by the Hoeffding’s bound.
Intuitively, the Clopper-Pearson interval represents a lower and upper bound of the expected accuracy given an empirical accuracy tailored to a Bernoulli random variable; the Hoeffding’s bound can similarly bound the mean but in a more general setup.
Thus, the Clopper-Pearson interval can be smaller (see Appendix A.4 for a proof).
Lemma 4.
Let s ∼ Binomial ( n, p ) and F ( s ; n, p ) is the CDF of Binomial ( n, p ) ; we have
[Equation]
Leveraging this, we have the following bound on false negative rate (see Appendix A.5 for a proof).
Theorem 5 (Bound on false negative rate) .
Assume ˆ g t achieves nontrivial accuracy, i.e. , µ ∗ w,t ≥ 1 / 2 + (cid:15) , where (cid:15) ∈ (0 , 1 / 2] , is the accuracy at distinguishing S and ¯ T w,t .
Let a ( w, α ) :
= 2 w ( 1 / 2 + (cid:112) log [Equation] [Equation] w ) and b ( w, α ) :
= 2 w ( 1 / 2 − (cid:112) log(2 /α ) / 4 w ) .
If S (cid:54) = ¯ T w,t and w − 1 −(cid:98) (cid:112) w log(2 /α ) (cid:99) ≥ 0 , then we have
[Equation]
In the false negative bound, the ﬁrst term is dominant and increases as w increases, which implies the sample size needs to be increased to have a powerful shift detector; the condition on w suggests that the bound is valid when w ≥ 201 given α = 0 .
We note that the assumption L (ˆ g t ) := 1 − µ ∗ ≤ 1 / 2 − (cid:15) can be achieved under standard conditions.
For instance, assume that the model family G of source-target classiﬁers has ﬁnite VC dimension ( i.e. , VC ( G ) < ∞ ), and that the optimal model g ∗ ∈ G has nontrivial inaccuracy L ( g ∗ )
= 1 / 2 − ξ for some ξ ∈ R > 0 ; then, with probability at least 1 − δ with respect to S w,t and T w,t and letting m = 2 w , we have
[Equation]
(cid:123)(cid:122) (cid:125) =: (cid:15) , where the second term (which we have taken to be (cid:15) ) satisﬁes (cid:15) > 0 for sufﬁciently large m .
E XPERIMENTS
We evaluate the effectiveness of our algorithm at detecting both natural and synthetic covariate shifts of varying forms (e.g., gradual shifts and multiple shifts back and forth), showing that it signiﬁcantly outperforms natural baselines.
5. E XPERIMENT S ETUP
Baselines.
We compare our algorithm to three baselines: two of them differ in the way they use the samples at each time step, the third uses Wald’s sequential likelihood test (Wald, 1945).
For the ﬁrst two baselines, while our approach uses all samples to construct the CP interval around the accuracy of the source-target classiﬁer ˆ g t as well as to train ˆ g t , the baseline instead constructs a held-out test set using every H th sample.
Then, only this held-out test set is used to compute the CP interval, and only the remaining samples are used to train ˆ g t .
In our experiments, we used values of H ∈ { 2 , 5 } , denoted H2, H5, respectively.
For Wald’s test, we consider the Bernoulli random variable with a probability p indicating whether the prediction of the source-target classiﬁer is correct for the given
[Table] This baseline is essentially the online version of an existing classiﬁer two-sample test (C2ST) (Kim et al., 2021; Lopez-Paz & Oquab, 2017), which splits the (ﬁxed) training dataset into a training set to train ˆ g t and a held-out test set to estimate the accuracy of ˆ g t ; thus, H controls the tradeoff between the number of examples in the training set and held-out test set.
Source-target classiﬁer.
We use a fully-connected neural network with a single hidden layer (with 128 hidden units) and with the ReLU activation functions as the source-target classiﬁer ˆ g t .
We use a binary cross-entropy loss for training in conjunction with an SGD optimizer with a learning rate of 0.01 (for natural shift experiments) and 0.001 (for synthetic shift experiments).
Finally, since the inputs are ImageNet images (Russakovsky et al., 2015), we use a 2048-dimensional feature vector generated by ﬁrst running a pretrained ResNet152 model (He et al., 2016) on the images, and then using these features vectors for the covariates of S w,t and T w,t .
Scenarios.
We run each algorithm to test whether the target distribution in the given window is shifted with three different scenarios: multiple shift (“M-shift”), gradually increasing shift (“GIshift”), and gradually increasing-then-decreasing shift (“GID-shift”).
Table 1 describes each scenario.
For example, the multiple shift scenario proceeds as follows: (i) it starts with no covariate shift at the beginning; (ii) after observing 25% target samples ( i.e. , 250 th samples for natural shift experiments and 2500 th samples for synthetic shift experiments), covariate shift is applied to all target samples (with probability 1) by adding random perturbations for synthetic shift and by drawing samples from a target distribution for natural shift; (iii) after 50% of target samples, it reverts to no covariate shift; and (iv) ﬁnally after observing 75% target samples, the covariate shift is applied to the all target samples.
Gradually increasing shift and gradually increasing-then-decreasing shift scenarios start with no covariate shift for the ﬁrst 20% of target samples; then, covariate shift is applied with some probability 0 < p < 1 by gradually changing p over time.
Stream data generation.
For each shift ( i.e. , natural shift and synthetic shift), we have a source dataset S and target datasets T t , from which we randomly draw source and target samples for each time step t .
In particular, we consider a batch of samples for computational efﬁciency, where we denote the batch size by B ; we use B = 10 for our experiments.
That is, we wait for B samples to be collected from the target distribution before checking for covariate shift and the updating the sourcetarget classiﬁer; then, we begin collecting the next batch.
Finally, we evaluate each approach using multiple random repetitions, which we denote by R (the value of R depends on each experiment).
5. N ATURAL SHIFT
Dataset.
First, we consider a natural shift on ImageNet.
To construct such a shift, we consider the subset of dog classes; in particular, 120 of the 1000 of the ImageNet classes are of dogs (Khosla et al., 2011).
Then, we randomly select half ( i.e. , 60) of these classes to be the source dataset, and the other half to be the target dataset; thus, the number of source and target images is 2997 each (after removing duplicated images).
As a consequence, the source and target datasets correspond to different dog breeds, which is a kind of natural distribution shift.
Results.
Figure 1 and Table 2 show results for the natural shift experiment with w = 10 and α = 1% .
Figure 1 illustrates detection rates of the four algorithms with R = 100 repetitions (i.e., the fraction of repetitions that reported “covariate shift” at each step).
Table 2a shows the number [Figure] [Figure] [Table]
[Figure]
[Table]
[Figure]
[Table]
of shifted samples required to reach at least 80% of covariate shift detection rate under the shift
[Table] Table 2b shows false positive rate (FPR) after 50, 100, 150, and 200 samples with R =repetitions.
Discussion.
Figure 1 shows the detection rate of each algorithm as each scenario progresses.
In multiple shift (Figure 1a) and gradually increasing-then-decreasing shift (Figure 1c), covariate shift disappears after a certain point, and all algorithms correctly detect this change.
However, as shown in Table 2a, our approach always requires fewer samples to detect the shift.
While H5 does not achieve 80% detection and H2 and Wald reach 80% only for multiple shift change scenario, our approach always detects covariate shift at a rate higher than 80%.
Furthermore, for multiple shift, our algorithm requires fewer than half the number of samples compared to H2.
In summary, our algorithm is signiﬁcantly more sample efﬁcient at detecting covariate shift compared to the baselines, most likely since it utilizes all samples for both training the source-target classiﬁer and constructing the CP interval.
For FPR, all algorithms always satisfy the FPR bound ( i.e. , FPR ≤ α ).
5. S YNTHETIC S HIFT
Dataset.
Next, we consider a synthetic shift on ImageNet.
In particular, we split the original ImageNet validation set into equal sized source and target datasets.
To construct the target dataset, we add synthetic perturbations on original images.
We (separately) consider ﬁve perturbation types from (Hendrycks & Dietterich, 2019)—in particular, Contrast, Defocus Blur, Elastic Transform,
Gaussian Blur, and Gaussian Noise, with ﬁve different severity levels.
Results.
The experiment results are shown in Figure 2 and Table 3 for the experiments with the perturbation severity of 2, window size w = 10 , and signiﬁcance level α = 1% .
Table 3a shows the number of target samples required by each algorithm to detect the ﬁrst covariate shift in the detection rate of at least 80%.
Table 3b shows the false positive rate (FPR) after 500, 1000, 1500 andsamples for each of the three scenarios.
Figure 2 shows the detection rates over multiple repetitions [Figure] [Figure] [Figure]
[Table]
for each of the three scenarios using the Gaussian noise perturbation.
Results for other perturbation types and severities are shown in Appendix D.
Discussion.
As can be seen, our approach outperforms the baselines in terms of sample efﬁciency for the covariate shift detection as was the case of the natural shift.
Our algorithm requires about half as many samples before detecting covariate shift compared to the baselines.
In terms of FPR, our approach always satisﬁes the FPR bound.
Finally, Figure 2 shows the accuracy drop with the shifted samples.
In particular, the red dotted line shows the accuracy of ResNet152 on the examples in the source and target samples of the given window; as can be seen, the accuracy decreases as the degree of the shift increases.
Covariate shift detection can be successfully used to notify a user that an accuracy drop may have occurred.
C ONCLUSION
We have proposed a novel covariate shift detection algorithm, which uses a classiﬁer two-sample test to check whether the current test examples differ in distribution compared to the training examples.
Our approach ensures sample efﬁciency by avoiding the need to split the dataset into a training set and a held-out test set, and instead using all the data to both train the source-target discriminator and to evaluate its accuracy.
We prove that even with this optimization, our approach provides ﬁnite sample guarantees on the false positive rate at a desired level; we also prove bounds on the false negative rate under a mild conditions on the trained classiﬁer.
Finally, we empirically demonstrate that our proposed algorithm is signiﬁcantly more sample efﬁcient compared to a natural baseline that uses a held-out test set in terms of detecting both natural and synthetic shifts on ImageNet.
Reproducibility Statement.
For our empirical results, we stated our algorithm in Algorithm 1, hyperparameters in Section 5.1, and dataset setups in Section 5.2 and 5.3.
We have included the source code in the supplement for reproducing the experimental results.
For our theory, we have included all proofs in Appendix A.
A. P ROOF OF L EMMA
For any source-target classiﬁer ˆ g t , if S = ˆ T w,t , the following holds:
[Equation]
where the sixth equality holds since S
= ˆ T w [Equation] t ; the claim follows.
A. P ROOF OF T HEOREM
Denote the event that P ( x,y ) ∼Q w,t [ˆ g t ( x ) =
y ]
= 1 / 2 by E , and let s w,t = 2 w ˆ µ w,t .
Then, we have
[Equation]
where the ﬁrst equality holds since S
= ˆ T w,t and by Lemma 2, the third equality holds by Lemma 2, and the last inequality holds by the property of the Clopper-Pearson interval and Lemma 1.
A. P ROOF OF L EMMA
We use the tail bound of the binomial distribution using the Hoeffding’s inequality— i.e.
[Equation]
For the upper bound of the upper Clopper-Pearson interval, we have
[Equation]
For the lower bound of the lower Clopper-Pearson interval
[Equation]
we have
[Equation]
Finally, (4) and (5) imply the claim.
A. P ROOF OF T HEOREM
Let the lower and upper bound of the Clopper-Pearson interval Θ CP be Θ CP and Θ CP , respectively.
Recall that we denote the CDF of a binomial distribution binomial ( n, p ) by F ( s ; n, p ) .
Then, we have
[Equation]
where (6) and (7) hold due to P S w,t ,T w,t [ µ ∗ w,t < 1 / 2 + (cid:15) ]
= 0 and P S w,t ,T w,t [ µ ∗ w,t ≥ 1 / 2 + (cid:15) ]
= 1 from the assumption on ˆ g t and S (cid:54) =
T , respectively.
By Lemma [Equation] the ﬁrst term is lower bounded as follows:
[Equation]
where the last inequality holds since the binomial parameter 1 2 − (cid:15) makes the CDF F smallest.
Similarly, the second term is lower bounded as follows:
[Equation]
where the last inequality holds since the binomial parameter µ ∗ w,t = 1 makes the CDF F smallest.
The claim follows by combining (8) and (9).
[Figure]
[Figure]
[Table] [Figure] [Table]
[Figure] [Table]
[Figure] [Table]
B A DDITIONAL B ASELINE
We include one additional baseline, adapted version of Deep Kernel MMD (Liu et al., 2020).
This Deep Kernel (DK) requires training of the kernel parameters and the network for extracting features. We use half of samples for this training process and conduct a test using the rest of samples as H2. We run this algorithm for Natural shift as in Section 5.2.
Results.
The experimental results with DK are shown in Figure 4, and Table 4.
As described in
Section 5.2, Figure 4 shows the detection rates of each algorithms, and Table 4 presents FPR at selected points.
Our approach outperforms this adapted version of Deep kernel MMD approach.
First, in terms of the false positive rate (FPR), DK violates the desired FPR bound, while ours satisﬁes the bound (Table 4b).
Furthermore, Table 4a shows that our approach generally requires a smaller number of samples for detection.
In the M-shift case, ours needs ten more samples.
In the other two shifts, however, ours shows superior performance compared to DK.
In the GI-shift, our approach requires 150 fewer samples, and in GID-shift, our approach uses 310 samples for the detection, while DK cannot achieve 80 % detection.
C A DDITIONAL D ISCUSSION C. M ULTIPLE E POCHS IN T RAINING
As shown in Algorithm 1, each example is used only once in updating the source-target classiﬁer, baselines also follow this setting in all experiments.
We consider this single epoch update anticipating that our algorithms being used in the online setting, where it is infeasible to take multiple passes over the training data.
However, without consideration of the online setting, each example can be used multiple times during training with the restriction that the example can be used only once in the CP interval, which can improve the performance.
As this strategy is orthogonal to our approach, it can be applied to both ours and other baselines expecting the performance improvement.
D A DDITIONAL E XPERIMENTAL R ESULTS D. D ETECTION R ATE
This section shows the additional detection rate plots for the different perturbations, severities, and window sizes ( w ) including ﬁgures in the main paper.
D.1. M- SHIFT
Figure 5 - Figure 19 display the detection rate plot for M-shift scenario with different settings.
These all different settings show the similar pattern with the ﬁgures in the main paper.
[Figure]
[Figure]
[Figure]
[Figure]
[Figure]
[Figure]
[Figure]
[Figure]
[Figure]
[Figure]
[Figure]
[Figure]
[Figure]
[Figure] [Figure] D.1. GI- SHIFT This section includes the plots for GI-shift scenario with different perturbation, window sizes ( w ), and ﬁxed severity.
[Figure] [Figure]
[Figure]
D.1. GID- SHIFT
Similar to the previous two sections, this section includes ﬁgures for the GID-shift scenario with different perturbation with severity 2, and different window sizes ( w ).
[Figure] [Figure]
[Figure]
D. N UMBER OF S AMPLES FOR D ETECTION
This section presents the required number of samples for detecting covariate shift over repetitions (Rate ≥ 80% ) with different perturbations, severities, and window sizes ( w ).
D.2. M- SHIFT
[Table] [Table]
[Table]
[Table]
[Table] [Table]
[Table] [Table]
[Table] [Table]
[Table] [Table]