Created
February 10, 2020 02:46
-
-
Save Felflare/59780c7b25d04660621fd4281e8c6013 to your computer and use it in GitHub Desktop.
Demonstration of XLNet with Classification head on top, implementation of XLNet follows huggingface's pytorch build.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from transformers import XLNetTokenizer, XLNetForSequenceClassification | |
import torch | |
tokenizer = XLNetTokenizer.from_pretrained('xlnet-large-cased') | |
model = XLNetForSequenceClassification.from_pretrained('xlnet-large-cased') | |
input_ids = torch.tensor(tokenizer.encode("Hello, my dog is cute", add_special_tokens=True)).unsqueeze(0) # Batch size 1 | |
labels = torch.tensor([1]).unsqueeze(0) # Batch size 1 | |
outputs = model(input_ids, labels=labels) | |
loss, logits = outputs[:2] | |
print(f'Current Loss at -- {loss.tolist()}') | |
# Current Loss at -- 1.1906177997589111 | |
print(f'Output labels are -- {logits.tolist()[0]}') | |
# Output labels are -- [0.613148033618927, -0.21501630544662476] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment