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
- InstallationInstall the `transformers` library using pip.
- Loading a Pre-trained ModelLoad a pre-trained model and tokenizer. Here, we'll use the BERT model.
- TokenizationTokenize the input text using the loaded tokenizer.
- Model InferencePass 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