生成英文文本 (GPT-2)
> 用 pipeline 生成
`from transformers import pipeline
generator = pipeline("text-generation", model="gpt2")
prompt = "Once upon a time,"
out = generator(
prompt,
max_new_tokens=60,
do_sample=True,
temperature=0.9,
top_k=50,
top_p=0.95,
num_return_sequences=2
)
for i, o in enumerate(out):
print("=== sequence", i+1, "===\n")
print(o["generated_text"])
print("\n")`
實驗
print("=== max_new_tokens = 80 ===")
out = generator(prompt, max_new_tokens=80, do_sample=True)
print(out[0]["generated_text"], "\n")
`
pad_token_id
to eos_token_id
:50256 for open-end generation.pad_token_id
to eos_token_id
:50256 for open-end generation.=== max_new_tokens = 80 ===
Once upon a time, we were all part of the same group of individuals.
Now, all the data we have, all the data we now have about women's health, our sexual and reproductive health, and our health, all of that is being produced in a society that is essentially misogynistic, that is in a state of total denial: they're not just doing research, they're doing research. And you
print("=== temperature = 0.2 (保守) ===")
out = generator(prompt, max_new_tokens=50, do_sample=True, temperature=0.2)
print(out[0]["generated_text"], "\n")
print("=== temperature = 1.2 (更隨機) ===")
out = generator(prompt, max_new_tokens=50, do_sample=True, temperature=1.2)
print(out[0]["generated_text"], "\n")
pad_token_id
to eos_token_id
:50256 for open-end generation.pad_token_id
to eos_token_id
:50256 for open-end generation.The people are
=== temperature = 1.2 (更隨機) ===
Once upon a time, there were a few people who did nothing but watch the screen play on TV, like if somebody who worked at your company gave you a job in a particular field or that you called up some friend, so all you had to do was just watch a
print("=== top_k = 10 (限制更嚴格) ===")
out = generator(prompt, max_new_tokens=50, do_sample=True, top_k=10, temperature=0.9)
print(out[0]["generated_text"], "\n")
print("=== top_k = 100 (選擇更自由) ===")
out = generator(prompt, max_new_tokens=50, do_sample=True, top_k=100, temperature=0.9)
print(out[0]["generated_text"], "\n")
pad_token_id
to eos_token_id
:50256 for open-end generation.pad_token_id
to eos_token_id
:50256 for open-end generation.The sky was white and clear as if a sun was shining. The sun was shining, and it was all in the world.
A moment later the world was like this:
The world was
=== top_k = 100 (選擇更自由) ===
Once upon a time, the Lord Almighty summoned the heavens, the earth and all the things that exist, in order that His creatures and the Creator might be filled of His glorious knowledge."23 Now it is written, "You will know that God was the Lord Himself from before
print("=== top_p = 0.8 (保守,少數詞) ===")
out = generator(prompt, max_new_tokens=50, do_sample=True, top_p=0.8, temperature=0.9)
print(out[0]["generated_text"], "\n")
print("=== top_p = 0.95 (更開放) ===")
out = generator(prompt, max_new_tokens=50, do_sample=True, top_p=0.95, temperature=0.9)
print(out[0]["generated_text"], "\n")
pad_token_id
to eos_token_id
:50256 for open-end generation.pad_token_id
to eos_token_id
:50256 for open-end generation.=== top_p = 0.95 (更開放) ===
Once upon a time, even when he was still young, he would have dreamed of becoming a priest."
Then in his youth, he had a tendency toward the world of the priest. His imagination could conceive of the world of the priest. He even envisioned his own
print("=== num_return_sequences = 3 ===")
out = generator(prompt, max_new_tokens=50, do_sample=True, temperature=0.9, num_return_sequences=3)
for i, o in enumerate(out):
print(f"--- sequence {i+1} ---")
print(o["generated_text"], "\n")
pad_token_id
to eos_token_id
:50256 for open-end generation.--- sequence 2 ---
Once upon a time, after I'd been writing about him I would not have been able to get up from my stool before we were set to go to bed. I would not have been able to sleep past 8 AM as I had been sitting there for hours. Every time
--- sequence 3 ---
Once upon a time, there were many things that were good about our company. We had great writers – writers that worked well within the company, and people that were extremely passionate with us.
With the help of my friends, a lot of folks found the company rewarding