22.02.2024 Views

Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

loaded_model = (AutoModelForSequenceClassification

.from_pretrained('bert_alice_vs_wizard'))

loaded_model.device

Output

device(type='cpu')

The model is loaded to the CPU by default, but we can send it to a different device

in the usual PyTorch way:

device = 'cuda' if torch.cuda.is_available() else 'cpu'

loaded_model.to(device)

loaded_model.device

Output

device(type='cuda', index=0)

Predictions

We can finally answer the most important question of all: Where does the

sentence in the title, "Down the Yellow Brick Rabbit Hole," come from? Let’s ask

BERT:

sentence = 'Down the yellow brick rabbit hole'

tokens = auto_tokenizer(sentence, return_tensors='pt')

tokens

Output

{'input_ids': tensor([[ 101, 2091, 1996, 3756, 5318, 10442,

4920, 102]]),

'attention_mask': tensor([[1, 1, 1, 1, 1, 1, 1, 1]])}

After tokenizing the sentence, we need to make sure the tensors are in the same

device as the model.

Fine-Tuning with HuggingFace | 999

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!