Add comprehensive model card for encoder model

#1
by nielsr HF Staff - opened
Files changed (1) hide show
  1. README.md +65 -0
README.md ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ pipeline_tag: feature-extraction
3
+ library_name: transformers
4
+ license: apache-2.0
5
+ ---
6
+
7
+ # Encoder Model from "Should We Still Pretrain Encoders with Masked Language Modeling?"
8
+
9
+ This repository contains an encoder model, part of the research presented in the paper **"[Should We Still Pretrain Encoders with Masked Language Modeling?](https://huggingface.co/papers/2507.00994)"**.
10
+
11
+ This paper investigates the effectiveness of Masked Language Modeling (MLM) versus Causal Language Modeling (CLM) for pretraining text encoders to achieve high-quality text representations. It demonstrates that while MLM generally yields better performance, CLM-trained models are more data-efficient. The research further proposes a biphasic training strategy that sequentially applies CLM and then MLM, achieving optimal performance under a fixed computational budget.
12
+
13
+ * **Paper:** [Should We Still Pretrain Encoders with Masked Language Modeling?](https://huggingface.co/papers/2507.00994)
14
+ * **Project Page:** [https://hf.co/MLMvsCLM](https://hf.co/MLMvsCLM)
15
+ * **Code:** [https://github.com/Nicolas-BZRD/EuroBERT](https://github.com/Nicolas-BZRD/EuroBERT)
16
+
17
+ ## Model Description
18
+
19
+ This model is an encoder designed to produce robust text representations for a wide range of natural language processing tasks. It is trained as part of an extensive study on encoder pretraining objectives, focusing on the trade-offs and benefits of MLM and CLM, and the effectiveness of a biphasic training approach. The model architecture is an `SLModel` as identified in the `config.json`.
20
+
21
+ ## Usage
22
+
23
+ You can use this model for feature extraction with the Hugging Face `transformers` library. Since this model might use a custom architecture (`SLModel`), you may need to install the associated `EuroBERT` package and use `trust_remote_code=True` when loading the model.
24
+
25
+ First, install the `EuroBERT` package:
26
+ ```bash
27
+ pip install git+https://github.com/Nicolas-BZRD/EuroBERT.git
28
+ ```
29
+
30
+ Then, you can load and use the model as follows:
31
+
32
+ ```python
33
+ from transformers import AutoTokenizer, AutoModel
34
+ import torch
35
+
36
+ # Replace with the actual model ID if different, e.g., "AhmedAliHassan/MLMvsCLM-Biphasic-210M"
37
+ # This placeholder assumes the current repository is the model you want to load.
38
+ model_name = "<YOUR_MODEL_ID_HERE>"
39
+
40
+ # Load the tokenizer and model, ensuring trust_remote_code for custom architectures
41
+ tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
42
+ model = AutoModel.from_pretrained(model_name, trust_remote_code=True)
43
+
44
+ text = "This is an example sentence to extract features from."
45
+
46
+ inputs = tokenizer(text, return_tensors="pt")
47
+
48
+ with torch.no_grad():
49
+ outputs = model(**inputs)
50
+
51
+ # The last hidden state contains the token embeddings (features)
52
+ last_hidden_state = outputs.last_hidden_state
53
+ print(f"Shape of last hidden state: {last_hidden_state.shape}")
54
+
55
+ # For sentence-level embeddings, common approaches include:
56
+ # 1. Averaging the token embeddings (excluding special tokens)
57
+ # 2. Using the embedding of the [CLS] token (if applicable for the model's architecture)
58
+ # Example: Mean pooling (simple average over non-padding tokens)
59
+ attention_mask = inputs["attention_mask"]
60
+ input_mask_expanded = attention_mask.unsqueeze(-1).expand(last_hidden_state.size()).float()
61
+ sum_embeddings = torch.sum(last_hidden_state * input_mask_expanded, 1)
62
+ sum_mask = torch.clamp(input_mask_expanded.sum(1), min=1e-9)
63
+ mean_pooled_embedding = sum_embeddings / sum_mask
64
+ print(f"Shape of mean pooled embedding: {mean_pooled_embedding.shape}")
65
+ ```