Selected topic
Gpt Models
Prefer practical output? Use related tools below while reading.
GPT models are a type of transformer-based language model that has revolutionized natural language processing (NLP) tasks. These models are trained on large datasets and can generate human-like text, making them useful for various applications such as chatbots, language translation, and text summarization.
python
import torch
from transformers import GPT2Tokenizer, GPT2Model# Load pre-trained GPT-2 model and tokenizer
tokenizer = GPT2Tokenizer.from_pretrained('gpt2-medium')
model = GPT2Model.from_pretrained('gpt2-medium')
# Define a function to generate response
def generate_response(input_text):
inputs = tokenizer.encode_plus(input_text,
max_length=512,
padding='max_length',
truncation=True,
return_attention_mask=True,
return_tensors='pt')
outputs = model(inputs['input_ids'], attention_mask=inputs['attention_mask'])
last_hidden_state = outputs.last_hidden_state
# Take the first token (index 0) as the response
response = tokenizer.decode(last_hidden_state[0][0], skip_special_tokens=True)
return response
# Example usage
user_input = "What is the meaning of life?"
response = generate_response(user_input)
print(response)
In this example, we load a pre-trained GPT-2 model and use it to generate a response to the user's query. The generate_response function takes an input text, encodes it using the tokenizer, passes it through the model, and returns the generated response.
Note that this is a highly simplified example, and in practice, you would need to fine-tune the model on your specific dataset and task to achieve good results.