Registry / ai-ml / pytorch-pretrained-bert

pytorch-pretrained-bert

JSON →
library0.6.2pypypi✓ verified 86d ago

PyTorch version of Google AI's BERT model with a script to load Google pre-trained models. This library (version 0.6.2) was the predecessor to the 'transformers' library by Hugging Face, which now includes BERT and many other models. It is deprecated and no longer maintained; all users should migrate to the 'transformers' package for active development, bug fixes, and better performance.

pip install pytorch-pretrained-bert==0.6.2
INSTALL
IMPORT
SIG · PYTORCH-PRETRAINED
P
pytorch-pretrained-bert
ai-mlpythonv0.6.2
Install
41.6s avg
Import
7863ms
Disk
1515MB
Pass rate
4/ 10
Env Coverage4 / 10
glibc
3.93.13
musl
3.93.13
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
musl
glibc
py 3.10
1/2 runs
✓ 47.6s
py 3.11
1/2 runs
✓ 44s
py 3.12
1/2 runs
✓ 38.1s
py 3.13
1/2 runs
✓ 36.8s
py 3.9
1/2 runs
1/2 runs
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)
Debug
Known issues
deprecatedpytorch-pretrained-bert is deprecated and no longer maintained. All models have been merged into the 'transformers' library. Use 'transformers' for latest features and security fixes.
fix
Run: pip install transformers. Then replace imports: from transformers import BertTokenizer, BertModel.
affects: all
breakingThe API for model output changed. In pytorch-pretrained-bert, model() returns a tuple. In 'transformers', it returns a ModelOutput object. Access last hidden state via outputs.last_hidden_state.
fix
Use outputs.last_hidden_state or outputs[0] consistently across both libs.
affects: >=0.6.0
gotchaTokenizer.from_pretrained() downloads files from S3. If you have network issues, it fails silently. Pre-download or use environment variable BERT_CACHE_DIR.
fix
Set BERT_CACHE_DIR to a local path, e.g., import os; os.environ['BERT_CACHE_DIR'] = './cache'.
affects: all
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`.
fix
Use correct import: `from pytorch_pretrained_bert import BertModel`.
ModuleNotFoundError: No module named 'pytorch_pretrained_bert'
The library is not installed or pip install failed.
fix
Run `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.
fix
Reinstall: `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.
Agent activity
28 hits · last 30 days
node
24
OpenAI (training)
1
Resources
pytorch-pretrained-bert — pip install pytorch-pretrained-bert · libregistry