Views
No views yet
vinai/phobert-basequestion)answer)context)[CLS] embedding is projected through a linear layer and L2-normalized.
Contrastive learning ensures semantically related pairs are close in embedding space.1graph TD
2A[Question + Context] -->|Tokenizer| B[PhoBERT Encoder]
3C[Answer + Context] -->|Tokenizer| B2[PhoBERT Encoder]
4B --> D[CLS Embedding]
5B2 --> D2[CLS Embedding]
6D --> E[Linear Projection]
7D2 --> E2[Linear Projection]
8E --> F[L2 Normalize]
9E2 --> F2[L2 Normalize]
10F --> G[Contrastive Loss]
11F2 --> G| Field | Description |
|---|---|
question | Medical question |
answer | Correct answer |
context | Related context such as symptoms or drug effects |
| Metric | Validation Value |
|---|---|
| Average Cosine Similarity | ~0.97 |
import torch
from transformers import AutoTokenizer
from model import ViMedEmbeddingModel
import torch.nn.functional as F
device = "cuda" if torch.cuda.is_available() else "cpu"
model = ViMedEmbeddingModel()
model.load_state_dict(torch.load("checkpoints_with_anchor/best_model.pt", map_location=device))
model.to(device).eval()
tokenizer = AutoTokenizer.from_pretrained("vinai/phobert-base", use_fast=True)
def get_embedding(text):
inputs = tokenizer(text, padding="max_length", truncation=True, max_length=256, return_tensors="pt").to(device)
with torch.no_grad():
emb = model(inputs["input_ids"], inputs["attention_mask"])
return emb
question = "Thuốc Buscopan có thể gây ra tác dụng phụ nào liên quan đến huyết áp?"
answer = "Thuốc Buscopan có thể gây hạ huyết áp và chóng mặt."
emb_q = get_embedding(question)
emb_a = get_embedding(answer)
similarity = F.cosine_similarity(emb_q, emb_a).item()
print(f"🔹 Cosine Similarity: {similarity:.4f}")
@misc
{ViMedEmbedding2025,
title={ViMedEmbedding: Vietnamese Medical Sentence Embedding },
author={Mouth Ji},
year={2025},
publisher={Hugging Face},
howpublished={[https://huggingface.co/MouhJI/vi-heath-embedding/edit/main/README.md](https://huggingface.co/MouhJI/vi-heath-embedding)}
}