HuggingFace bert-large-uncased gives NameError

Carl_B
New Contributor II

Hello,

I am trying to run the LLM bert-large-uncased from HuggingFace.

I have downloaded the transformers from github. I have installed the various packages. I am now getting an error message: NameError: name 'torch' is not defined.

Not sure what the problem is. Is pytorch not installed? Any help would be appreciated.

C

 

lingareddy_Alva
Esteemed Contributor

Hi @Carl_B 

The error indicates that PyTorch is not installed in your environment. Just try below this.

Install PyTorch
Option 1: Using pip
pip install torch torchvision torchaudio

Option 2: Using conda (if you're using Anaconda/Miniconda)
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia

Option 3: CPU-only version (if no GPU)
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu

After installation, test it:
import torch
print(torch.__version__)
print(torch.cuda.is_available()) # Check if CUDA is available

Complete Setup for BERT
Once PyTorch is installed, you'll also need:
# Install transformers properly via pip instead of GitHub
pip install transformers

# Additional dependencies you might need
pip install datasets tokenizers accelerate

Test BERT Model Loading

from transformers import AutoTokenizer, AutoModel
import torch

# Load BERT model and tokenizer
model_name = "bert-large-uncased"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModel.from_pretrained(model_name)

print("BERT model loaded successfully!")
print(f"Model device: {next(model.parameters()).device}")

If You're Using Databricks
If you're running this on Databricks, install via notebook cell:
%pip install torch torchvision torchaudio transformers

Then restart the Python kernel:
dbutils.library.restartPython()

 

LR

View solution in original post