Registry /
ai-ml / pytorch-pretrained-bert
Install & Compatibility
Where this runs
tested against v0.6.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
1515MB installed
● package 1515MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
BertModel
✓ from pytorch_pretrained_bert import BertModel
✗ from pytorch_pretrained_bert.model import BertModel
Correct import is from pytorch_pretrained_bert directly.
BertTokenizer
✓ from pytorch_pretrained_bert import BertTokenizer
✗ from pytorch_pretrained_bert.tokenization import BertTokenizer
Common mistake: trying to import from submodules.
Minimal example: tokenize input, load BERT, and get encoder output.
from pytorch_pretrained_bert import BertTokenizer, BertModel
import torch
# Load pre-trained model tokenizer (vocabulary)
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
# Encode text
text = "Who was Jim Henson?"
tokenized_text = tokenizer.tokenize(text)
indexed_tokens = tokenizer.convert_tokens_to_ids(tokenized_text)
# Convert to PyTorch tensors
tokens_tensor = torch.tensor([indexed_tokens])
# Load pre-trained model
model = BertModel.from_pretrained('bert-base-uncased')
model.eval()
# Predict hidden states features
with torch.no_grad():
outputs = model(tokens_tensor)
print(outputs[0].shape) # (batch_size, seq_len, hidden_size)
Errors
Common errors & fixes
AttributeError: 'BertModel' object has no attribute 'from_pretrained'
Importing the class directly from the submodule, e.g., `from pytorch_pretrained_bert.model import BertModel`.
fixUse correct import: `from pytorch_pretrained_bert import BertModel`.
ModuleNotFoundError: No module named 'pytorch_pretrained_bert'
The library is not installed or pip install failed.
fixRun `pip install pytorch-pretrained-bert==0.6.2`. If you are offline, download the wheel from PyPI.
ImportError: cannot import name 'BertTokenizer' from 'pytorch_pretrained_bert'
Corrupted installation or version mismatch.
fixReinstall: `pip uninstall pytorch-pretrained-bert && pip install pytorch-pretrained-bert==0.6.2`.
Upgrade
Version history
0.6.2latest on PyPI · released Apr 25, 2019
Audit
Dependencies
torchrequiredRequired for PyTorch models.
numpyrequiredUsed for numerical operations.