Fill-Mask
Transformers
Safetensors
bert
masked-language-modeling
historical-nlp
temporal-language-model
Instructions to use TextMachineProject/NewsBERT_1800-1920-Temporal with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use TextMachineProject/NewsBERT_1800-1920-Temporal with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("fill-mask", model="TextMachineProject/NewsBERT_1800-1920-Temporal")# Load model directly from transformers import AutoTokenizer, AutoModelForMaskedLM tokenizer = AutoTokenizer.from_pretrained("TextMachineProject/NewsBERT_1800-1920-Temporal") model = AutoModelForMaskedLM.from_pretrained("TextMachineProject/NewsBERT_1800-1920-Temporal", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Update continuous_time_embedding.py
Browse files- continuous_time_embedding.py +19 -8
continuous_time_embedding.py
CHANGED
|
@@ -2,10 +2,8 @@
|
|
| 2 |
Companion module for loading the continuous-time NewsBERT model.
|
| 3 |
|
| 4 |
This model is not a plain AutoModelForMaskedLM. It wraps a full
|
| 5 |
-
fine-tuned BERT with a continuous sinusoidal time embedding
|
| 6 |
-
input layer. You need this file
|
| 7 |
-
to load and query it; `AutoModelForMaskedLM.from_pretrained(...)` alone will
|
| 8 |
-
not work.
|
| 9 |
|
| 10 |
Usage:
|
| 11 |
from continuous_time_embedding import load_continuous_time_model
|
|
@@ -20,6 +18,8 @@ import torch.nn as nn
|
|
| 20 |
from transformers import AutoTokenizer, AutoModelForMaskedLM
|
| 21 |
from huggingface_hub import snapshot_download
|
| 22 |
|
|
|
|
|
|
|
| 23 |
MIN_YEAR = 1800.0
|
| 24 |
MAX_YEAR = 1920.0
|
| 25 |
MIN_PERIOD_YEARS = 5.0
|
|
@@ -30,8 +30,7 @@ class ContinuousTimeEmbedding(nn.Module):
|
|
| 30 |
"""Sinusoidal (Fourier) features over normalized year, projected to
|
| 31 |
hidden_size. Nearby years produce nearby embeddings by construction.
|
| 32 |
Frequency band: lowest = 1 cycle over the full 1800-1920 span, highest =
|
| 33 |
-
1 cycle per MIN_PERIOD_YEARS (5 years)
|
| 34 |
-
card for the reasoning."""
|
| 35 |
|
| 36 |
def __init__(self, hidden_size, n_freqs=N_TIME_FREQS, min_year=MIN_YEAR, max_year=MAX_YEAR,
|
| 37 |
min_period_years=MIN_PERIOD_YEARS):
|
|
@@ -103,13 +102,25 @@ class ContinuousTimeBertForMLM(nn.Module):
|
|
| 103 |
|
| 104 |
def load_continuous_time_model(repo_id_or_path, device=None, **kwargs):
|
| 105 |
"""Convenience loader. Works with either a local checkpoint directory or
|
| 106 |
-
a Hugging Face Hub repo id (downloads it locally first).
|
|
|
|
| 107 |
if os.path.isdir(repo_id_or_path):
|
| 108 |
local_dir = repo_id_or_path
|
| 109 |
else:
|
| 110 |
local_dir = snapshot_download(repo_id_or_path)
|
| 111 |
|
| 112 |
-
tokenizer =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
model = ContinuousTimeBertForMLM.load_pretrained(local_dir, **kwargs)
|
| 114 |
|
| 115 |
device = device or ("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
| 2 |
Companion module for loading the continuous-time NewsBERT model.
|
| 3 |
|
| 4 |
This model is not a plain AutoModelForMaskedLM. It wraps a full
|
| 5 |
+
fine-tuned BERT with a continuous sinusoidal (Fourier) time embedding
|
| 6 |
+
injected at the input layer. You need this file to load and query it.
|
|
|
|
|
|
|
| 7 |
|
| 8 |
Usage:
|
| 9 |
from continuous_time_embedding import load_continuous_time_model
|
|
|
|
| 18 |
from transformers import AutoTokenizer, AutoModelForMaskedLM
|
| 19 |
from huggingface_hub import snapshot_download
|
| 20 |
|
| 21 |
+
BASE_MODEL_ID = "TextMachineProject/NewsBERT_1800-1920"
|
| 22 |
+
|
| 23 |
MIN_YEAR = 1800.0
|
| 24 |
MAX_YEAR = 1920.0
|
| 25 |
MIN_PERIOD_YEARS = 5.0
|
|
|
|
| 30 |
"""Sinusoidal (Fourier) features over normalized year, projected to
|
| 31 |
hidden_size. Nearby years produce nearby embeddings by construction.
|
| 32 |
Frequency band: lowest = 1 cycle over the full 1800-1920 span, highest =
|
| 33 |
+
1 cycle per MIN_PERIOD_YEARS (5 years)."""
|
|
|
|
| 34 |
|
| 35 |
def __init__(self, hidden_size, n_freqs=N_TIME_FREQS, min_year=MIN_YEAR, max_year=MAX_YEAR,
|
| 36 |
min_period_years=MIN_PERIOD_YEARS):
|
|
|
|
| 102 |
|
| 103 |
def load_continuous_time_model(repo_id_or_path, device=None, **kwargs):
|
| 104 |
"""Convenience loader. Works with either a local checkpoint directory or
|
| 105 |
+
a Hugging Face Hub repo id (downloads it locally first).
|
| 106 |
+
"""
|
| 107 |
if os.path.isdir(repo_id_or_path):
|
| 108 |
local_dir = repo_id_or_path
|
| 109 |
else:
|
| 110 |
local_dir = snapshot_download(repo_id_or_path)
|
| 111 |
|
| 112 |
+
tokenizer = None
|
| 113 |
+
for candidate in (os.path.join(local_dir, "full_model"), local_dir):
|
| 114 |
+
try:
|
| 115 |
+
tokenizer = AutoTokenizer.from_pretrained(candidate)
|
| 116 |
+
break
|
| 117 |
+
except Exception:
|
| 118 |
+
continue
|
| 119 |
+
if tokenizer is None:
|
| 120 |
+
print(f"[load_continuous_time_model] No tokenizer found in {repo_id_or_path}, "
|
| 121 |
+
f"falling back to base model tokenizer: {BASE_MODEL_ID}")
|
| 122 |
+
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_ID)
|
| 123 |
+
|
| 124 |
model = ContinuousTimeBertForMLM.load_pretrained(local_dir, **kwargs)
|
| 125 |
|
| 126 |
device = device or ("cuda" if torch.cuda.is_available() else "cpu")
|