{'fraction_non_alphanumeric': 0.02957960127130887, 'fraction_numerical': 0.009968217278243282, 'mean_word_length': 4.920248022236477, 'pattern_counts': {'":': 0, '<': 0, '<?xml version=': 0, '>': 0, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Understanding and Testing Generalization of Deep Networks on Out-of-Distribution Data

Deep network models perform excellently on In-Distribution (ID) data, but can significantly fail on Out-Of-Distribution (OOD) data. While developing methods focus on improving OOD generalization, few attention has been paid to evaluating the capability of models to handle OOD data. This study is devoted to analyzing the problem of experimental ID test and designing OOD test paradigm to accurately evaluate the practical performance. Our analysis is based on an introduced categorization of three types of distribution shifts to generate OOD data. Main observations include: (1) ID test fails in neither reflecting the actual performance of a single model nor comparing between different models under OOD data. (2) The ID test failure can be ascribed to the learned marginal and conditional spurious correlations resulted from the corresponding distribution shifts. Based on this, we propose novel OOD test paradigms to evaluate the generalization capacity of models to unseen data, and discuss how to use OOD test results to find bugs of models to guide model debugging.


Introduction
The last few years have witnessed human-level performance of deep network models under experimental test in various tasks like image recognition, speech processing and reading comprehension [16,39]. However, in practical industrial implementations when the real-world data deviates from the observed training data, the model performance significantly declines and even sometimes completely fails [19,22,40,45]. Extensive efforts have been taken to bridge the distribution gap between real-world data and training data to guarantee practical performance. Typical solutions include domain generalization [10,11,29,32], causal learning [35,36,36], stable learning [42,53], etc.
Other than developing methods to improve the experiment-practical generalization capability, few attention has been paid to the issue of model testing and address questions like "why the experimental testing results fail to reflect the real performance in practical implementation?" We argue that the practical implementation relies on models' capability to handle Out-Of-Distribution (OOD) data, while experimental test essentially evaluates In-Distribution (ID) capability of models where training and test data follow the identical distribution. This study attempts to address the following three specific questions: • What are the problems of ID test? • Why ID test fails to reflect the practical implementation performance? • How to evaluate the practical implementation performance under experimental test?
To facilitate these analyses, we first introduce a unified perspective to simulate the distribution shift between the observed training data and unseen test data. Specifically, three types of distribution shift are considered: (1) marginal distribution shift which refers to changes in the marginal distributions of irrelevant features between the training and test data; (2) conditional distribution shift which refers to changes in conditional distributions of labels when irrelevant features are given; and (3) joint distribution shift which suffers both the above situations. As illustrated in the toy example in Figure 1, these distribution shifts represent typical ways to construct OOD data and mislead the model to learn non-optimal parameters for OOD generalization. Following this categorization, we construct three ID settings and the corresponding OOD settings of training and test set on CelebA [30] and Colored MNIST [3]. The analysis into the result difference between ID settings and OOD settings address the above first question and observes the following problems of ID test: (1) ID test result cannot reflect models' absolute OOD performance, (2) ID test result cannot compare between different models' relative OOD performance.
Following the observations on the problems of ID test, we further ascribe the reason that ID test fails to reflect the practical implementation performance to the spurious correlations that model learned when training. Corresponding to the marginal and conditional distribution shifts, two types of spurious correlations are discussed: (1) marginal spurious correlation that irrelevant features are employed, and (2) conditional spurious correlation that incorrect featurelabel correlations are employed. Experiment validates the performance decline on OOD data resulted from the different spurious correlations. Finally, we propose practical OOD test paradigms to evaluate generalization capability of models to unseen data using the observed data. We discuss test paradigms for both scenarios when irrelevant features are known and unknown using different OOD data settings. Moreover, the derived OOD test results gain access to identify what irrelevant features critically influence the model performance (i.e., finding bugs), and provide guideline to develop targeted solutions for performance improvement (i.e., debugging). It is desired to consider the test module beyond simply evaluation and form a closed loop between testing and debugging to promote the model performance in practical implementation.
The main contributions of this paper are as follows: • We introduce a unified perspective to simulate OOD data construction into three types of distribution shift. Experimental analysis observes the problems of ID test under corresponding distribution shift settings. • We explore the reasons of ID test failure as two types of learned spurious correlations. The discussions on spurious correlations illustrate their differences in effects on the generalization capability of models. • We propose novel OOD test paradigms to evaluate the models' performance in handling unseen data. This opens possibility to employ the proposed OOD test to help identify critical irrelevant features.

A Unified Perspective of Out-of-Distribution Data
In this section, we first introduce the concepts of IIDbased supervised learning and out-of-distribution data. We then propose a novel and unifying view on categorization of distribution shift. Further, we construct three ID settings and the corresponding OOD settings of training and test sets on two simple image classification datasets for experiments in Section 3.
IID-based Supervised Learning. Let X denotes the feature space and Y the label space. A predictive function or model is defined as f θ ∶ X → Y, which takes features as input and predicted labels as output. A loss function is defined as ∶ Y × Y → [0, +∞). A data distribution is defined as P (X, Y ) which means a joint distribution on X × Y. The context of supervised learning usually assumes that there exists a set of training samples The goal is to find a set of optimal parameters Θ which minimize the risk E (x,y)∼P te [ (f Θ (x), y)], where P te means test data distribution. Since we only have access to the training distribution P tr (X, Y ) via the dataset S, the test distribution P te remains unseen to us. We instead search a predictor minimizing the empirical risk which assumes that the training samples and test samples are both independent and identically distributed (IID) [47].
Out-of-Distribution Data. Although the IID assumption allows us to train a model more conveniently, this assumption is difficult to be satisfied in reality. The distribution of test data can be distinct from the distribution of training data (i.e., out-of-distribution) [38], namely

Three Types of Distribution Shift
Some works assume that the cause of the distribution shift is covariate shift [5], that is P tr (Y |X) = P te (Y |X) and P tr (X) ≠ P te (X), of which we think as not comprehensive. In general, for most deep learning tasks, we categorize features into relevant features X * and irrelevant fea-turesX. As shown in the Figure 1a, the relevant features determine the real correspondence of input data and label. For example, determining which species a cat belongs to is relevant to its pattern and coat, not to its background, nor whether it is jumping or sleeping. We further make some reasonable assumptions to simplify Equation 2. Assumption 2. Relevant features and irrelevant features are conditionally independent when label is given, denoted as Assumption 3. There is no label shift [12] between training data and test data, denoted as P tr (Y ) = P te (Y ).
With these assumptions, we can rewrite P (X, Y ) as and the essential distribution shift between training and test data is the shift of P (X, Y ) between them. According to the joint probability formula, we have: Therefore, there are three ways to construct OOD test set, that is, distribution shift between training and test data can be divided into three types: • 1) Marginal distribution shift onX. As shown in Figure 1b, P (X) changes while P (Y |X) remains unchanged; • 2) Conditional distribution shift on Y whenX is given. As shown in Figure 1c, P (Y |X) changes while P (X) remains unchanged; • 3) Joint distribution shift, a combination of 1) and 2). P (Y |X) and P (X) both change, as shown in Figure  1d. 1 There are some works devoted to studying the situation where P (X * , Y ) changes, such as concept drift [9], which is not included in the discussion of this work. Taking a binary classification task of dogs and cats as an example, we consider backgrounds as irrelevant features. Suppose the backgrounds have four types: in cage, at home, on grass and on snow. If available data only cover one or two types of background, there occurs marginal distribution shift. If we get all four types of background, but the backgrounds are correlated with the labels (e.g., cats all in cages or at home, dogs all on grass or snow), a conditional distribution shift occurs. The joint distribution shift occurs when it suffers both the above situations. For example, the backgrounds of available data are not full-scale and partly correspond to labels. As depicted in Figure 1, in these cases, the model can learn red lines that deviate from optimum blue ones, which achieve high accuracy on in-distribution data but cannot generalize to out-of-distribution data.

OOD Dataset Setting
To investigate how models based on the IID assumption perform on OOD data, we choose two simple image classification datasets, CelebA [30] and Colored MNIST [3], on which we construct the above three types of distribution shift.
• CelebA [30] is a large-scale face dataset, each image with 40 attributes, and always used in Fairness area [37,49]. In our experimental setting, we pick genders as labels and select 9,600 images in which the proportion of male and female is balanced.  Figure 2a.
• Colored MNIST [3] is a colored MNIST handwritten digits dataset. In our experiment, we expand the digits colors of [3] to four types: red, yellow, green, and  blue. The number of each digits category is the same as the original MNIST [27], and each color accounts for a quarter in each digit category, roughly 7,500 samples per color. Similar to the setting of CelebA, we construct three pairs of datasets to correspond to three distribution shifts. See detailed settings in Figure 2b.

The Problem of ID Test
One of the main goals of training deep network models is to make the models generalize in the wild. As obtaining in-the-wild test data is expensive and time-consuming, most works use ID test data, that is, randomly splitting a portion of the training data as test data, to evaluate the models. In this section, we evaluate the performance of several ID test-based models under different distribution shifts and find several shortcomings of the ID test. See results in Table  1 and Figure 3.
Accuracy of ID Test is Not Accurate. Similar to results of [18,20,40], models with high accuracy on in-distribution data can fail on new distribution. From the results in Table 1, there shows a significant drop of accuracy in models when testing on OOD data. The accuracy drops range from 7.78% to 77.23% on CelebA and 48.85% to 100% on Colored MNIST. This demonstrates that the performance of the ID test cannot represent the real performance of the model, while the OOD test can reflect it to some extent. Furthermore, the models decline differently on the OOD test sets with different distribution shifts. Taking CelebA as a detailed example, the average drops of models accuracy under marginal distribution shift is 10.26%, while under conditional and joint distribution shifts, the number is 71.41% and 30.23% respectively, both higher than the first. This illustrates that different distribution shifts impair models' performance in different degrees, so we need different types of OOD data for testing comprehensively. In short, the accuracy of ID test is not accurate, and we need OOD test to address this problem.  cannot represent the real accuracy of the models, we hope that the rank of ID accuracy of models can help us pick the best one. However, in our experiment, we find that the ID rank of the models is not robust, i.e., cannot represent the OOD rank. For instance, Figure 3b shows that under conditional distribution shift the Vgg11 model has the lowest ID rank, but a relatively high OOD rank (only lower than ResNeXt50). Some prior works show that a model with high accuracy on in-distribution data will also achieve relatively high accuracy on out-of-distribution data [31,40] which is inconsistent with our finding. We think they come to this result because the distribution shift between the OOD test data and training data they used is very small. Under our dataset partition settings, the marginal and conditional distribution shifts we construct are extreme, while the joint distribution shift is less extreme. Figure 3 shows that the ID ranks and OOD ranks of the models barely coincide under the marginal and conditional distribution shifts, while under the joint distribution shift the ranks almost coincide. In summary, ID test results cannot compare between different models' relative OOD ranks, and the ranks are not consistent across different types of distribution shift, so we cannot only use a single OOD test set to evaluate the real performance of models.

Reasons that ID Test Fails
To better guide us to generate practical OOD test set, we need to know what causes the failure of ID test. We conclude that ID test fails as a result of two different spurious correlations, which are 1) marginal spurious correlation and 2) conditional spurious correlation. These two spurious correlations incur performance drop of models under marginal and conditional distribution shifts, respectively. Definition 1. Given a dataset D with an irrelevant featurẽ x ∈X which makes the conditional probability of any two labels y i , y j equal, namely p(y i |x) = p(y j |x). If a model trained on D usex as a discriminative feature when making decisions, we call the model has learned a marginal spurious correlation.

Definition 2. Given a dataset D with an irrelevant featurẽ
x ∈X, there exists two y i , y j whose conditional probability are not equal, namely p(y i |x) ≠ p(y j |x). If a model trained on D usex as a discriminative feature when making decisions, we call the model have learned a conditional spurious correlation. Figure 4 shows an example, where y represents the label of the image and a represents the specific attribute of the irrelevant feature hair color. The top half of the Figure 4 shows that the model trained on the dataset with all black hairs has a drop in predictions probability when testing in dataset with all gray hairs (i.e., marginal spurious correlation); at the bottom half, the gender and hair color in test data has the opposite correspondence with the training set, and the model's prediction result is also the opposite (i.e., conditional spurious correlation).

Marginal Spurious Correlation
From the results in Table 1, it can be seen that the accuracy of models declines on the OOD test set with marginal distribution shift, which is not in line with human intuition. Intuitively, the models should not use an irrelevant feature as a discriminative feature when the irrelevant feature and label are not correlated in the training data. In other words the model performance should not be affected even if the distribution of the irrelevant feature shifts in the test set. We suspect the reason for the experimental phenomenon contradicting intuition is that the deep models have learned the marginal spurious correlation.
To verify this point, we construct two groups of data based on MNIST [27]. The first group randomly selects 1,000 images from MNIST and creates 4 copies of different colors: red, yellow, green and blue. So these four copies are consistent except for digits colors. The second group uses the full MNIST samples, and the color settings are the same as the first group. We train a ResNet18 model and a 2-layer CNN model on the above two groups of data, using one copy for training each time, and the other copies for testing. We report ID validation accuracy, OOD test accuracy and the average drop rate of the two models in Table 2.  The results in Table 2 show that: 1) whether the model size is large or small, performance drops when training on one color and testing on other colors, indicating that the models have learned marginal spurious correlations; 2) as [18,45] find that diverse training data can improve models' robustness to image corruptions and dataset shift, we find the increase of training data reduce the models' learning of marginal spurious correlations. For example, the accuracy of the ResNet18 trained on R-Full drops by an average of 20.4% in other color settings, which is 1/3 of that trained on R-1K; 3) while [18] suggests that larger models can reduce the gap between ID accuracy and OOD accuracy, we find that larger models are more likely to learn marginal spurious correlation than small models. For example, the average drop rate of the 2-layer CNN model on OOD test sets is much smaller than that of ResNet18.
To explain why the model exhibits such counter-intuitive behavior, part of the clues already exist in [33,44]: the gradient descent-based optimization algorithm used for model training can have a slow logarithmic rate of convergence, and it usually cannot find the optimal parameters. Even if an irrelevant feature does not correlate with the label in the data, the model trained by gradient descent can unintentionally use it as a discriminative feature. See illustration in Figure 1b.

Conditional Spurious Correlation
The models are greatly affected by the conditional distribution shift. For example, ResNet18 can achieve the accuracy of 99.36% on ID validation set of CelebA but only the accuracy of 27% on the OOD test set. We consider that the model intentionally learns the conditional spurious correlation employing incorrect feature-label correlations in the training set, and relies heavily on it when testing. To further explore how conditional spurious correlation affects model performance and how it differs from marginal spurious correlation, we report the accuracy for the first 1,000 iterations of ResNet18 with a batch size of 16 in two OOD settings (marginal and conditional distribution shifts). As shown in Figure 5b, the model shows a polarization trend at the very beginning, and achieves ID accuracy of 95% and OOD accuracy of 20% in the early iterations. This demonstrates that in such an OOD setting, the model learns almost only conditional spurious correlations in early training. In contrast, Figure 5a shows there is a small gap between ID accuracy and OOD accuracy in the later iterations of training which indicates the model has a slower learning rate for marginal spurious correlation. Further, in Figure 5b, after about 200 iterations, the ID accuracy of the model stabilizes and there is a small rebound in OOD accuracy which always stays below 50%. This indicates that as the training progresses, the model can slightly reduces the use of conditional spurious correlations.

Towards OOD Test
While a lot of works are devoted to improving the OOD generalization of models, there is still a great gap in how to evaluate the model performance in handling unseen OOD data. In this section, we propose OOD test paradigms for both scenarios when irrelevant features are known and unknown, using different OOD data settings, and we discuss how to use OOD test results to find bugs in models and guide debugging.

Evaluation with Different Levels and Types of OOD Test Set
Since the models can be affected by spurious correlations during training (see Section 4), we cannot use ID performance to measure the real OOD performance of the model. Although some prior works have already recognized the importance of OOD test [1,2,4,15,46], their test methods are incomplete, such as using only one single OOD test set for evaluation. As distribution shifts in practical scenarios are diverse, we suggest constructing OOD test sets with different levels and types of distribution shift to more comprehensively and accurately evaluate the OOD performance of the models.
To prove the effectiveness, we construct two types and multiple levels distribution shifts on CelebA and Colored MNIST to evaluate the OOD performance of several models. As shown in Figure 6, as the degree of distribution shifts increases, the performance of the models is basically declining. Specifically, as the degree of marginal distribution shifts increases, the model performance decreases at a nearly linear trend, indicating that the model is progressively more influenced by the marginal spurious correlation. However, as the degree of conditional distribution shifts increases, the model is less affected at the beginning and has a sharp drop at the late stage indicates that the model is strongly influenced by conditional spurious correlations in extreme settings. With these results, we can infer the possible performance of these models in practical scenarios, helping us to anticipate the possible risks.

Evaluation with Clustered OOD Test Set
Knowing the labels and specific irrelevant features of the data (e.g., NICO [17] gives both labels and backgrounds of the images), we can directly use the features to construct different types and levels of OOD test sets. But in general, we have only the raw data without irrelevant features that can be used directly. In such a scenario, we suggest clustering the data to construct OOD test sets with implicit distribution shifts.
We use a ResNet50 pre-trained at ImageNet as the feature extractor, and the input vectors of the fully-connected layer as the embedding representation of samples. We then cluster the samples of each category of Colored MNIST into 8 clusters in the embedding space, and combine them in order of cluster size to obtain 8 sub-datasets (D i ) N =8 i . We use one for training and the others for testing, and report ID accuracy and average OOD test accuracy in Table 3. The results show that different clustered OOD test sets bring different degrees of performance decline which demonstrates that clustered OOD data can evaluate the OOD performance of the models to some extent. And we can use these results to compare models to choose the better one. For example, a small model seems a better choice than a large model for the problem of colored handwritten digit recognition.  Figure 7. Employing OOD test to find bugs for deep network models.

Discussions on Finding Bugs for Deep Network Models
Similar to software engineering, finding "bugs" in machine learning, i.e., which irrelevant features significantly undermine the performance, plays a critical role in testing before the model is deployed to production [6]. In current machine learning test, however, finding bugs is largely ignored. One reason is that, machine learning models, especially deep network models conduct end-to-end inference as a black-box fashion, making traditional testing paradigm such as unit testing in software engineering failed to be directly applied. In this section, we discuss the possibility of employing the proposed OOD test paradigm to identify the significantly harmful irrelevant features. The following elaborates the three steps of finding bugs for deep network models. This is illustrated in Figure 7. Irrelevant Feature Candidate Set Preparation. The workflow starts with preparing a candidate set of all possible irrelevant features (x i ) n i ⊆X for the examined task. For example, for the task of image gender classification, we may construct the candidate set consisting of irrelevant features concerning experssion, hair color, wearing, etc. OOD Dataset Construction. Given one irrelevant feature to be tested, e.g.,x i = hair color, we then construct OOD datasets by modifying the distribution ofx i with different types and levels 2 . Evaluation and Finding Bugs. Evaluation can be conducted on the above constructed different settings of OOD datasets. By examining the performance decline in different types/levels of distribution shifts, we can identify bugs in two levels: (1) whether the tested irrelevant feature is bug (i.e., siginicantly undermines the performance), (2) which type of spurious correlation the model learned and mainly relied on.
The above workflow iterates till we test all the candidate irrelevant features to derive the identified bugs for current deep network model. This enables targeted debugging and developing corresponding solutions for more effective model improvement. For example, the identified specific irrelevant feature (e.g., hair color) leads to data augmentation on balancing the distribution or model regularization on penalizing features regarding hair color.

Related Work
As models can significantly fail when the test distribution is distinct from the training distribution [19,22,40,45], a range of advanced algorithms are proposed for OOD generalization, such as invariant representation learning [7,10,13,50], feature disentanglement [28,34,52], adversarial data augmentation [41,48,54]. While these algorithms improve the OOD generalization of models under their own experimental settings, a recent work [14] finds that these algorithms may be overestimated, i.e., most advanced OOD generalization algorithms are only comparable with Empirical Risk Minimization (ERM) [47] when under the same experimental settings. Therefore, in addition to developing algorithms that improve the OOD generalization, it is also important to develop methods that can better evaluate the capability of algorithms to handle unseen OOD data.
A number of benchmarks have been introduced to develop OOD generalization in both CV [8,17,19,28] and NLP [21,31], but most of these benchmarks build only one type of distribution shift. DomainBed proposed by [14] includes several OOD generalization datasets, so it has a richer type of distribution shift, but not multi-level distribution shifts. This suggests that we need a new testing paradigm to test the OOD generalization capability of the models.

Conclusion
We propose practical OOD test paradigms to evaluate the OOD performance of the model and a workflow for finding bugs that can bring guidelines for model debugging. To accomplish this, we first introduce a unified categorization perspective of distribution shift between the observed training data and unseen test data, and find the problem of ID test: the ID performance cannot represent the OOD performance. We then ascribe the reason for IID test failures to marginal and conditional spurious correlation. We expect the test paradigms and workflow can lead to more focusing on the testing of models' capabilities to handle OOD data.