Views
No views yet
google-bert/bert-base-uncased. It has been fine-tuned with a masked language modeling (MLM) objective on all historical English newspaper text (1800-1920) from the following two collections:BertForMaskedLMgoogle-bert/bert-base-uncased1from transformers import AutoTokenizer, AutoModelForMaskedLM, pipeline
2
3model_id = "TextMachineProject/NewsBERT_1800-1920"
4
5tokenizer = AutoTokenizer.from_pretrained(model_id)
6model = AutoModelForMaskedLM.from_pretrained(model_id)
7
8fill_mask = pipeline("fill-mask", model=model, tokenizer=tokenizer)
9
10text = "The [MASK] was published in the newspaper."
11preds = fill_mask(text)
12
13for p in preds:
14 print(f"{p['sequence']} (score={p['score']:.4f})")
151import torch
2from transformers import AutoTokenizer, AutoModel
3
4model_id = "TextMachineProject/NewsBERT_1800-1920"
5device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
6
7tokenizer = AutoTokenizer.from_pretrained(model_id)
8model = AutoModel.from_pretrained(model_id).to(device)
9model.eval()
10
11def encode(text, max_length=512):
12 inputs = tokenizer(
13 text,
14 return_tensors="pt",
15 truncation=True,
16 max_length=max_length
17 ).to(device)
18
19 with torch.no_grad():
20 outputs = model(**inputs)
21 embedding = outputs.last_hidden_state[:, 0, :] # CLS token
22
23 return embedding.squeeze(0).cpu() # [768]
24
25embedding = encode("Example newspaper article text...")
26print(embedding.shape) # torch.Size([768])1import torch.nn.functional as F
2
3e1 = encode("Article text one...")
4e2 = encode("Another article...")
5
6cos_sim = F.cosine_similarity(e1, e2, dim=0)
7print("Cosine similarity:", cos_sim.item())