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

peiying410632
from peiying410632 More from this publisher
22.02.2024 Views

logits.logits.argmax(dim=1)Outputtensor([0], device='cuda:0')BERT has spoken: The sentence "Down the Yellow Brick Rabbit Hole" is more likelycoming from The Wonderful Wizard of Oz (the negative class of our binaryclassification task).Don’t you think that’s a lot of work to get predictions for a single sentence? I mean,tokenizing, sending it to the device, feeding the inputs to the model, getting thelargest logit—that’s a lot, right? I think so, too.PipelinesPipelines can handle all these steps for us, we just have to choose the appropriateone. There are many different pipelines, one for each task, like theTextClassificationPipeline and the TextGenerationPipeline. Let’s use theformer to run our tokenizer and trained model at once:from transformers import TextClassificationPipelinedevice_index = (loaded_model.device.indexif loaded_model.device.type != 'cpu'else -1)classifier = TextClassificationPipeline(model=loaded_model,tokenizer=auto_tokenizer,device=device_index)Every pipeline takes at least two required arguments: a model and a tokenizer. Wecan also send it straight to the same device as our model, but we would need to usethe device index instead (-1 if it’s on a CPU, 0 if it’s on the first or only GPU, 1 if it’son the second one, and so on).Now we can make predictions using the original sentences:classifier(['Down the Yellow Brick Rabbit Hole', 'Alice rules!'])Fine-Tuning with HuggingFace | 1001

Output[{'label': 'LABEL_0', 'score': 0.9951714277267456},{'label': 'LABEL_1', 'score': 0.9985325336456299}]The model seems pretty confident that the first sentence is from The WonderfulWizard of Oz (negative class) and that the second sentence is from Alice’sAdventures in Wonderland (positive class).We can make the output a bit more intuitive, though, by setting proper labels foreach of the classes using the id2label attribute of our model’s configuration:loaded_model.config.id2label = {0: 'Wizard', 1: 'Alice'}Let’s try it again:classifier(['Down the Yellow Brick Rabbit Hole', 'Alice rules!'])Output[{'label': 'Wizard', 'score': 0.9951714277267456},{'label': 'Alice', 'score': 0.9985325336456299}]That’s much better!More PipelinesIt’s also possible to use pre-trained pipelines for typical tasks like sentimentanalysis without having to fine-tune your own model:from transformers import pipelinesentiment = pipeline('sentiment-analysis')That’s it! The task defines which pipeline is used. For sentiment analysis, thepipeline above loads a TextClassificationPipeline like ours, but one that’s pretrainedto perform that task.1002 | Chapter 11: Down the Yellow Brick Rabbit Hole

logits.logits.argmax(dim=1)

Output

tensor([0], device='cuda:0')

BERT has spoken: The sentence "Down the Yellow Brick Rabbit Hole" is more likely

coming from The Wonderful Wizard of Oz (the negative class of our binary

classification task).

Don’t you think that’s a lot of work to get predictions for a single sentence? I mean,

tokenizing, sending it to the device, feeding the inputs to the model, getting the

largest logit—that’s a lot, right? I think so, too.

Pipelines

Pipelines can handle all these steps for us, we just have to choose the appropriate

one. There are many different pipelines, one for each task, like the

TextClassificationPipeline and the TextGenerationPipeline. Let’s use the

former to run our tokenizer and trained model at once:

from transformers import TextClassificationPipeline

device_index = (loaded_model.device.index

if loaded_model.device.type != 'cpu'

else -1)

classifier = TextClassificationPipeline(model=loaded_model,

tokenizer=auto_tokenizer,

device=device_index)

Every pipeline takes at least two required arguments: a model and a tokenizer. We

can also send it straight to the same device as our model, but we would need to use

the device index instead (-1 if it’s on a CPU, 0 if it’s on the first or only GPU, 1 if it’s

on the second one, and so on).

Now we can make predictions using the original sentences:

classifier(['Down the Yellow Brick Rabbit Hole', 'Alice rules!'])

Fine-Tuning with HuggingFace | 1001

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

Saved successfully!

Ooh no, something went wrong!