Title: TEST-TIME TRAINING FOR GENERALIZATION UNDER DISTRIBUTION SHIFTS

Abstract: We introduce a general approach, called test-time training, for improving the performance of predictive models when training and test data come from different distributions. Test-time training turns a single unlabeled test instance into a selfsupervised learning problem, on which we update the model parameters before making a prediction on this instance. We show that this simple idea leads to surprising improvements on diverse image classification benchmarks aimed at evaluating robustness to distribution shifts. Theoretical investigations on a convex model reveal helpful intuitions for when we can expect our approach to help.

1. INTRODUCTION:

Supervised learning remains notoriously weak at generalization under distribution shifts. Unless training and test data are drawn from the same distribution, even seemingly minor differences turn out to defeat state-of-the-art models. Transfer learning, domain adaptation and adversarial robustness are but a few existing paradigms that anticipate differences of sorts between the training and test distribution. In this work, we explore a new take on generalization, without any mathematical structure or data available at training-time to anticipate the distribution shifts.

We start from a simple observation. The unlabeled test instance x presented at test-time gives us a hint about the distribution from which it was drawn. Our approach, called test-time training, allows the model parameters θ to depend on the test instance x, but not its unknown label y. This variable decision boundary θ(x) is powerful in theory (see Appendix A), and raises new challenges in practice that we only begin to address here.

Our algorithm creates a self-supervised learning problem based on this single test instance x, updating θ at test-time before making a prediction. Self-supervised learning uses an auxiliary task that automatically creates labels from unlabeled inputs. For the visual data we work with, the task rotates an image x by a multiple of 90 degrees, and assigns the angle as the label (Gidaris et al., 2018) .

Our approach can also be easily modified to work outside the standard supervised learning setting. If several test instances arrive in a batch, we can use the entire batch for test-time training. If test instances arrive online sequentially, we obtain further improvements by keeping the state of the parameters. After all, prediction is rarely a single event. The online setting can be the natural mode of deployment in practice, and shows the strongest improvements.

We experiment with test-time training for generalization under distribution shifts in the context of object recognition on several benchmarks. These include images with diverse types of corruption at various levels (Hendrycks & Dietterich, 2019) , video frames of moving objects (Shankar et al., 2019) , and a new test set of unknown shifts collected by Recht et al. (2018) . Our algorithm does not hurt on the original distribution, but makes substantial improvements under distribution shifts.

In all our experiments, we compare with a strong baseline (labeled joint training) that uses both supervised and self-supervised learning at training-time, but keeps the model fixed at test-time. Very recent work shows that additional training-time self-supervision improves robustness Hendrycks et al. (2019b) . The joint training baseline we use corresponds to an improved implementation of their work. We are also inspired by Sun et al. (2019) , which performs joint training with a selfsupervised task in the context of unsupervised domain adaptation, simultaneously on the source and target domain. A comprehensive review of related work follows in section 5.

We complement the empirical results with theoretical investigations in section 4, of when test-time training is expected to help on a convex model, and establish an intuitive sufficient condition, which, roughly speaking, is to have correlated gradients between the loss functions of the two tasks.

2. METHOD:

Next we describe the algorithmic details of our method. To setup notations, consider a standard Klayer neural network with parameters θ k for layer k. The stacked parameter vector θ = (θ 1 , . . . , θ K ) specifies the entire model for a classification task with loss function l m (x, y; θ) on the test instance (x, y). We call this the main task, as indicated by the subscript of the loss function.

We assume to have training data (x 1 , y 1 ), . . . , (x n , y n ) drawn i.i.d. from a distribution P . Standard empirical risk minimization corresponds to solving the optimization problem:

min θ 1 n n i=1

l m (x i , y i ; θ).

(1)

Our method requires a self-supervised auxiliary task with loss function l s (x). In this paper, we choose the rotation prediction task (Gidaris et al., 2018) , which has been demonstrated to be simple and effective at feature learning for neural networks. The task simply rotates x on the image plane by one of 0, 90, 180 and 270 degrees and have the model predict the angle of rotation as a four-way classification problem. Other self-supervised tasks in section 5 might also be used for our method.

The auxiliary task shares some of the model parameters θ e = (θ 1 , . . . , θ κ ) up to a certain κ ∈ {1, . . . , K}. We think of those κ layers as a shared feature extractor. The auxiliary task uses its own task-specific parameters θ s = (θ κ+1 , . . . , θ K ). We call the unshared parameters θ s the selfsupervised task branch, and θ m = (θ κ+1 , . . . , θ K ) the main task branch. Pictorially, the joint architecture is a Y -structure with a shared bottom and two branches. For our experiments, the selfsupervised task branch has the exact same architecture as the main branch, except for the output dimensionality of the last layer due to the different number of classes in the two tasks.

Training is done in the fashion of multi-task learning (Caruana, 1997) ; the model is trained on both tasks on the same data drawn from P . Losses for both tasks are added together, and gradients are taken for the collection of all parameters. The joint training problem is therefore min θe,θm,θs

EQUATION

Now we describe the standard version of test-time training on a single test instance x. Simply put, test-time training finetunes the shared feature extractor θ e by minimizing the auxiliary task loss on x. This can be formulated as min θe l s (x; θ s , θ e ).

(3) Denote θ * e the (approximate) minimizer of Equation 3. The model then makes a prediction using the updated parameters θ(x) = (θ * e , θ m ). Empirically, the difference is negligible between minimizing Equation 3 over θ e versus over both θ e and θ s . Theoretically, there exists a difference only when optimization is done with more than one step of gradient descent.

In the standard version of our method, the optimization problem in Equation 3 is always initialized with parameters θ = (θ e , θ s ) obtained by minimizing Equation 2 on data from P . After making a prediction on x, θ * e is discarded. Outside of the standard supervised learning setting, when the test instances arrive online sequentially, the online version of test-time training solves the same optimization problem as in Equation 3 to update the shared feature extractor θ e . However, on test instance x t , θ is instead initialized with θ(x t-1 ) updated on the previous instance x t-1 . This allows θ(x t ) to take advantage of the distributional information available in x 1 , . . . , x t-1 as well as x t .

Test-time training naturally benefits from standard data augmentation techniques. On each test instance x, we perform the exact same set of random transformations as used for data augmentation during training to form a batch only containing these augmented copies of 

3. EMPIRICAL RESULTS:

We experiment with both versions of our method (standard and online) on three kinds of benchmarks for distribution shifts, presented here in the order of visually low to high-level, which is roughly also the order of easy to hard. Our code will be released once the paper is accepted.

Network details. Our architecture and hyper-parameters are consistent across all experiments. We use Residual Networks (ResNets) (He et al., 2016b) , which are constructed differently for CIFAR-101 (26-layer) and ImageNet 2 (18-layer). ResNets on ImageNet have four groups, each containing convolutional layers with the same number of channels and size of feature maps; our splitting point is the end of the third group. ResNets on CIFAR-10 have three groups; our splitting point is the end of the second group. In addition, Batch Normalization (BN), a popular module in deep networks, is ineffective when training on small batches, for which the estimated batch statistics are less accurate (Ioffe & Szegedy, 2015) . This technicality hurts test-time training since each batch only contains (augmented) copies of a single image. Therefore our networks instead use Group Normalization (GN) (Wu & He, 2018) , which achieves similar performance as BN on large batches without hurting on small ones. Results with BN are shown in Appendix E for completeness.

Optimization details. For Equation 2, optimization hyper-parameters are set to the default 3 in standard practice (Huang et al., 2016; He et al., 2016a) . For Equation 3, we use stochastic gradient descent (SGD) with the learning rate set to that of the last epoch during training, which is 0.001 in all our experiments. Following standard practice (He et al., 2018; Liu et al., 2018) known to improve performance when finetuning, we do not use weight decay or momentum. For the standard version, we take ten gradient steps, using batches independently generated by the same image. For online we take only one step. The computational aspects of our method are discussed in Appendix C. Following standard practice, the transformations used for data augmentation are random crop with padding and random horizontal flip for CIFAR-10 ( Guo et al., 2017a; Huang et al., 2016) , and random resized crop and random horizontal flip for ImageNet (Ioffe & Szegedy, 2015; He et al., 2016a) . Specifically, these transformations do not contain information about the distribution shifts.

In all the tables and figures, baseline refers to the plain ResNet model (using version as the average over a sliding window of 100 samples; test-time learning online generalizes better as more samples are tested on, without hurting on the original distribution. We use accuracy instead of error here because the baseline performance is very poor with most corruptions. See subsection 3.1 for details.

3.1 COMMON CORRUPTIONS Hendrycks & Dietterich (2019) propose to benchmark robustness of neural networks on 15 types of corruptions from four broad categories: noise, blur, weather and digital. Each corruption type comes in five levels of severity, with level 5 the most severe (details and sample images in Appendix D). The corruptions are algorithmically simulated to mimic real-world corruptions as much as possible on copies of the test set for both CIFAR-10 and ImageNet. According to the authors, training should be done on the original training set, and the diversity of corruption types should make it difficult for any method to work well across the board if it relies too much on corruption specific knowledge.

CIFAR-10-C. Our results on the level 5 corruptions (most severe) are shown in Figure 9 . Due to space constraints, our results on levels 1-4 are shown in Appendix E. Across all five levels and 15 corruption types, both versions of test-time training always improve over the baseline by a large margin. The standard version of test-time training always improves over joint training, and the online version often improves very significantly (>10%) over joint training and never hurts by more than 0.2%. Specifically, test-time training online contributes >24% on the three noise types and 38% on pixelation. For the seemingly unstable setup of a learning problem that abuses a single image, this kind of consistency is rather surprising.

The baseline ResNet-26 has error 8.9% on the original test set of CIFAR-10. The joint training baseline actually improves performance on the original to 8.1%. Most surprisingly, unlike many other methods that tradeoff original performance with robustness, test-time training further improves on the original test set by 0.2% consistently over many independent trials. This indicates that our method does not choose between specificity and generality.

Separate from our method, it is interesting to note that joint training consistently improves over the baseline, as discovered by Hendrycks et al. (2019b) . Hendrycks & Dietterich (2019) We report accuracy for each class and the average over all samples. See subsection 3.2 for details.

all levels of CIFAR-10-C are shown in Appendix E, along with its implementation details. While surprisingly robust under some of the most severe corruptions (especially the three noise types), ALP incurs a much larger error (by a factor of two) on the original distribution and some corruptions (e.g. all levels of contrast and fog), and hurts performance significantly when the corruptions are not as severe (especially on levels 1-3); this kind of tradeoff is to be expected for methods based on adversarial training, but not test-time training.

ImageNet-C. Our results on the level 5 corruptions (most severe) are shown in Figure 2 . We use accuracy instead of error for this dataset because the baseline severely underperforms with most corruptions. The general trend is roughly the same as on CIFAR-10-C. Test-time training (standard version) always improves over the baseline and joint training, while the online version only hurts on the original by 0.1% over the baseline, but dramatically improves (by a factor of more than three) on many of the corruption types.

In the lower panel of Figure 2 , we visualize how the accuracy (averaged over a sliding window) of the online version changes as more images are tested on. Due to space constraints, we show this plot on the original test set, as well as every third corruption type, following the same order as in the original paper. On the original test set, there is no visible change in performance after updating on the 50,000 samples. With corruptions, accuracy has already risen significantly after 10,000 samples, but is still rising towards the end of the 50,000 2 . Again, we use accuracy instead of error because the baseline performance is poor.

In addition, we take the seven classes in VID that overlap with CIFAR-10, and rescale those video frames to the size of CIFAR-10 images, as a new test set for the model trained on CIFAR-10 in the previous subsection. Again, we apply our method to this dataset without any modification. Our results are shown in Table 1 , with a breakdown for each class. Noticing that test-time training does not improve on the airplane class, we inspect some airplane samples, and observe that most of them have black margins on the sides, which provide a trivial hint for the rotation prediction task. In addition, for airplanes captued in the sky, it is often impossible even for humans to tell if an image is rotated. This shows that our method requires the self-supervised task to be both well defined and non-trivial on the new domain. 

4. TOWARDS UNDERSTANDING TEST-TIME TRAINING:

This section contains our preliminary study of when and why test-time training is expected to work.

For convex models, we prove that positive gradient correlation between the loss functions leads to better performance on the main task after test-time training. Equipped with this insight, we then empirically demonstrate that gradient correlation governs the success of test-time training on the deep learning model discussed in Section 3.

Before stating our main theoretical result, we first illustrate the general intuition with a toy model. Consider a regression problem where x ∈ R d denotes the input, y 1 ∈ R denotes the label, and the objective is the square loss (ŷ -y 1 ) 2 /2 for a prediction ŷ. Consider a two layer linear network parametrized by A ∈ R h×d and v ∈ R h (where h stands for the hidden dimension). The prediction according to this model is ŷ = v Ax, and the main task loss is

EQUATION

In addition, consider a self-supervised regression task that also uses the square loss and automatically generates a label y s for x. Let the self-supervised head be parameterized by w ∈ R h . Then the selfsupervised task loss is

EQUATION

)

Now we apply test-time training to update the shared feature extractor A by one step of gradient descent on l s , which we can compute with y 2 known. This gives us

EQUATION

)

where A is the updated matrix and η is the learning rate. If we set η = η * where

EQUATION

then with some simple algebra, it is easy to see that the main task loss l m (x, y 1 ; A , v) = 0. Concretely, test-time training drives the main task loss down to zero with a single gradient step for a carefully chosen learning rate. In practice, this learning rate is unknown since it depends on the unknown y 1 . However, since our model is convex, as long as η * is positive, it suffices to set η to be a small positive constant (see Lemma 1). If x = 0, one sufficient condition for η * to be positive (when neither loss is zero) is to have

EQUATION

)

For our toy model, both parts in Equation 8have an intuition interpretation. The first part says that the mistakes should be correlated, in the sense that predictions from both tasks are mistaken in the same direction. The second part, v w > 0, says that the decision boundaries on the feature space should be correlated. In fact, these two parts hold iff ∇l m (A), ∇l s (A) > 0 (see Lemma 2). To summarize, if the gradients have positive correlation, test-time training is guaranteed to reduce the main task loss. Our main theoretical result extends this to general smooth and convex loss functions.

Theorem 1. Let l m (x, y; θ) denote the main task loss on test instance x, y with parameters θ, and l s (x; θ) the self-supervised task loss that only depends on x5 . Assume that for all x, y, l m (x, y; θ) is differentiable, convex and β-smooth in θ, and both ∇l m (x, y; θ) , ∇l s (x, θ) ≤ G for all θ.

With a fixed learning rate η = βG 2 , for every x, y such that ∇l m (x, y; θ), ∇l s (x; θ) > ,

we have

EQUATION

where θ(x) = θ -η∇l s (x; θ) i.e. test-time training with one step of gradient descent.

The proof is in Appendix B.2. Theorem 1 reveals gradient correlation as a determining factor of the success of test-time training in the smooth and convex case. In Figure 3 , we empirically show that our insight also holds for non-convex loss functions, on the deep learning model and across the diverse set of corruptions considered in section 3; stronger gradient correlation clearly indicates higher performance improvements over the baseline.

5. RELATED WORK:

Our work has been influenced by the successes and limitations of many related fields. Each of these fields contains maybe hundreds of interesting works, which we unfortunately do not have enough time and space to acknowledge in this draft. We apologize for the omissions and are happy to include additional citations upon request.

Learning (Guo et al., 2017b; Song et al., 2017 ) are shown to be less effective (Athalye et al., 2018) . There are two main problems in this field. First, all the approaches mentioned above can be seen as smoothing the decision boundary. This establishes a theoretical tradeoff between accuracy and robustness (Tsipras et al., 2018; Zhang et al., 2019) , which we also observe empirically with our adversarial training baseline in Section 3. Intuitively, the more diverse ∆ is, the less effective this one-boundary-fits-all approach can be for a particular element of ∆. Second, adversarial methods rely heavily on the mathematical structure of ∆, which might not accurately model perturbations in the real world. Therefore, generalization remains hard outside of the ∆ we know in advance or can mathematically model, especially for non-adversarial distribution shifts. Empirically, Kang et al. (2019) shows that robustness for one ∆ might not transfer to another, and training on the L ∞ ball actually hurts robustness on the L 1 ball.

Non-adversarial robustness studies the effect of corruptions, perturbations, out-of-distribution examples, and real-world distribution shifts (Hendrycks et al., 2019a; b; 2018; Hendrycks & Gimpel, 2016) . Geirhos et al. (2018) show that training on images corrupted by Gaussian noise makes deep learning models recover super-human performance on this particular noise type, but cannot improve performance on another those corrupted by another noise type e.g. salt-and-pepper noise.

Unsupervised domain adaptation (a.k.a. transfer learning) studies the problem of distribution shift (from P to Q), when unlabled data from Q is available at training-time (Tzeng et al., 2017; Ganin et al., 2016; Gong et al., 2012; Long et al., 2016; Chen et al., 2018; 2011; Hoffman et al., 2017; Csurka, 2017; Long et al., 2015) . We are inspired by this very active and successful community, especially (Sun et al., 2019) , and believe that progress in this community can motivate new algorithms in the framework of test-time learning. Our update rule can be viewed as performing one-sample unsupervised domain adaptation on the fly6 . On the other hand, test-time learning comes from realizing the limitations of the unsupervised domain adaptation setting, that outside of the specific target distribution where data is available for training, generalization is still elusive. Previous works make the source and target distributions broader with multiple and evolving sources and targets without fundamentally address this problem (Hoffman et al., 2018; 2012; 2014) .

Self-supervised learning studies how to create labels from the data, by designing ingenious tasks that contain semantic information without human annotations, such as context prediction (Doersch et al., 2015) , solving jigsaw puzzles (Noroozi & Favaro, 2016) , colorization (Larsson et al., 2017; Zhang et al., 2016) , noise prediction (Bojanowski & Joulin, 2017) , and feature clustering (Caron et al., 2018) . Self-supervision has also been used on videos (Wang & Gupta, 2015; Wang et al., 2019) . Particularly relevant to our work, Asano et al. (2019) shows that self-supervised learning on only a single image, surprisingly, can produce low-level features that generalize well. In addition, Hendrycks et al. (2019b) proposes that jointly training a main task and a self-supervised task (our joint training baseline in section 3) can improve robustness of the main task. The same idea is used in few-shot learning (Su et al., 2019) , and domain generalization (Carlucci et al., 2019) .

Domain generalization studies when a meta distribution generates multiple environment distributions, some of which are available during training (source), while others are used for testing (target) (Li et al., 2018; Shankar et al., 2018; Muandet et al., 2013; Balaji et al., 2018; Ghifary et al., 2015; Motiian et al., 2017; Li et al., 2017a; Gan et al., 2016) . With only a few environments, information on the meta distribution is often too scarce to be helpful, and with many environments, we are back to the i.i.d. setting where each environment can be seen as a sample, and a strong baseline is to simply train on all the environments (Li et al., 2019) . The setting of domain generalization is limited by the inherent tradeoff between specificity and generality of a fixed decision boundary, and the fact that generalization is again elusive outside of the meta distribution i.e. the actual P .

Continual learning (a.k.a. learning without forgetting) studies when a model is made to learn a sequence of tasks, and not forget about the task at the beginning (Li & Hoiem, 2017; Lopez-Paz & Ranzato, 2017; Kirkpatrick et al., 2017; Santoro et al., 2016) . Test-time training does not care about forgetting the past test instances since they have already been evaluated on; and if a past instance comes up by any chance, it would go through test-time training again. In addition, the impact of forgetting the training set is minimal, because both tasks have been jointly trained. This is in contrast to continual learning, when the tasks are trained one-by-one from scratch.

Few (one)-shot learning studies extremely small training sets (maybe for some categories) (Snell et al., 2017; Vinyals et al., 2016; Fei-Fei et al., 2006; Ravi & Larochelle, 2016; Li et al., 2017b; Finn et al., 2017; Gidaris & Komodakis, 2018) . Our update rule can be viewed as performing one-shot self-supervised learning and can potentially be improved by progress in few-shot learning.

Online learning (a.k.a. online optimization) is a well-studied area of learning theory (Shalev-Shwartz et al., 2012; Hazan et al., 2016) . The basic setting repeats the following: receive x t , predict ŷt , receive y t from a worst-case oracle and learn. Final performance is evaluated using the regret, colloquially how much worse than the best fixed model in hindsight. It is easy to see how our setting differs, even for the online version. We learn before predicting ŷt , but never receive any y t that is evaluated on, thus do not need to consider the worst-case oracle or the regret.

6. CONCLUSION:

The idea of test-time training also makes sense for other tasks such as segmentation and detection, and in other fields such as speech recognition and natural language processing. More generally, we hope this paper can encourage researchers to abandon the self-imposed constraint of a fixed decision boundary at test-time, or even altogether, the artificial divison between training and testing.

A INFORMAL DISCUSSION ON OUR VARIABLE DECISION BOUNDARY:

In section 1, we claim that in traditional supervised learning θ gives a fixed decision boundary, while our θ gives a variable decision boundary. Here we informally discuss this claim.

Denote the input space X and output space Y. A decision boundary is simply a mapping f : X → Y.

Let Θ be a model class e.g R d . Now consider a family of parametrized functions g θ : X → Y, where θ ∈ Θ. In the context of deep learning, g is the neural network architecture and θ contains the parameters. We say that f is a fixed decision boundary w.r.t. g and Θ if there exists θ ∈ Θ s.t. f (x) = g θ (x) for every x ∈ X , and a variable decision boundary if for every x ∈ X , there exists θ ∈ Θ s.t. f (x) = g θ (x). Note how selection of θ can depend on x for a variable decision boundary, and cannot for a fixed one. It is then trivial to verify that our claim is true under those definitions.

A critical reader might say that with an arbitrarily large model class, can't every decision boundary be fixed? Yes, but this is not the end of the story. Let d = dim(X ) × dim(Y), and consider the enormous model class Θ = R d which is capable of representing all possible mappings between X and Y. Let g θ simply be the mapping represented by θ ∈ Θ . A variable decision boundary w.r.t. g and Θ then indeed must be a fixed decision boundary w.r.t. g and Θ , but we would like to note two things. First, without any prior knowledge, generalization in Θ is impossible with any finite amount of training data; reasoning about g and Θ is most likely not productive from an algorithmic point of view, and the concept of a variable decision boundary is to avoid such reasoning. Second, selecting θ based on x for a variable decision boundary can be thought of as "training" on all points x ∈ R d ; however, "training" only happens when necessary, for the x that it actually encounters.

Altogether, the concept of a variable decison boundary is different from what can be described by traditional learning theory. A formal discussion is beyond the scope of this paper.

B PROOFS:

Here we prove the theoretical results covered in section 4.

B.1 PROOFS FOR THE TOY PROBLEM:

The following setting applies to the two lemmas; this is simply the setting of our toy problem, reproduced here for ease of reference. Consider a two layer linear network parametrized by A ∈ R h×d (shared) and v, w ∈ R h (fixed) for the two heads, respectively. Denote x ∈ R d the input and y 1 , y 2 ∈ R the labels for the two tasks, respectively. For the main task loss

EQUATION

and the self-supervised task loss

EQUATION

test-time learning yields an updated matrix

EQUATION

)

where η is the learning rate. Lemma 1. Following the exposition of section 4, denote

EQUATION

)

Assume η * ∈ [ , ∞) for some > 0. Then for any η ∈ (0, ], we are guaranteed an improvement on the main loss i.e. l m (A ) < l m (A).

Proof. From the exposition of section 4, we know that

l m (A -η * ∇l s A)) = 0,

which can also be derived from simple algebra. Then by convexity, we have

EQUATION

< l m (A), (18) where the last inequality uses the assumption that l m (A) > 0, which holds because η * > 0.

Lemma 2. Define U , V = vec (U ) vec (V ) i.e. the Frobenious inner product, then sign (η * ) = sign ( ∇l m (A), ∇l s (A) ) .

Proof. By simple algebra,

EQUATION

which has the same sign as η * .

B.2 PROOF OF THEOREM 1:

For any η, by smoothness and convexity, l m (x, y; θ(x)) = l m (x, y; θ -η∇l s (x; θ))

EQUATION

Denote η * = ∇l m (x, y; θ), ∇l s (x, θ)

β ∇l s (x; θ) 2 .

Then Equation 23 becomes l m (x, y; θ -η * ∇l s (x; θ)) ≤ l m (x, y; θ) -∇l m (x, y; θ), ∇l s (x, θ) 2

EQUATION

)

And by our assumptions on the gradient norm and gradient inner product,

EQUATION

)

Because we cannot observe η * in practice, we instead use a fixed learning rate η = βG 2 , as stated in Theorem 1. Now we argue that this fixed learning rate still improves performance on the main task.

By our assumptions, η * ≥ βG 2 , so η ∈ (0, η * ]. Denote g = ∇l s (x; θ), then by convexity of l m ,

EQUATION

Combining with Equation 26, we have

EQUATION

Since η/η * > 0, we have shown that l m (x, y; θ) -l m (x, y; θ(x)) > 0.

C COMPUTATIONAL ASPECTS OF OUR METHOD At test-time, our method is 2×batch size×number of iterations times slower than regular testing, which only performs a single forward pass for each sample. As the first work on test-time learning, this paper is not as concerned about computational efficiency as improving robustness, but here we provide two potential solutions that might be useful, but have not been thoroughly verified.

The first is to use the thresholding trick on l s , introduced as a solution for the small batches problem in section 2. For the models considered in our experiments, roughly 80% of the test instances fall below the threshold, so test-time learning can only be performed on the other 20% without much effect on performance, because those 20% contain most of the samples with wrong predictions.

The second is to reduce the number of iterations of test-time updates. For the online version, the number of iterations is already 1, so there is nothing to do. For the standard version, we have done some preliminary experiments setting number of iterations to 1 (instead of 10) and learning rate to 0.01 (instead of 0.001), and observing results almost as good as the standard hyperparameter setting. A more in depth discussion on efficiency is left for future works, which might, during training, explicitly make the model amenable to fast updates. We have also abbreviated the names of the corruptions. The full names are, in order: original test set, Gaussian noise, shot noise, impulse noise, defocus blur, glass blue, motion blur, zoom blur, snow, frost, fog, brightness, contrast, elastic transformation, pixelation, and JPEG compression.

D SAMPLE IMAGES FROM THE COMMON CORRUPTIONS BENCHMARK:



E.1 RESULTS USING BATCH NORMALIZATION:

As discussed in section 3, Batch Normalization (BN) is ineffective for small batches, which are the inputs for test-time training (both standard and online version) since there is only one sample available when forming each batch; therefore, our main results are based on a ResNet using Group Normalization (GN). Here we provide results of our method on CIFAR-10-C level 5, with a ResNet using Batch Normalization (BN). These results are meant to be merely a point of reference for the curious readers, instead of our technical contributions.

In the early stage of this project, we have experimented with two potential solutions to the small batches problem with BN. The naive solution is to fix the BN layers during test-time training. but this diminishes the performance gains since there are fewer shared parameters. The better solution, adopted for the results below, is hard example mining: instead of updating on all inputs, we only update on inputs that incur large self-supervised task loss l s , where the large improvements might counter the negative effects of inaccurate statistics. Test-time training (standard version) is still very effective with BN. In fact, some of the improvements are quite dramatic, such as on contrast (34%), defocus blue (18%) and Gaussian noise (22% comparing to joint-training, and 16% comparing to the baseline). Performance on the original distribution is still almost the same, and the original error with BN is in fact slightly lower than with GN, and takes half as many epochs to converge.

We did not further experiment with BN because of two reasons: 1) The online version does not work with BN, because the problem with inaccurate batch statistics is exacerbated when training online for many (e.g. 10000) steps. 2) The baseline error for almost every corruption type is significantly higher with BN than with GN. Although unrelated to the main idea of our paper, we make the interesting note that GN significantly improves model robustness.

E.2 ADDITIONAL BASELINE: ADVERSARIAL LOGIT PAIRING:

As discussed in subsection 3.1, Hendrycks & Dietterich (2019) point to Adversarial Logit Pairing (ALP) (Kannan et al., 2018) as an effective method for improving model robustness to corruptions and perturbations, even though it was designed to defend against adversarial attacks. We take ALP as an additional baseline on all benchmarks based on CIFAR-10 (using GN), following the training procedure in Kannan et al. (2018) and their recommended hyper-parameters. The implementation of the adversarial attack comes from the codebase of Ding et al. (2019) . We did not run ALP on ImageNet because the two papers we reference for this method, Kannan et al. ( 2018) and Hendrycks & Dietterich (2019) , did not run on ImageNet or make any claim or recommendation.

E.3 RESULTS ON CIFAR-10-C AND IMAGENET-C, LEVEL 5:

The following two tables correspond to the bar plots in section 3. .9 TTT 69.0 3.1 4.5 3.5 10.1 6.8 13.5 18.5 17.1 17.9 20.0 47.0 14.4 20.9 22.8 25.3 TTTO 68.8 26.3 28.6 26.9 23.7 6.6 28.7 33.4 35.6 18.7 47.6 58.3 35.3 44.3 47.8 44.3 Table 6 : Test accuracy (%) on ImageNet-C, level 5, ResNet-18. E.4 RESULTS ON CIFAR-10-C, LEVELS 1-4

The following bar plots and tables are on levels 1-4 of CIFAR-10-C. The original distribution is the same for all levels, so are our results on the original distribution. 8.9 46.4 39.2 44.8 15.3 52.5 19.1 20.5 21.3 26.9 13.3 10.5 13.7 20.8 35.3 26.9 JT 8.1 45.0 38.3 42.2 16.4 50.2 20.7 20.5 21.1 25.4 14.1 10.0 14.7 19.0 33.2 25.1 TTT 7.9 41.5 35.4 39.8 15.0 47.8 19.1 18.4 20.1 24.0 13.5 10.0 14.1 17.7 29.4 24.5 TTTO 8.2 22.9 20.0 23.9 11.2 35.1 15.6 13.8 18.6 15.9 12.3 9.7 11.9 16.7 13.6 19.8 ALP 16.5 21.3 20.5 24.5 20.7 25.9 23.7 21.4 9 20.8 17.3 18.4 25.0 11.4 9.2 12.0 15.2 20.8 22.8 TTT 7.9 37.2 31.6 28.6 11.5 35.8 19.1 15.8 17.8 23.3 11.0 9.1 11.6 14.3 18.9 22.3 TTTO 8.2 21.3 17.7 17.9 9.0 23.4 15.3 12.5 16.4 15.8 10.9 9.0 10.7 12.8 12.2 18.7 ALP 16.5 20.0 19.3 20.5 19.2 21.2 24.0 20.5 20.9 24.2 30.1 16.6 39.6 20.9 17.8 orig gauss shot impul defoc glass motn zoom snow frost fog brit contr elast pixel jpeg B 8.9 31.7 22.6 24.3 9.9 42.6 14.9 14.7 21.7 18.4 9.8 9.1 10.0 13.1 17.1 22.4 JT 8.1 31.0 22.6 23.4 9.1 39.2 16.4 14.2 21.2 17.5 9.4 8.3 10.6 12.8 15.9 20.5 TTT 7.9 28.8 20.7 23.0 9.0 36.6 15.4 13.1 20.2 16.9 9.2 8.3 10.2 12.5 14.8 19.7 TTTO 8.2 16.8 13.8 15.5 8.5 23.4 13.3 11.5 16.8 12.7 9.4 8.4 9.7 12.4 11.5 17.0 ALP 16.5 18.0 17.2 19.0 17.8 20.7 21.2 19.3 19.0 20.1 22.4 16.3 29.2 20.3 17.4 17.8 orig gauss shot impul defoc glass motn zoom snow frost fog brit contr elast pixel jpeg B 8.9 21.7 17.1 17.0 9.0 44.0 12.1 13.9 14.3 13.4 9.2 8.9 9.0 13.2 12.0 17.3 JT 8.1 20.4 16.6 16.9 8.2 40.5 12.2 13.0 13.1 12.3 8.4 8.1 8.5 12.9 11.3 15.9 TTT 7.9 19.1 15.8 16.5 8.0 37.9 11.7 12.2 12.8 11.9 8.2 8.0 8. 3 12.6 11.1 15.5 TTTO 8.2 13.8 11.9 12.2 8.5 24.4 10.5 11.5 12.4 10.7 8.5 8.3 8.6 12.4 10.7 14.4 ALP 17.0 16.8 17.6 16.8 20.9 18.7 19.0 17.3 17.5 17.4 16.1 18.4 20.4 17.0 17.2 Table 10: Test error (%) on CIFAR-10-C, level 1, ResNet-26. 

:

2009) is a standard object recognition dataset with 10 classes of objects in natural scenes. The standard train / test split has 50,000 / 10,000 images, each of size 32-by-32 pixels.2 The ImageNet 2012 classification dataset(Russakovsky et al., 2015) for object recognition has images from 1, 000 classes, 1.2 million for training and 50,000 for validation. Following standard practice(He et al., 2016a;b;Huang et al., 2016), the validation set is used as the test set.3 Namely, we use stochastic gradient descent (SGD) with weight decay and momentum; learning rate starts at 0.1 and is dropped by a factor of ten at two scheduled milestones, to 0.01 and 0.001.

The VID dataset contains 1109 sets of video frames; each set forms a short video clip where all the frames are similar to an anchor frame. Our results are reported on the anchor frames. To map the 1000 ImageNet classes to the 30 VID classes, we use the max-conversion function inShankar et al. (2019).

Because the main task branch and the self-supervised branch are kept fixed at test-time, we do not explicitly describe their parameters, and include them implicitly in the loss functions.

Note that typical unsupervised domain adaptation algorithms,such as those based on distributional discrepancy, adversarial learning, co-training and generative modeling, might not work in our framework because the concept of a target distribution, which has been so deeply rooted and heavily relied on, becomes ill-defined when there is only one sample from the target domain.

Figures and Tables:

Figure 1: Test error (%) on CIFAR-10-C, level 5. See subsection 3.1 for details.

Figure 2: Test accuracy (%) on ImageNet-C, level 5. The lower panel shows the accuracy of the online

Figure3: Scatter plot of the inner product between the gradients (on the shared feature extractor θe) of the main task lm and the self-supervised task le, and the improvement in test error (%) from test-time training, for the standard (left) and online (right) version. Each point is the average over a test set, and each scatter plot has 75 test sets, from all 15 types of corruptions over five levels as described in subsection 3.1. The blue lines and bands are the best linear fits and the 99% confidence intervals. The linear correlation coefficients are 0.93 and 0.89 respectively, indicating strong positive correlation between the two quantities, as suggested by Theorem 1.

Figure 4: Sample images from the Common Corruptions Benchmark, taken from the original paper by Hendrycks & Dietterich (2019).

Figure 6: Test error (%) on CIFAR-10-C, level 4. See subsection 3.1 for details.

Figure 8: Test error (%) on CIFAR-10-C, level 2. See subsection 3.1 for details.

Figure 9: Test error (%) on CIFAR-10-C, level 1. See subsection 3.1 for details.

Figure 10: Sample Images from the VID dataset in subsection 3.2 adapted to CIFAR-10. Each row shows eight sample images from one class. The seven classes shown are, in order: airplane, bird, car, dog, cat, horse, ship.

x for test-time training.

Table content: <table><tr><td>Error (%)</td><td>20 30 40 50</td><td colspan="3">Baseline Joint training Test-time training Test-time training online</td></tr><tr><td/><td>10</td><td/><td/><td/></tr><tr><td/><td>0</td><td/><td/><td/></tr><tr><td/><td/><td>o r i g i n a l g a u s s s h o t i m p u l s e d e f o c u s g l a s s m o t i o n z o o m</td><td>s n o w</td><td>f r o s t</td><td>f o g</td><td>b r i g h t c o n t r a s t e l a s t i c p i x e l a t e j p e g</td></tr></table>

have also experimented with various other training methods on this benchmark, and point to Adversarial Logit Pairing (ALP) (Kannan et al., 2018) as the most effective. Results of this additional baseline on Test accuracy (%) on our video classification dataset, adapted from Shankar et al. (2019).

Table content: <table><tr><td>Accuracy (%)</td><td colspan="3">Airplane Bird Car Dog Cat Horse Ship Average</td></tr><tr><td>Baseline</td><td>67.9</td><td>35.8 42.6 14.7 52.0 42.0 66.7</td><td>41.4</td></tr><tr><td>Joint training</td><td>70.2</td><td>36.7 42.6 15.5 52.0 44.0 66.7</td><td>42.4</td></tr><tr><td>Test-time training</td><td>70.2</td><td>39.2 42.6 21.6 54.7 46.0 77.8</td><td>45.2</td></tr><tr><td>TTT online</td><td>70.2</td><td>39.2 42.6 22.4 54.7 46.0 77.8</td><td>45.4</td></tr></table>

3.3 CIFAR-10.1: A NEW TEST SET WITH UNKNOWN DISTRIBUTION SHIFTS CIFAR-10.1(Recht et al., 2018) is a new test set of size 2000 modeled after CIFAR-10, with the exact same classes and image dimensionality, following the dataset creation process documented by the original CIFAR-10 paper as closely as possible. The purpose is to investigate the distribution shifts present between the two test sets, and the effect on object recognition. All models tested by the authors suffer a large performance drop on CIFAR-10.1 comparing to CIFAR-10, even though there is no human noticable difference, and both have the same human accuracy. This demonstrates how insidious and ubiquitous distribution shifts are, even when researchers strive to minimize them.

Table content: <table><tr><td>The distribution shifts from CIFAR-10 to CIFAR-10.1 pose an extremely difficult problem, and nobody has made a success-ful attempt to improve the performance of an existing model</td><td>Method Baseline</td><td>Error (%) 17.4</td></tr><tr><td>on this new test set, probably because 1) Researchers cannot even identify the distribution shifts, let alone describe them with mathematics. 2) The samples in CIFAR-10.1 are only</td><td>Joint training Test-time training</td><td>16.7 15.9</td></tr><tr><td>revealed at test-time; and even if revealed during training, the</td><td/><td/></tr><tr><td>distribution shifts are too subtle, and the sample size is too</td><td colspan="2">Table 3: Test error (%) on CIFAR-</td></tr><tr><td>small, for domain adaptation algorithms (Recht et al., 2018).</td><td colspan="2">10.1. See subsection 3.3 for details.</td></tr></table>

from its downsampled version. An older work,Jain & Learned-Miller (2011) improves Viola-Jones(Viola et al., 2001) for face detection, by bootstrapping the more difficult faces in an image from the easier ones with high confidence in the same image. The online version of our algorithm is inspired byMullapudi et al. (2018), which makes video segmentation more efficient by using a student model that learns online from a teacher model. The idea of online updates has also been used inKalal et al. (2011) for tracking and detection.Zhu et al. (2019), a concurrent work in echocardiography, improves the deep learning model that tracks myocardial motion and cardiac blood flow with sequential updates. Lastly, we share the philosophy of transductive learning(Vapnik, 2013; Gammerman et al., 1998), but have little in common with their classical algorithms; concurrent workTripuraneni & Mackey (2019) theoretically explores this for linear prediction.Adversarial robustness studies the robust risk: R P,∆ (θ) = E x,y∼P max δ∈∆ l(x+δ, y; θ) where l is some loss function, and ∆ is the set of perturbations; ∆ is often chosen as the L p ball, for p ∈ {1, 2, ∞}. Many popular algorithms formulate and solve this as a robust optimization problem(Goodfellow et al., 2014;Madry et al., 2017;Sinha et al., 2017;Raghunathan et al., 2018;Wong & Kolter, 2017;Croce et al., 2018), and the most well known technique is adversarial training. Another line of work is based on randomized smoothing(Cohen et al., 2019;Salman et al., 2019), while some other approaches such as input transformations

Table content: <table/>

Gaussian Noise Shot Noise Impulse Noise Defocus Blur Frosted Glass Blur

Table content: <table><tr><td>Motion Blur</td><td>Zoom Blur</td><td>Snow</td><td>Frost</td><td>Fog</td></tr><tr><td>Brightness</td><td>Contrast</td><td>Elastic</td><td>Pixelate</td><td>JPEG</td></tr></table>

Test error (%) on CIFAR-10-C, level 5, ResNet-26 with Batch Normalization.

Table content: <table><tr><td/><td>60</td><td colspan="3">Baseline Joint training Test-time training</td><td/></tr><tr><td>Error (%)</td><td>20 40</td><td/><td/><td/><td/></tr><tr><td/><td>0</td><td/><td/><td/><td/></tr><tr><td/><td>o r i g i n a l g a u s s</td><td>s h o t i m p u l s e d e f o c u s g l a s s m o t i o n z o o m</td><td>s n o w</td><td>f r o s t</td><td>f o g</td><td>b r i g h t c o n t r a s t e l a s t i c p i x e l a t e j p e g</td></tr><tr><td/><td colspan="6">Figure 5: Test error (%) on CIFAR-10-C, level 5, ResNet-26 with Batch Normalization.</td></tr><tr><td/><td colspan="6">orig gauss shot impul defoc glass motn zoom snow frost fog brit contr elast pixel jpeg</td></tr><tr><td/><td colspan="6">B 7.9 63.9 58.8 64.3 46.3 54.6 41.6 45.9 31.9 44.0 37.5 13.0 69.2 33.8 61.4 31.7</td></tr><tr><td/><td colspan="6">JT 7.5 70.7 65.6 67.2 43.1 55.4 40.9 42.7 30.3 44.5 42.5 12.7 58.6 30.7 62.6 31.9</td></tr><tr><td colspan="7">TTT 7.9 47.9 45.2 54.8 27.6 50.4 31.5 30.9 28.7 34.3 26.9 12.6 35.2 30.6 51.2 31.3</td></tr></table>

Test error (%) on CIFAR-10-C, level 5, ResNet-26.

Table content: <table><tr><td/><td colspan="2">orig gauss shot impul defoc glass motn zoom snow frost fog brit contr elast pixel jpeg</td></tr><tr><td>B</td><td colspan="2">8.9 50.5 47.2 56.1 23.7 51.7 24.3 26.3 25.6 34.4 28.1 13.5 25.0 27.4 55.8 29.8</td></tr><tr><td>JT</td><td colspan="2">8.1 49.4 45.3 53.4 24.2 48.5 24.8 26.4 25.0 32.5 27.5 12.6 25.3 24.0 51.6 28.7</td></tr><tr><td colspan="3">TTT 7.9 45.6 41.8 50.0 21.8 46.1 23.0 23.9 23.9 30.0 25.1 12.2 23.9 22.6 47.2 27.2</td></tr><tr><td colspan="3">TTTO 8.2 25.8 22.6 30.6 14.6 34.4 18.3 17.1 20.0 18.0 16.9 11.2 15.6 21.6 18.1 21.2</td></tr><tr><td colspan="3">ALP 16.5 22.7 22.9 28.3 25.0 25.6 27.4 23.1 25.2 27.2 64.8 21.7 73.6 23.0 20.2 18.9</td></tr><tr><td/><td colspan="2">orig gauss shot impul defoc glass motn zoom snow frost fog brit contr elast pixel jpeg</td></tr><tr><td>B</td><td>68.9 1.3 2.0 1.3</td><td>7.5 6.6 11.8 16.2 15.7 14.9 15.3 43.9 9.7 16.5 15.3 23.4</td></tr><tr><td colspan="2">JT 69.1 2.1 3.1 2.1</td><td>8.7 6.7 12.3 16.0 15.3 15.8 17.0 45.3 11.0 18.4 19.7 22</td></tr></table>

24.2 23.9 42.2 17.5 53.7 22.1 19.1 18.5 Test error (%) on CIFAR-10-C, level 4, ResNet-26. gauss shot impul defoc glass motn zoom snow frost fog brit contr elast pixel jpeg B 8.9 42.2 35.1 30.7 12.2 41.7 18.6 17.5 19.0 25.3 10.8 9.7 11.6 15.3 21.7 24.6 JT 8.1 40.2 34.4 29.9 12.2 37.

Table content: <table><tr><td>Error (%)</td><td>20 30 40</td><td/><td/><td/><td/><td>Baseline Joint training Test-time training Test-time training online</td></tr><tr><td/><td>10</td><td/><td/><td/><td/></tr><tr><td/><td>0</td><td/><td/><td/><td/></tr><tr><td/><td>o r i g i n a l g a u s s</td><td>s h o t i m p u l s e d e f o c u s g l a s s m o t i o n z o o m</td><td>s n o w</td><td>f r o s t</td><td>f o g</td><td>b r i g h t c o n t r a s t e l a s t i c p i x e l a t e j p e g</td></tr></table>

18.0 Test error (%) on CIFAR-10-C, level 3, ResNet-26.

Table content: <table><tr><td>Error (%)</td><td>20 30 40</td><td/><td/><td/><td/><td>Baseline Joint training Test-time training Test-time training online</td></tr><tr><td/><td>10</td><td/><td/><td/><td/></tr><tr><td/><td>0</td><td/><td/><td/><td/></tr><tr><td/><td>o r i g i n a l g a u s s</td><td>s h o t i m p u l s e d e f o c u s g l a s s m o t i o n z o o m</td><td>s n o w</td><td>f r o s t</td><td>f o g</td><td>b r i g h t c o n t r a s t e l a s t i c p i x e l a t e j p e g</td></tr></table>

Test error (%) on CIFAR-10-C, level 2, ResNet-26.

Table content: <table><tr><td>Error (%)</td><td>20 30 40</td><td/><td/><td/><td/><td>Baseline Joint training Test-time training Test-time training online</td></tr><tr><td/><td>10</td><td/><td/><td/><td/></tr><tr><td/><td>0</td><td/><td/><td/><td/></tr><tr><td/><td>o r i g i n a l g a u s s</td><td>s h o t i m p u l s e d e f o c u s g l a s s m o t i o n z o o m</td><td>s n o w</td><td>f r o s t</td><td>f o g</td><td>b r i g h t c o n t r a s t e l a s t i c p i x e l a t e j p e g</td></tr></table>

Peer review (iclr2020):
The authors propose a method for adapting model parameters by doing self-supervised training on each individual test example. They show striking improvements in out-of-domain performance across a variety of image classification tasks while preserving in-domain performance; the latter is a marked difference from other robustness procedures which tend to sacrifice in-domain performance for out-of-domain (or adversarial) performance. These results are exciting, and I believe that this proposed test-time training method will spur a significant amount of further research into similar approaches. The paper is well-written and the experiments are thorough, so I have no major concerns. Some remaining questions about the proposed approach are: 1) How sensitive is test-time training to hyperparameters like splitting the parameters at the right location (i.e., the particular partitioning of into , , and ), or to the learning rate? Is there a good way to pick these hyperparameters, given that evaluation is on an out-of-domain distribution that we assume we do not have access to at training time? The paper proposes a particular split of parameters and a particular learning rate and number of steps (which differs for standard vs. online training). How were those chosen? 2) How does test-time training compare to methods that assume access to the test distribution? I understand that a big benefit is that test-time training does not need to see the entire distribution (unlike standard domain adaptation approaches). But in cases where we do get to see parts of the test distribution -- say some unlabeled examples from it, or even some labeled examples -- how does test-time training compare? For example, should we see test-time training as providing the benefits of domain adaptation even when we*re unable to access the unlabeled test distribution; or should we see it as doing something beyond what standard domain adaptation methods do, even when we have access to the unlabeled test distribution? Minor comments, no need to respond: a) There are several minor typos in the paper, e.g.: p1, *prediciton*; eqn 8, v; p8, *address*; p9, *orcale*; appendix A, missing ref. b) The discussion in Appendix A seems a bit speculative and opinionated. For example, it is not obvious that one has to fall back on the space of all possible models in order to represent test-time training with a single gradient step as a fixed model. The discussion is useful but in my subjective opinion could be toned down; the experiments and discussion in the main paper are strong and less speculative. === Edit: Thank you for the response. The discussion about hyper-parameters makes sense. My rating edit comes from the realization that the performance improvements obtained are almost entirely from the *online* version, which gets to see the test distribution. So I think the baselines are in lacking in that sense: as a straw man baseline for example, one could simply run the normal model on half of the test set, and then use those observed test examples to do some other sort of domain adaptation training.