CoolFace
Modelpublic

ducanhdinh/jepa_proof_barlow_twins_replace

sourceHugging Faceapache-2.0updated 3mo agoView on Hugging Face
0likes
Model Card

ducanhdinh/jepaproofbarlowtwinsreplace

BERT encoder pretrained from scratch với Barlow Twins + Lexical Substitution augmentation.

Augmentation strategy

Thay vì span masking, model này dùng lexical substitution để tạo view 2:

Mô tả
View 1Câu gốc (không thay đổi)
View 215–20% token ngẫu nhiên được thay bằng token ngữ nghĩa gần, dự đoán bởi BERT MLM (top-5, loại trừ token gốc)

Quá trình tạo view 2 được thực hiện offline 1 lần trước khi train.

Kiến trúc Barlow Twins

View 1 ──► Encoder (θ) ──► Projector (θ) ──► z1  ──┐
                                                     ├──► Cross-correlation C = Z1ᵀZ2 / N  ──► Loss
View 2 ──► Encoder (θ) ──► Projector (θ) ──► z2  ──┘

Loss = Σ(C_ii - 1)²  +  λ · Σ_{i≠j} C_ij²
         on-diagonal       off-diagonal (redundancy reduction)

Encoder và Projector dùng shared weights (không có target network).

Thông số huấn luyện

Tham sốGiá trị
Max sequence length256
Batch size256
Epochs10
Learning rate0.0001
Projector hidden dim2048
Projector out dim8192
Off-diagonal coeff (λ)0.005
Mask ratio (lexsubst)0.15–0.2
Top-k candidates5

Cách dùng — BERT encoder (feature extraction)

python
from transformers import BertModel, BertTokenizerFast
import torch

tokenizer = BertTokenizerFast.from_pretrained("ducanhdinh/jepa_proof_barlow_twins_replace")
bert      = BertModel.from_pretrained("ducanhdinh/jepa_proof_barlow_twins_replace/encoder")

encoded = tokenizer(
    ["Hello world!", "Barlow Twins with lexical substitution."],
    return_tensors="pt",
    padding=True,
    truncation=True,
)
with torch.no_grad():
    out     = bert(**encoded)
    cls_emb = out.last_hidden_state[:, 0, :]   # [CLS] token → (B, 768)

Cách dùng — Full model

python
import torch
from text_barlow_twins_replace import TextBarlowTwinsReplace, BarlowTwinsReplacePretrainConfig

cfg   = BarlowTwinsReplacePretrainConfig()
model = TextBarlowTwinsReplace(cfg)
state = torch.load(
    hf_hub_download("ducanhdinh/jepa_proof_barlow_twins_replace", "pytorch_model.bin"),
    map_location="cpu",
)
model.load_state_dict(state)
model.eval()