{"id":24472,"library":"pytorch-pretrained-bert","title":"PyTorch Pretrained BERT","description":"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.","status":"deprecated","version":"0.6.2","language":"python","source_language":"en","source_url":"https://github.com/huggingface/pytorch-pretrained-BERT","tags":["bert","nlp","pytorch","transformer","deprecated"],"install":[{"cmd":"pip install pytorch-pretrained-bert==0.6.2","lang":"bash","label":"Last stable version"},{"cmd":"pip install transformers","lang":"bash","label":"Migration target"}],"dependencies":[{"reason":"Required for PyTorch models.","package":"torch","optional":false},{"reason":"Used for numerical operations.","package":"numpy","optional":false}],"imports":[{"note":"Correct import is from pytorch_pretrained_bert directly.","wrong":"from pytorch_pretrained_bert.model import BertModel","symbol":"BertModel","correct":"from pytorch_pretrained_bert import BertModel"},{"note":"Common mistake: trying to import from submodules.","wrong":"from pytorch_pretrained_bert.tokenization import BertTokenizer","symbol":"BertTokenizer","correct":"from pytorch_pretrained_bert import BertTokenizer"}],"quickstart":{"code":"from pytorch_pretrained_bert import BertTokenizer, BertModel\nimport torch\n\n# Load pre-trained model tokenizer (vocabulary)\ntokenizer = BertTokenizer.from_pretrained('bert-base-uncased')\n\n# Encode text\ntext = \"Who was Jim Henson?\"\ntokenized_text = tokenizer.tokenize(text)\nindexed_tokens = tokenizer.convert_tokens_to_ids(tokenized_text)\n\n# Convert to PyTorch tensors\ntokens_tensor = torch.tensor([indexed_tokens])\n\n# Load pre-trained model\nmodel = BertModel.from_pretrained('bert-base-uncased')\nmodel.eval()\n\n# Predict hidden states features\nwith torch.no_grad():\n    outputs = model(tokens_tensor)\nprint(outputs[0].shape)  # (batch_size, seq_len, hidden_size)","lang":"python","description":"Minimal example: tokenize input, load BERT, and get encoder output."},"warnings":[{"fix":"Run: pip install transformers. Then replace imports: from transformers import BertTokenizer, BertModel.","message":"pytorch-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.","severity":"deprecated","affected_versions":"all"},{"fix":"Use outputs.last_hidden_state or outputs[0] consistently across both libs.","message":"The 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.","severity":"breaking","affected_versions":">=0.6.0"},{"fix":"Set BERT_CACHE_DIR to a local path, e.g., import os; os.environ['BERT_CACHE_DIR'] = './cache'.","message":"Tokenizer.from_pretrained() downloads files from S3. If you have network issues, it fails silently. Pre-download or use environment variable BERT_CACHE_DIR.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Use correct import: `from pytorch_pretrained_bert import BertModel`.","cause":"Importing the class directly from the submodule, e.g., `from pytorch_pretrained_bert.model import BertModel`.","error":"AttributeError: 'BertModel' object has no attribute 'from_pretrained'"},{"fix":"Run `pip install pytorch-pretrained-bert==0.6.2`. If you are offline, download the wheel from PyPI.","cause":"The library is not installed or pip install failed.","error":"ModuleNotFoundError: No module named 'pytorch_pretrained_bert'"},{"fix":"Reinstall: `pip uninstall pytorch-pretrained-bert && pip install pytorch-pretrained-bert==0.6.2`.","cause":"Corrupted installation or version mismatch.","error":"ImportError: cannot import name 'BertTokenizer' from 'pytorch_pretrained_bert'"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}