Introduction
In the modern digital landscape, software permeates nearly every aspect of our daily lives. As these systems grow in scale and complexity, so does the variety of security loopholes they contain. For security analysts, the sheer volume of code to review is overwhelming. In 2023 alone, the National Vulnerability Database (NVD) published over 28,900 new Common Vulnerabilities and Exposures (CVE) entries. Disturbingly, over 4,000 of those cases remained unclassified in terms of their specific type for extended periods.
Identifying that a piece of code is “vulnerable” is only step one. The more difficult—and crucial—step is classifying what kind of vulnerability it is. Is it a buffer overflow? A double free? An SQL injection? This classification is handled by the Common Weakness Enumeration (CWE) system. Accurate classification allows developers to prioritize fixes based on severity and apply the correct remediation strategies.
Manual classification is slow and relies heavily on expert knowledge. While Deep Learning (DL) has revolutionized image and text processing, applying it to vulnerability classification faces a unique hurdle: the data is terribly imbalanced.
The researchers behind the paper “Applying Contrastive Learning to Code Vulnerability Type Classification” have proposed a novel framework that tackles this imbalance head-on. By leveraging the natural hierarchical structure of vulnerability categories and utilizing a technique called Contrastive Learning, they have developed a model that not only detects vulnerabilities but understands the nuanced relationships between them.
In this post, we will deconstruct their methodology, explain how they overcame the limitations of previous models, and analyze why their approach sets a new standard for automated security analysis.
The Problem: Long Tails and Isolated Classes
To understand the innovation of this paper, we first need to understand the data problem. In the world of software vulnerabilities, not all bugs are created equal. Some, like Cross-Site Scripting (XSS) or Out-of-bounds Write, appear frequently. Others appear very rarely.
When you plot the frequency of these vulnerability types, you get what is known as a long-tailed distribution.

As shown in Figure 1, the “Head” of the distribution contains a small number of very common vulnerability types (CWEs). The “Tail” stretches out with a vast number of types that have very few samples.
Why does this break Machine Learning?
Standard deep learning models are data-hungry. They excel at learning the classes in the “Head” because they see thousands of examples. However, they struggle to learn the classes in the “Tail.” In a standard classification setup, a model might ignore the rare classes entirely to maximize its overall accuracy on the common ones.
Furthermore, previous approaches treated every CWE class as an isolated island. They would treat “CWE-415 (Double Free)” and “CWE-416 (Use After Free)” as totally distinct labels, ignoring the fact that they are both memory management errors. This results in a code representation (the vector space where the model “thinks”) that is not scalable or semantically rich.
The Core Method: Hierarchical Contrastive Learning
The researchers propose a framework that changes how the model learns. Instead of just asking, “Which label is this?”, the model asks, “How is this code similar to other code in the same category, and how does it fit into the broader hierarchy of vulnerabilities?”
This is achieved through Hierarchical Contrastive Learning.
1. Understanding the Hierarchy
The CWE standard is not a flat list; it is a tree. At the top, you have abstract categories (Pillars). As you move down, the categories become more specific (Classes, Bases, and Variants).

Figure 2 illustrates this refinement chain. Take CWE-415 (Double Free).
- Pillar (664): Improper Control of a Resource Through its Lifetime.
- Class (118/119): Improper Restriction of Operations within Memory Buffer.
- Variant (415): Double Free.
The researchers realized that if the model learns this hierarchy, it can make better predictions. Even if it hasn’t seen many examples of a specific rare “Variant,” it might have seen plenty of examples of its “Pillar” or “Class,” allowing it to make an educated inference.
2. The Architecture Overview
The proposed framework follows a specific pipeline designed to process source code and refine its understanding through layers of contrastive learning.

As visualized in Figure 3, the workflow proceeds as follows:
- Label Expanding: The single label of a code snippet (e.g., CWE-415) is expanded into a chain of 5 labels representing its full ancestry in the CWE tree.
- Tokenization & Max-Pooling: The code is converted into tokens (numbers) the model can read.
- Hierarchical Contrastive Learning: The core training phase where the model aligns code representations at every level of the hierarchy.
- Classifier: A final layer predicts the specific vulnerability type.
3. Handling Long Code with Max-Pooling
Transformer models (like BERT) usually have a strict input limit (e.g., 512 tokens). Vulnerability code is often much longer than this. Previous methods simply chopped off the end of the code (truncation), potentially losing the actual vulnerable line.
The authors solved this using Max-Pooling. They split the long code into multiple chunks (e.g., two chunks of 512). They run both chunks through the model and then apply a Max-Pooling operation. This effectively keeps the “strongest” signal from both chunks, allowing the model to “see” a longer context without crashing the memory limits.
4. The Mathematical Engine: Contrastive Loss
This is the heart of the paper. Standard classification uses Cross-Entropy Loss, which penalizes the model based on probability.

While useful, Cross-Entropy doesn’t explicitly force the model to group similar things together in vector space. To fix this, the authors introduce Contrastive Loss.
Self-Supervised Contrastive Loss
First, they use a self-supervised approach. They take a code sample, create an “augmented” version of it (perhaps by masking some words), and tell the model: “These two are the same. Pull them close together. Push everything else away.”

In this equation (Image 004), \(z_i\) and \(z_{j(i)}\) are the representations of the original and augmented code. The goal is to maximize their similarity while minimizing similarity to others.
Supervised Contrastive Loss
However, we know the labels! So we should use them. In Supervised Contrastive Learning, the model is told: “All samples with the label ‘Buffer Overflow’ should be clustered together.”

This equation (Image 006) ensures that all positive pairs (samples of the same class) are pulled together in the hypersphere of the vector space.
The Problem of Class Collapse
If you rely only on Supervised Contrastive Learning, you risk Class Collapse. This happens when the model makes every single “Buffer Overflow” vector identical. It loses the nuance between different instances of a buffer overflow.
To prevent this, the authors combine all three losses. They use the Supervised loss to cluster classes, the Self-Supervised loss to maintain unique geometric spread (robustness), and Cross-Entropy for the final classification accuracy.

The final loss function (Image 007) is a weighted sum of these three components, controlled by parameters \(\lambda\) and \(\mu\).
Experiments and Results
The researchers evaluated their framework against several state-of-the-art baselines, including CodeBERT, GraphCodeBERT, and specialized vulnerability models like VulExplainer. They used the Big-Vul dataset and a newer, higher-quality dataset called PrimeVul.
Performance Comparison
The results were decisive. The proposed Hierarchical Contrastive Learning (HCL) framework significantly outperformed existing methods.

As shown in Table 1 (Image 008), looking at the Weighted F1 score (a metric that balances precision and recall while accounting for class imbalance):
- On Big-Vul, the proposed method (using CodeBERT) achieved 65.34%, compared to just 43.07% for standard CodeBERT.
- On the high-quality PrimeVul dataset, it achieved 41.24%, beating the nearest competitor significantly.
The “Tier 1” through “Tier 5” columns show that the model performs well not just at the abstract level, but all the way down to the specific variant level.
Why does it work? (Ablation Study)
To prove that their specific design choices mattered, the authors performed an Ablation Study. They systematically removed parts of the model to see if performance dropped.

Table 2 (Image 009) reveals the contribution of each component:
- Row 1 vs. Row 2: Adding Hierarchical Contrastive Learning (HCL) alone boosted accuracy from 63.19% to 66.92%.
- Row 2 vs. Row 3: Adding the “USCL” (Unsupervised/Self-Supervised loss) to prevent class collapse further improved performance to 68.31%.
- Row 6 (Full Model): Combining HCL, USCL, and Max-Pooling (MP) yielded the best result of 69.06%.
This confirms that every part of the architecture—the hierarchy, the geometric spread, and the long-text handling—is essential.
Robustness and Comparison with LLMs
Finally, the researchers tested how sensitive the model was to the weighting of the loss functions (\(\lambda\) and \(\mu\)) and compared it against GPT-4 (zero-shot).

The charts in Figure 4 (Image 013, top) show that the model is relatively stable; accuracy remains high across a reasonable range of hyperparameter values.
More interestingly, Table 3 (Image 013, bottom) shows that general-purpose Large Language Models (LLMs) like GPT-4 struggle with this specific task. With zero-shot prompting, GPT-4 only achieved roughly 22-25% accuracy. Even with Chain-of-Thought prompting, it lagged far behind the specialized Contrastive Learning model. This highlights that while LLMs are powerful, specialized domain-specific architectures are still superior for complex classification tasks like vulnerability analysis.
Conclusion and Implications
The paper “Applying Contrastive Learning to Code Vulnerability Type Classification” presents a significant step forward in automated software security. By refusing to treat vulnerability types as isolated labels and instead embracing their natural hierarchy, the researchers created a model that “understands” the relationship between software weaknesses.
Key Takeaways:
- Hierarchy Matters: utilizing the CWE tree structure allows the model to learn shared features across related vulnerabilities, mitigating the long-tail problem.
- Contrastive Learning is Powerful: Explicitly pulling similar vectors together results in a more robust and meaningful code representation than simple classification.
- Details Count: Technical adjustments like Max-Pooling for long code and mixing Self-Supervised loss to prevent class collapse provided measurable gains in accuracy.
For students and researchers in this field, this work underscores the importance of representation learning. It’s not just about building a bigger transformer; it’s about designing the training process to reflect the semantic reality of the data. As software systems continue to grow, tools built on these principles will be essential for keeping them secure.
](https://deep-paper.org/en/paper/file-2755/images/cover.png)