Pre-Training LLMs: Techniques and Objectives

Anay Dongre
4 min readJul 31, 2023


Image from author

Welcome to the second article in our comprehensive series on Pre-Training Large Language Models (LLMs). In this installment, we will delve deep into the technical intricacies of pre-training LLMs, focusing on two pivotal techniques: Masked Language Modeling (MLM) and Contrastive Learning with negative sampling. Additionally, we will explore the significance of selecting appropriate pre-training objectives tailored to specific downstream tasks. Get ready for a technical journey into the cutting-edge advancements in the world of Natural Language Processing (NLP)!

If you haven’t read the first article in the series, make sure to get a hold of it… Large Language Models: What, How, and Why?

1. Masked Language Modeling (MLM)
2. Contrastive Learning and the Role of Negative Sampling
3. Selecting Appropriate Pre-Training Objectives

1. Masked Language Modeling (MLM) — Unraveling the Technique

Masked Language Modeling (MLM) has emerged as a cornerstone technique in the pre-training of LLMs. The MLM objective involves randomly masking certain tokens within a given input sequence, and the model is tasked to predict the original tokens based on the context provided by the non-masked tokens. This unsupervised pre-training approach fosters a profound understanding of language semantics within the model.

During MLM pre-training, a fraction of the tokens in a sentence are randomly masked, and the model is required to predict the masked tokens. This process is facilitated by providing a binary mask input to the model, indicating the positions of the masked tokens. The model then generates token embeddings for all positions, and the masked token predictions are compared against the ground truth using an appropriate loss function, such as cross-entropy loss. The model’s parameters are optimized through backpropagation to improve the masked token predictions.

One of the primary advantages of MLM is its bidirectionality, where the model learns to generate contextual representations based on both left and right contexts. This bidirectional context modeling enables the model to grasp intricate semantic relationships within the language, leading to significant improvements in various NLP tasks.

2. Contrastive Learning and the Role of Negative Sampling

Contrastive Learning is another powerful pre-training objective that has gained prominence, especially with models like SimCLR. The fundamental principle of contrastive learning revolves around maximizing agreement between similar pairs of data instances while minimizing agreement between dissimilar pairs.

The crux of contrastive pre-training lies in establishing a notion of similarity between data samples. During the pre-training process, augmented versions of the same input data are considered positive pairs, while data instances that are unrelated or dissimilar are treated as negative pairs. These negative samples play a crucial role in prompting the model to effectively distinguish between similar and dissimilar instances.

The contrastive loss function is pivotal in driving the model to assign higher similarity scores to positive pairs and lower similarity scores to negative pairs. Common metrics, such as cosine similarity, are often employed to measure the similarity between embeddings. The optimization process during pre-training aims to minimize the contrastive loss, effectively pulling similar instances closer together and pushing dissimilar instances apart in the embedding space.

Contrastive Learning has shown promise in capturing intricate relationships within the data, resulting in enhanced generalization capabilities of the pre-trained model.

3. Selecting Appropriate Pre-Training Objectives

The selection of pre-training objectives is a critical aspect that heavily influences the performance of LLMs on downstream tasks. While MLM and contrastive learning are two powerful techniques, the choice of objectives should be tailored to the specific tasks the LLM is intended to serve. Each objective imparts certain linguistic and semantic characteristics to the pre-trained model, making it more adept at handling specific types of tasks.

For instance, tasks that involve generating sequential outputs, such as machine translation, may benefit from integrating sequence-to-sequence (Seq2Seq) objectives into the pre-training process. Additionally, objectives like next-sentence prediction can help the model capture sentence-level relationships, making it more versatile for question-answering and document classification tasks.

The selection of appropriate pre-training objectives is an active area of research, and researchers continuously explore novel objectives and combinations to unlock the full potential of LLMs.


In this article, we have explored the technical intricacies of two fundamental pre-training techniques for LLMs: Masked Language Modeling (MLM) and Contrastive Learning with negative sampling. MLM’s bidirectionality fosters a deep understanding of language semantics, while contrastive learning captures intricate relationships within the data, leading to improved generalization capabilities.

Moreover, we have emphasized the significance of selecting appropriate pre-training objectives tailored to specific downstream tasks. Each objective imbues the pre-trained model with unique linguistic characteristics, enhancing its performance on diverse NLP applications.

As NLP research continues to advance, the development and optimization of pre-training objectives will remain crucial in propelling the progress of Large Language Models.


  1. Devlin, J., Chang, M. W., Lee, K., & Toutanova, K. (2019). BERT: Bidirectional Encoder Representations from Transformers. arXiv preprint arXiv:1810.04805.
  2. Chen, T., Kornblith, S., Norouzi, M., & Hinton, G. (2020). A Simple Framework for Contrastive Learning of Visual Representations. arXiv preprint arXiv:2002.05709.
  3. Vaswani, Ashish, et al. “Attention is all you need.” Advances in neural information processing systems 30 (2017).
  4. Richard Socher, Alex Perelygin, Jean Wu, Jason Chuang, Christopher D. Manning, Andrew Ng, and Christopher Potts. 2013. Recursive Deep Models for Semantic Compositionality Over a Sentiment Treebank. In Proceedings of the 2013 Conference on Empirical Methods in Natural Language Processing, pages 1631–1642, Seattle, Washington, USA. Association for Computational Linguistics.