22.02.2024 Views

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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 Wonderful

Wizard of Oz (negative class) and that the second sentence is from Alice’s

Adventures in Wonderland (positive class).

We can make the output a bit more intuitive, though, by setting proper labels for

each 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 Pipelines

It’s also possible to use pre-trained pipelines for typical tasks like sentiment

analysis without having to fine-tune your own model:

from transformers import pipeline

sentiment = pipeline('sentiment-analysis')

That’s it! The task defines which pipeline is used. For sentiment analysis, the

pipeline above loads a TextClassificationPipeline like ours, but one that’s pretrained

to perform that task.

1002 | Chapter 11: Down the Yellow Brick Rabbit Hole

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

Saved successfully!

Ooh no, something went wrong!