ViCLSR (Vietnamese Contrastive Learning for Sentence Representations) is a supervised contrastive learning framework for Vietnamese Natural Language Understanding (NLU). The model leverages Natural Language Inference (NLI) datasets to learn high-quality sentence embeddings using entailment and contradiction relationships, targeting low-resource Vietnamese NLU settings.
High-quality text representations are crucial for NLU, but low-resource languages like Vietnamese face challenges due to limited annotated data. We propose ViCLSR, a novel supervised contrastive learning framework that optimizes sentence embeddings for Vietnamese by leveraging existing NLI datasets. ViCLSR significantly outperforms strong baselines on five Vietnamese NLU benchmarks, demonstrating that supervised contrastive learning can effectively address resource limitations in low-resource NLU tasks.
Performance
ViCLSR is evaluated on five Vietnamese NLU benchmarks spanning NLI, Fact Checking, Constructive Speech Detection, and Reading Comprehension (Table 4 in the paper).
ViCLSR Results
Dataset
Task
Metric
ViCLSR
vs. XLM-R Large
vs. PhoBERT Large
ViNLI
Natural Language Inference
F1
82.84
↑1.53
↑6.97
ViWikiFC
Fact Checking
F1
86.57
↑1.42
↑4.97
ViFactCheck
Fact Checking
F1
88.78
↑0.76
↑9.02
UIT-ViCTSD
Constructive Speech Detection
F1
82.22
↑2.78
↑5.36
ViMMRC2.0
Reading Comprehension
Acc
59.06
↑1.54
↑4.33
Full results and analysis are available in the paper.
Intended Uses
ViCLSR is designed for Vietnamese NLU research and can be applied to:
✅ Sentence embedding
✅ Semantic similarity
✅ Natural Language Inference (NLI)
✅ Information retrieval
✅ Fact checking
✅ Sentiment analysis
✅ Vietnamese NLU tasks in general
Out-of-Scope Uses
❌ Non-Vietnamese languages (model is optimized for Vietnamese)
❌ Commercial use (CC BY-NC-SA 4.0 license)
Architecture Note
ViCLSR extends XLM-RoBERTa-Large with a custom MLP projection head (mlp.dense, Linear 1024→1024) trained with supervised contrastive loss. This projection head is essential for obtaining high-quality embeddings — using the raw CLS token without it will yield suboptimal results. The examples below demonstrate the correct loading procedure.
Usage
Installation
pip install transformers torch huggingface_hub
Model Loading Helper
Both usage examples share the same model loading procedure. We recommend defining a helper function:
python
1import os
2os.environ["DISABLE_SAFETENSORS_CONVERSION"]="1"3import transformers
4from transformers import AutoTokenizer, XLMRobertaModel
5import torch
6import torch.nn as nn
7import torch.nn.functional as F
8from huggingface_hub import hf_hub_download
91011defload_viclsr(model_name="huynhtin/ViCLSR"):12 transformers.logging.set_verbosity_error()# suppress load warnings1314 tokenizer = AutoTokenizer.from_pretrained(model_name)15 model = XLMRobertaModel.from_pretrained(16 model_name,17 use_safetensors=False# use pytorch_model.bin directly18)1920# Load custom MLP projection head trained with contrastive loss21 model.mlp = nn.Linear(1024,1024)22 ckpt_path = hf_hub_download(repo_id=model_name, filename="pytorch_model.bin")23 state_dict = torch.load(ckpt_path, map_location="cpu")24 model.mlp.weight = nn.Parameter(state_dict["mlp.dense.weight"])25 model.mlp.bias = nn.Parameter(state_dict["mlp.dense.bias"])26 model.eval()27return tokenizer, model
Sentence Embedding
python
1tokenizer, model = load_viclsr()23text ="Trí tuệ nhân tạo đang phát triển rất nhanh."4inputs = tokenizer(text, return_tensors="pt")56with torch.no_grad():7 outputs = model(**inputs)8 cls_emb = outputs.last_hidden_state[:,0]9 embedding = model.mlp(cls_emb)# pass through MLP head10 embedding = F.normalize(embedding, dim=-1)# L2 normalize1112print(embedding.shape)# torch.Size([1, 1024])
Semantic Similarity
python
1tokenizer, model = load_viclsr()23defget_embedding(text):4 inputs = tokenizer(5 text,6 return_tensors="pt",7 padding=True,8 truncation=True,9 max_length=25610)11with torch.no_grad():12 outputs = model(**inputs)13 cls_emb = outputs.last_hidden_state[:,0]14return F.normalize(model.mlp(cls_emb), dim=-1)1516sentence1 ="Hà Nội là thủ đô của Việt Nam."17sentence2 ="Thành phố Hà Nội là thủ đô nước Việt Nam."18sentence3 ="Bóng đá là môn thể thao phổ biến nhất thế giới."1920emb1 = get_embedding(sentence1)21emb2 = get_embedding(sentence2)22emb3 = get_embedding(sentence3)2324sim_12 =(emb1 * emb2).sum().item()25sim_13 =(emb1 * emb3).sum().item()2627print(f"Similarity (sentence1 vs sentence2): {sim_12:.4f}")# ~0.9828print(f"Similarity (sentence1 vs sentence3): {sim_13:.4f}")# ~0.48
Training Details
Base model: XLM-RoBERTa-Large
Training framework: Supervised Contrastive Learning
Training data: Vietnamese NLI datasets (entailment/contradiction pairs)
Objective: Contrastive loss using positive (entailment) and negative (contradiction) pairs
Projection head: MLP Linear(1024 → 1024) trained jointly with contrastive loss
Language: Vietnamese
Limitations
Optimized specifically for Vietnamese — performance may degrade significantly on other languages
Performance depends on the quality and domain of input text
Best suited for research purposes under CC BY-NC-SA 4.0
Citation
If you use ViCLSR in your research, please cite:
bibtex
1@article{huynh2026viclsr,
2 title={ViCLSR: A Supervised Contrastive Learning Framework with Natural Language Inference for Natural Language Understanding Tasks},
3 author={Huynh, Tin Van and Nguyen, Kiet Van and Nguyen, Ngan Luu-Thuy},
4 journal={International Journal of Machine Learning and Cybernetics},
5 volume={17},
6 number={8},
7 pages={387},
8 year={2026},
9 publisher={Springer}
10}