Views
No views yet
pritamdeka/S-Scibert-snli-multinli-stsb, maintained for teaching a course on transformer-based topic modeling in R.pytorch_model.bin (PyTorch pickle format) and no tokenizer.json. For Python users this works fine, but for R users working through the torch (libtorch) and safetensors R packages there is a more serious problem than just format inconvenience:aten::empty_strided ... CUDA backend error. Python's torch.load(map_location='cpu') rescues you from this, but R-torch's loader doesn't expose that argument, so the upstream file is effectively unusable from R unless you have a CUDA GPU available.model.safetensors — the same weights in safetensors format. Safetensors files do not record device information at all, so they load cleanly regardless of where the model was originally saved or what hardware the user has.| Property | Value |
|---|---|
| Architecture | BERT-base + mean-pooling head |
| Parameters | ~110M |
| Embedding dimension | 768 |
| Layers | 12 |
| Attention heads | 12 |
| Vocabulary | SciBERT scientific (cased, ~31K tokens) |
| Pooling | Mean over tokens (masked by attention) |
| Fine-tuning data | SNLI + MultiNLI + STS-B |
| Training max_seq_length | 75 tokens |
| Case sensitivity | Cased |
all-MiniLM-L6-v2 or all-mpnet-base-v2 will likely match or beat S-SciBERT on non-scientific content.torch (libtorch) R package, with no Python at runtime:1source("bert_r.R")
2enc <- load_hf_bert("NetworkIsLife/S-SciBert_DAFS")
3
4emb <- embed_texts(enc$model, enc$tokenizer,
5 c("CRISPR-Cas9 enables targeted gene editing.",
6 "Glioblastoma exhibits invasive growth patterns.",
7 "Gradient descent minimizes a loss function."),
8 max_length = 128)
9dim(emb) # 3 x 768
10
11# Cosine similarity (embeddings are L2-normalized by default)
12sims <- emb %*% t(emb)
13round(sims, 3)
14# Rows 1 and 2 should be more similar to each other (both biomedical)
15# than either is to row 3 (machine learning)1source("bertopic_r.R")
2fit <- fit_bertopic(enc, docs = my_abstracts,
3 umap_n_neighbors = 15,
4 hdbscan_min_pts = 10)
5print_topics(fit)1enc <- load_hf_bert(
2 "NetworkIsLife/S-SciBert_DAFS",
3 weights_path = hfhub::hub_download(
4 "NetworkIsLife/S-SciBert_DAFS",
5 "model.safetensors",
6 revision = "MAIN_COMMIT_HASH_HERE"
7 )
8)MAIN_COMMIT_HASH_HERE with the commit hash visible in this repo's commit history.1# Via sentence-transformers (easiest)
2from sentence_transformers import SentenceTransformer
3model = SentenceTransformer("NetworkIsLife/S-SciBert_DAFS")
4embeddings = model.encode([
5 "CRISPR-Cas9 enables targeted gene editing.",
6 "Glioblastoma exhibits invasive growth patterns."
7])
8
9# Via transformers (with manual mean pooling)
10from transformers import AutoTokenizer, AutoModel
11import torch
12import torch.nn.functional as F
13
14tok = AutoTokenizer.from_pretrained("NetworkIsLife/S-SciBert_DAFS")
15mod = AutoModel.from_pretrained("NetworkIsLife/S-SciBert_DAFS").eval()
16
17enc = tok(sentences, padding=True, truncation=True, return_tensors="pt", max_length=128)
18with torch.no_grad():
19 out = mod(**enc).last_hidden_state
20 m = enc["attention_mask"].unsqueeze(-1).float()
21 pooled = (out * m).sum(1) / m.sum(1).clamp(min=1e-9)
22 embeddings = F.normalize(pooled, p=2, dim=1)| File | Source | Purpose |
|---|---|---|
model.safetensors | converted from upstream pytorch_model.bin | model weights, modern format (device-agnostic) |
pytorch_model.bin | copied from upstream | model weights, legacy format (kept for compatibility) |
config.json | copied from upstream | BERT architecture parameters |
vocab.txt | copied from upstream | SciBERT WordPiece vocabulary |
tokenizer_config.json | copied from upstream (if present) | tokenizer settings (do_lower_case, special tokens) |
README.md | this file | provenance and usage |
model.safetensors file in this repo was produced by HuggingFace's official SFconvertbot (the same automated conversion used across thousands of HuggingFace repos). The conversion is purely a re-serialization — every tensor in the safetensors file is bit-identical to the corresponding tensor in pytorch_model.bin. No re-training, no quantization, no precision loss.1import torch
2from safetensors.torch import load_file
3
4# map_location='cpu' is needed because the upstream pickle was saved on GPU
5a = torch.load("pytorch_model.bin", map_location="cpu", weights_only=True)
6b = load_file("model.safetensors")
7assert set(a.keys()) == set(b.keys())
8for k in a:
9 assert torch.equal(a[k].cpu(), b[k]), f"Mismatch in {k}"
10print("Bit-identical.")1from sentence_transformers import SentenceTransformer
2import numpy as np
3
4sentences = [
5 "CRISPR-Cas9 enables targeted gene editing.",
6 "Glioblastoma exhibits invasive growth.",
7 "Gradient descent minimizes a loss function."
8]
9a = SentenceTransformer("pritamdeka/S-Scibert-snli-multinli-stsb").encode(sentences)
10b = SentenceTransformer("NetworkIsLife/S-SciBert_DAFS").encode(sentences)
11print("max |Δ| =", np.abs(a - b).max()) # should be ~1e-6 or smaller1@inproceedings{deka2021unsupervised,
2 title={Unsupervised Keyword Combination Query Generation from
3 Online Health Related Content for Evidence-Based Fact Checking},
4 author={Deka, Pritam and Jurek-Loughrey, Anna},
5 booktitle={The 23rd International Conference on Information Integration
6 and Web-based Applications & Services},
7 pages={267--277},
8 year={2021}
9}1@inproceedings{beltagy-etal-2019-scibert,
2 title = "{SciBERT}: A Pretrained Language Model for Scientific Text",
3 author = "Beltagy, Iz and Lo, Kyle and Cohan, Arman",
4 booktitle = "Proceedings of EMNLP-IJCNLP",
5 year = "2019",
6 url = "https://www.aclweb.org/anthology/D19-1371"
7}pritamdeka/S-Scibert-snli-multinli-stsb by Pritam Deka.
Base model: allenai/scibert_scivocab_cased by the Allen Institute for AI.