Transfer Learning

Lesson 7 of 7410 minInterview Foundations and Practical ML Techniques
In this lesson8 sections

Transfer Learning

Transfer learning starts with a pretrained model and adapts what it has learned to another task. Compare two choices: keep the pretrained layers fixed and use their features, or update some of their weights through fine-tuning. The available labeled data and the similarity between tasks help guide that choice.

What is transfer learning?

Transfer learning is the task of using a pre-trained model and applying it to a new task, i.e., transferring the knowledge learned from one task to another. This is useful because the model doesn’t have to learn from scratch and can achieve higher accuracy in less time as compared to models that don’t use transfer learning.

Transfer learning
Transfer learning

Why transfer learning matters

The examples below show three reasons to consider transfer learning: access to shared models, sub-problems that recur across tasks, and limited labeled data or training resources.

Motivation for transfer learning
Motivation for transfer learning
  1. Shared models and datasets: Universities and technology companies release pretrained models and datasets that other teams can use as a starting point. Reusing an existing model lets a team spend its effort on adapting and evaluating it for the new task.

  2. Common sub-problems: Another key motivator is that many problems share common sub-problems, e.g., in all visual understanding and prediction areas, tasks such as finding edges, boundaries, and background are common sub-problems. Similarly, in the text domain, the semantic understanding of textual terms can be helpful in almost all problems where text terms, including search, recommendation systems, ads, etc., represent the user.

  3. Limited supervised learning data and training resources: Many real-world applications are still mapped onto supervised learning problems where the model is asked to predict a label. One key problem is the limited amount of training data available for models to generalize well. One key advantage of doing transfer learning is that we have the ability to start learning from pre-trained models, and hence, we can utilize the knowledge from similar domains.

    Self-supervised learning models are able to utilize massive available datasets for text and image representation, e.g., Word2vec embedding models don’t need any manual labels and can use the books and Wikipedia data to build a semantic understanding of terms effectively. Once we train a model for a certain representation, it can be utilized and help in many other supervised learning tasks.

    Transfer learning also optimizes training resources, and it helps teams that don’t have massive computing resources available. For instance, Google can train a BERT model on billions of examples with its massive computing power, but others are going to find it challenging to train similar optimized models. With transfer learning, we don’t have to reinvest those resources and can just plug in the output of the BERT model or use it as a sub-model in our training process. The earlier embeddings lesson explains how these representations become inputs to another model.

Techniques for transfer learning utilization

The transfer learning technique can be utilized in the following ways:

Extract features from useful layers

Keep the useful pretrained layers frozen and replace the original classification head with a head for the new task. Train the new head while the retained base supplies fixed features. In fine-tuning, by contrast, you also update selected pretrained weights. Primary reference: TensorFlow transfer learning.

Fine-tuning

Fine-tuning updates existing weights for the new supervised task. Decide which layers to keep frozen and which to train by considering what each part of the network represents. In the image example, that means distinguishing the convolution, pooling, and fully connected layers before choosing how much of the pretrained network to update.

Transfer learning technique can be utilized in one or both of the above ways depending on the following two factors:

  1. Size of our supervised training dataset

    How much labeled data do we possess to optimize the model? Do we have 100k examples, 1 million examples, 10 million examples? This is an important question for deciding on the approach that we want to use in utilizing the pre-trained model.

    Training data is limited: In case of a limited amount of specialized training data, we can either go with the approach of freezing all the layers and using the pre-trained model for feature generation or fine-tuning only the final layers.

    Training data is plenty: If we have a significant amount of training data (e.g. one million+ examples), we have the choice to play around with multiple ideas. We can start with just freezing the model, fine-tuning only final layers, or we can retrain the whole model to adjust weights for our specialized task.

  2. Similarity of prediction tasks

    The similarity of learning tasks can also guide us on whether we can simply use the model as it is or need to fine-tune the model for our new prediction task. For example, if we built a classifier for cars and now we want to use it for trucks, there is a good chance that many of the features are going to be common and we don’t have to fine-tune much. Here, we can utilize the pre-trained model as it is and build our models on top of it (i.e., utilizing the output of pre-trained models as features).

Applications in Machine Learning Systems

In real-world ML systems, transfer learning is commonly used to accelerate development, reduce training costs, and improve performance when labeled data is scarce. It enables teams to reuse proven models and adapt them quickly to domain-specific problems at scale.

Computer vision problems

Consider a hypothetical classifier for medical images with 100k manually labeled training examples. One candidate starting point is a pretrained ImageNet classifier. The cases below compare how much of that model to update; the example’s data count is an assumption, not a threshold that determines the answer.

VGG16 architecture
VGG16 architecture

The convolutional filters in a trained convolutional neural network (CNN) are arranged in a kind of hierarchy. The filters in the first layer often detect edges or blocks of color. The second layer’s filters can detect features like shapes. All of them are very general features that are useful in analyzing any image in any dataset. The filters in the last layers are more specific. Let’s go over all of the freezing layers, fine-tuning a few layers, and fine-tuning the whole model scenarios:

  • Case 1: Fine-tuning a few layers

    If our prediction task is similar, there will be similar higher-level features or layers output. Therefore most or all of the pre-trained model layers already have relevant information about the new data set and should be kept. We will freeze the weight of most of the starting layers of the pre-trained model and fine-tune only the end layers.

    This approach will also be most viable if our labeled training data is limited as it would be hard to re-tune all layers based on that limited data set.

Fine-tuning a few layers
Fine-tuning a few layers
  • Case 2: Fine tuning more layers

    If we have significant amount of labelled examples and our learning tasks have commonalities but few differences as well, it would make sense to go deeper in fine tuning our pre-trained model. We will freeze the weights of the first few layers and fine-tune the weights of the remaining end layers to optimize the model for our new learning task on medical image dataset.

Fine-tuning most of the end layers
Fine-tuning most of the end layers
  • Case 3: Fine tuning the entire model

    With a larger new dataset, you can consider loading the pretrained weights and fine-tuning the entire network. This exposes more parameters to updates and increases training work. Compare the result with the more limited fine-tuning choices rather than assuming that updating every layer will be best.

Fine tuning the entire model
Fine tuning the entire model

The image-segmentation modeling lesson applies these transfer-learning choices to image data.

Natural language processing(NLP)

In many of NLP learning tasks such as language understanding, speech recognition, entity recognition, language generation, semantic understanding, etc. as well as other problems that are based on search, one major need is to represent our text terms in a way that they capture the semantic meaning. For this, we need to generate the dense representation of textual terms. A few of the popular term representation models that use a self-supervised learning approach, trained on massive datasets, are Word2Vec, BERT, and ELMO. The term representation based on these models captures their semantic meanings. Hence, we can transfer knowledge from this learned task to many of the NLP tasks. Through the transfer learning approach, we can now utilize these embeddings in an NER classifier, a spam detector classifier, search ranking, language understanding, etc., and can significantly improve the quality of these ML models.

BERT/ELMo can be as an embedding generator
BERT/ELMo can be as an embedding generator

For entity linking, apply the same transfer-learning choices: use the pretrained representation as an input feature or adapt it while training the downstream model.