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

[215]. For a demo of GPT-2’s capabilities, please check AllenNLP’s LanguageModeling Demo, [216] which uses GPT-2’s medium model (345 million parameters).You can also check GPT-2’s documentation [217] and model card, [218]available at HuggingFace, for a quick overview of the modeland its training procedure.For a general overview of GPT-2, see this great post by JayAlammar: "The Illustrated GPT-2 (Visualizing TransformerLanguage Models)." [219]To learn more details about GPT-2’s architecture, please check"The Annotated GPT-2" [220] by Aman Arora.There is also Andrej Karpathy’s minimalistic implementation ofGPT, minGPT, [221] if you feel like trying to train a GPT model fromscratch.Let’s load the GPT-2-based text generation pipeline:text_generator = pipeline("text-generation")Then, let’s use the first two paragraphs from Alice’s Adventures in Wonderland asour base text:base_text = """Alice was beginning to get very tired of sitting by her sister onthe bank, and of having nothing to do: once or twice she had peepedinto the book her sister was reading, but it had no pictures orconversations in it, `and what is the use of a book,'thought Alice`without pictures or conversation?' So she was considering in herown mind (as well as she could, for the hot day made her feel verysleepy and stupid), whether the pleasure of making a daisy-chainwould be worth the trouble of getting up and picking the daisies,when suddenly a White Rabbit with pink eyes ran close by her."""The generator will produce a text of size max_length, including the base text, so thisvalue has to be larger than the length of the base text. By default, the model in theGPT-2 | 1005

text generation pipeline has its do_sample argument set to True to generate wordsusing beam search instead of greedy decoding:text_generator.model.config.task_specific_paramsOutput{'text-generation': {'do_sample': True, 'max_length': 50}}result = text_generator(base_text, max_length=250)print(result[0]['generated_text'])Output...Alice stared at the familiar looking man in red, in a white dress,and smiled shyly.She saw the cat on the grass and sat down with it gently andeagerly, with her arms up.There was a faint, long, dark stench, the cat had its tail held atthe end by a large furry, white fur over it.Alice glanced at it.It was a cat, but a White Rabbit was very good at drawing this,thinking over its many attributes, and making sure that no redsappearedI’ve removed the base text from the output above, so that’s generated text only.Looks decent, right? I tried it several times, and the generated text is usuallyconsistent, even though it digresses some times and, on occasion, generates somereally weird pieces of text."What is this beam search? That sounds oddly familiar."That’s true, we briefly discussed beam search (and its alternative, greedy decoding)1006 | Chapter 11: Down the Yellow Brick Rabbit Hole

text generation pipeline has its do_sample argument set to True to generate words

using beam search instead of greedy decoding:

text_generator.model.config.task_specific_params

Output

{'text-generation': {'do_sample': True, 'max_length': 50}}

result = text_generator(base_text, max_length=250)

print(result[0]['generated_text'])

Output

...

Alice stared at the familiar looking man in red, in a white dress,

and smiled shyly.

She saw the cat on the grass and sat down with it gently and

eagerly, with her arms up.

There was a faint, long, dark stench, the cat had its tail held at

the end by a large furry, white fur over it.

Alice glanced at it.

It was a cat, but a White Rabbit was very good at drawing this,

thinking over its many attributes, and making sure that no reds

appeared

I’ve removed the base text from the output above, so that’s generated text only.

Looks decent, right? I tried it several times, and the generated text is usually

consistent, even though it digresses some times and, on occasion, generates some

really weird pieces of text.

"What is this beam search? That sounds oddly familiar."

That’s true, we briefly discussed beam search (and its alternative, greedy decoding)

1006 | 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!