Skip to main content
Article·huggingface.co
nlptransformerspre-trained-modelspythonbert

Hugging Face Transformers

Learn how to use Hugging Face Transformers for NLP tasks with pre-trained models. This Action Pack guides you through installation, model loading, and basic text processing.

beginner15 minutes4 steps
The play
  1. Installation
    Install the `transformers` library using pip.
  2. Loading a Pre-trained Model
    Load a pre-trained model and tokenizer. Here, we'll use the BERT model.
  3. Tokenization
    Tokenize the input text using the loaded tokenizer.
  4. Model Inference
    Pass the tokenized input to the model and get the output.
Starter code
# Install transformers: pip install transformers
from transformers import BertTokenizer, BertModel

model_name = 'bert-base-uncased'
tokenizer = BertTokenizer.from_pretrained(model_name)
model = BertModel.from_pretrained(model_name)

text = "Hello, how are you?"
inputs = tokenizer(text, return_tensors="pt")
outputs = model(**inputs)
last_hidden_states = outputs.last_hidden_state

print(last_hidden_states.shape)
Source
Hugging Face Transformers — Action Pack