What is Text Generation in transformers?

This recipe explains what is Text Generation in transformers.

Recipe Objective - What is Text Generation in transformers?

In text generation (also known as open text generation), the goal is to create a coherent part of the text that is a continuation of a given context. The following example shows how to use GPT2 in a pipeline to generate text. By default, all models apply TopK sampling when used in pipeline, as configured in their respective configuration (for example, see gpt2 configuration).

Get Access to Plant Species Identification Project using Machine Learning

For more related projects -

/projects/data-science-projects/deep-learning-projects
/projects/data-science-projects/keras-deep-learning-projects

Example of text generation using pipeline:

# Importing libraries
from transformers import pipeline

# Creating pipeline for text-generation
model_text_generator = pipeline("text-generation")

# Passing sequence to model
print(model_text_generator("I have never watched anything like this and", max_length=20, do_sample=False))

Output -
[{'generated_text': "I have never watched anything like this and I'm not sure if I'll ever watch it again."}]

In this way, we can perform text generation in transformers.

What Users are saying..

profile image

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

Insurance Pricing Forecast Using XGBoost Regressor
In this project, we are going to talk about insurance forecast by using linear and xgboost regression techniques.

Time Series Forecasting Project-Building ARIMA Model in Python
Build a time series ARIMA model in Python to forecast the use of arrival rate density to support staffing decisions at call centres.

Loan Eligibility Prediction Project using Machine learning on GCP
Loan Eligibility Prediction Project - Use SQL and Python to build a predictive model on GCP to determine whether an application requesting loan is eligible or not.

Detectron2 Object Detection and Segmentation Example Python
Object Detection using Detectron2 - Build a Dectectron2 model to detect the zones and inhibitions in antibiogram images.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Isolation Forest Model and LOF for Anomaly Detection in Python
Credit Card Fraud Detection Project - Build an Isolation Forest Model and Local Outlier Factor (LOF) in Python to identify fraudulent credit card transactions.

Deep Learning Project for Time Series Forecasting in Python
Deep Learning for Time Series Forecasting in Python -A Hands-On Approach to Build Deep Learning Models (MLP, CNN, LSTM, and a Hybrid Model CNN-LSTM) on Time Series Data.

End-to-End Speech Emotion Recognition Project using ANN
Speech Emotion Recognition using RAVDESS Audio Dataset - Build an Artificial Neural Network Model to Classify Audio Data into various Emotions like Sad, Happy, Angry, and Neutral

Build Time Series Models for Gaussian Processes in Python
Time Series Project - A hands-on approach to Gaussian Processes for Time Series Modelling in Python

Skip Gram Model Python Implementation for Word Embeddings
Skip-Gram Model word2vec Example -Learn how to implement the skip gram algorithm in NLP for word embeddings on a set of documents.