Introduction
GPT, short for Generative Pre-trained Transformer, is a family of transformer-based language models. Known as an example of an early transformer-based model capable of generating coherent text, OpenAI's GPT-2 was one of the initial triumphs of its kind and can be used as a tool for a variety of applications, including helping to write content in a more creative way. The Hugging Face Transformers library is a library of pre-trained models that makes it easy to work with these sophisticated language models.
Creative content generation could be valuable, for example, in the world of data science and machine learning, where it could be used in a variety of ways to improve boring reports, create synthetic data, or simply help guide the telling of a larger story. interesting. This tutorial will guide you through using GPT-2 with the Hugging Face Transformers library to generate creative content. Note that we use the GPT-2 model here for its simplicity and manageable size, but changing it to another generative model will follow the same steps.
Set up the environment
Before we begin, we need to configure our environment. This will involve installing and importing the necessary libraries and importing the necessary packages.
Install the necessary libraries:
pip install transformers torch
Import the required packages:
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
You can learn about auto classes and auto models from Huging Face. here. Forward.
Loading the model and tokenizer
Next, we will load the model and tokenizer into our script. The model in this case is GPT-2, while the tokenizer is responsible for converting the text into a format that the model can understand.
model_name = "gpt2"
model = AutoModelForCausalLM.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
Please note that changing the model name above may swap different Hugging Face language models.
Preparing input text for generation
In order for our model to generate text, we must provide the model with an initial input or message. This message will be tokenized by the tokenizer.
prompt = "Once upon a time in Detroit, "
input_ids = tokenizer(prompt, return_tensors="pt").input_ids
Please note that the return_tensors="pt"
The argument ensures that PyTorch tensors are returned.
Generating creative content
Once the input text has been tokenized and prepared for input into the model, we can use the model to generate creative content.
gen_tokens = model.generate(input_ids, do_sample=True, max_length=100, pad_token_id=tokenizer.eos_token_id)
gen_text = tokenizer.batch_decode(gen_tokens)(0)
print(gen_text)
Customizing the build with advanced settings
For more creativity, we can adjust the temperature and use top-k sampling and top-p (core) sampling.
Adjust temperature:
gen_tokens = model.generate(input_ids, do_sample=True, max_length=100, temperature=0.7, pad_token_id=tokenizer.eos_token_id)
gen_text = tokenizer.batch_decode(gen_tokens)(0)
print(gen_text)
Using top-k sampling and top-p sampling:
gen_tokens = model.generate(input_ids, do_sample=True, max_length=100, top_k=50, top_p=0.95, pad_token_id=tokenizer.eos_token_id)
gen_text = tokenizer.batch_decode(gen_tokens)(0)
print(gen_text)
Practical examples of creative content generation
Below are some practical examples of using GPT-2 to generate creative content.
# Example: Generating story beginnings
story_prompt = "In a world where ai contgrols everything, "
input_ids = tokenizer(story_prompt, return_tensors="pt").input_ids
gen_tokens = model.generate(input_ids, do_sample=True, max_length=150, temperature=0.4, top_k=50, top_p=0.95, pad_token_id=tokenizer.eos_token_id)
story_text = tokenizer.batch_decode(gen_tokens)(0)
print(story_text)
# Example: Creating poetry lines
poetry_prompt = "Glimmers of hope rise from the ashes of forgotten tales, "
input_ids = tokenizer(poetry_prompt, return_tensors="pt").input_ids
gen_tokens = model.generate(input_ids, do_sample=True, max_length=50, temperature=0.7, pad_token_id=tokenizer.eos_token_id)
poetry_text = tokenizer.batch_decode(gen_tokens)(0)
print(poetry_text)
Summary
Experimenting with different parameters and settings can significantly affect the quality and creativity of the generated content. GPT, especially the newer versions we all know, has enormous potential in creative fields, allowing data scientists to generate compelling narratives, synthetic data, and more. For further reading, consider exploring the Hugging Face documentation and other resources to deepen your understanding and expand your skills.
If you follow this guide, you should now be able to harness the power of GPT-3 and Hugging Face Transformers to generate creative content for various applications in data science and more.
For additional information on these topics, see the following resources:
Matthew May (twitter.com/mattmayo13″ rel=”noopener”>@mattmayo13) has a master's degree in computer science and a postgraduate diploma in data mining. As Editor-in-Chief, Matthew aims to make complex data science concepts accessible. His professional interests include natural language processing, machine learning algorithms, and exploring emerging ai. He is driven by the mission to democratize knowledge in the data science community. Matthew has been coding since he was 6 years old.
<script async src="//platform.twitter.com/widgets.js” charset=”utf-8″>