Skip to content

Instantly share code, notes, and snippets.

@IlievskiV
Created January 15, 2021 09:33
Show Gist options
  • Save IlievskiV/2bb3d315cacd8309e205fa1d41a6a74b to your computer and use it in GitHub Desktop.
Save IlievskiV/2bb3d315cacd8309e205fa1d41a6a74b to your computer and use it in GitHub Desktop.
How to use a pre-trained BERT
from transformers import BertTokenizer, BertModel
import torch
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = BertModel.from_pretrained('bert-base-uncased')
inputs = tokenizer("[CLS] This is very awesome!", return_tensors="pt")
outputs = model(**inputs)
# the learned representation for the [CLS] token
cls = outputs.last_hidden_state[0, 0, :]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment