Párametros del MLM
Como cualquier modelo de aprendizaje automático, los grandes modelos de lenguaje tienen varios parámetros que controlan la variación de la salida de texto generada. Hemos iniciado una serie multi-parte para explicar detalladamente el impacto de estos parámetros. Concluiremos alcanzando un equilibrio perfecto en la generación de contenido utilizando todos estos parámetros discutidos en nuestra serie multi-parte.
Bienvenido al segundo parte, donde discutimos otro parámetro bien conocido, la “Top-P”.
Top-P (Elección de Núcleo)
Si el objetivo es controlar la diversidad de la salida del modelo, entonces Top-P es la opción ideal para ti. Un Top-P bajo obliga al modelo a usar las palabras más probables, mientras que un Top-P alto obliga al modelo a usar palabras más diversas, aumentando la creatividad.
Veamos la acción de Top-P con el siguiente código y salida.
import torch
from transformers import GPT2LMHeadModel, GPT2Tokenizer
# Load GPT-2 model and tokenizer
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
model = GPT2LMHeadModel.from_pretrained("gpt2")
# Add pad token to tokenizer (GPT-2 doesn't have it by default)
tokenizer.pad_token = tokenizer.eos_token
# Function to generate response with varying top_p
def generate_with_top_p(prompt, top_p):
inputs = tokenizer(prompt, return_tensors='pt', padding=True)
# Set the attention_mask and pad_token_id
outputs = model.generate(
inputs.input_ids,
attention_mask=inputs['attention_mask'],
do_sample=True,
max_length=200,
top_p=top_p,
pad_token_id=tokenizer.eos_token_id
)
return tokenizer.decode(outputs[0], skip_special_tokens=True)
prompt = "What are some effective ways to manage stress in daily life?"
# List of top-p values and their descriptions
top_p_values = {
0.1: "Very conservative: Generates highly probable and safe responses.",
0.3: "Conservative: Generates probable responses with less risk.",
0.5: "Balanced: A mix of safe and creative responses.",
0.7: "Creative: Generates more diverse and creative responses.",
0.9: "Very creative: Allows for highly diverse and less probable responses."
}
# Test top_p variations
for top_p, description in top_p_values.items():
print(f"\nTop-p {top_p} ({description}):\n")
print(generate_with_top_p(prompt, top_p=top_p))
Salida:
python test_top_p.py
Top-p 0.1 (Very conservative: Generates highly probable and safe responses.):
What are some effective ways to manage stress in daily life?
The following are some of the most common ways to manage stress in daily life.
1. Avoiding stress
The most common way to avoid stress is to avoid it.
The most common way to avoid stress is to avoid it.
2. Avoiding stress
The most common way to avoid stress is to avoid it.
The most common way to avoid stress is to avoid it.
3. Avoiding stress
The most common way to avoid stress is to avoid it.
The most common way to avoid stress is to avoid it.
4. Avoiding stress
The most common way to avoid stress is to avoid it.
The most common way to avoid stress is to avoid it.
5. Avoiding stress
The most common way to avoid stress is to avoid it.
The most common way to avoid stress
Top-p 0.3 (Conservative: Generates probable responses with less risk.):
What are some effective ways to manage stress in daily life?
What are some effective ways to manage stress in daily life?
What are some effective ways to manage stress in daily life?
What are some effective ways to manage stress in daily life?
What are some effective ways to manage stress in daily life?
What are some effective ways to manage stress in daily life?
What are some effective ways to manage stress in daily life?
What are some effective ways to manage stress in daily life?
What are some effective ways to manage stress in daily life?
What are some effective ways to manage stress in daily life?
What are some effective ways to manage stress in daily life?
What are some effective ways to manage stress in daily life?
What are some effective ways to manage stress in daily life?
What are some effective ways to manage stress in daily life?
What are some effective
Top-p 0.5 (Balanced: A mix of safe and creative responses.):
What are some effective ways to manage stress in daily life?
1. Stay on top of your body's natural stress levels
When you're stressed, your body's natural stress levels are low.
If you're stressed, your body's natural stress levels are high.
If you're stressed, your body's natural stress levels are low.
2. Avoid excessive exercise
Exercise can make you feel better.
Exercise can make you feel better.
3. Get up early to avoid fatigue
Exercise can make you feel better.
Exercise can make you feel better.
4. Avoid the temptation to take the wrong thing
Exercise can make you feel better.
Exercise can make you feel better.
5. Avoid eating the wrong foods
Exercise can make you feel better.
Exercise can make you feel better.
6. Avoid the temptation to
Top-p 0.7 (Creative: Generates more diverse and creative responses.):
What are some effective ways to manage stress in daily life?
I am talking about a very specific situation. The person I am talking about has been stressed, but has not been doing much work for a long time. I want to tell you, because this person has had a lot of stress in his life, that it is not something you can just go back to. But what I'm trying to say is, that if you don't have a job, you have to go back to work every day, so you can spend more time with your family. So I've been doing that for a long time now. And so, that is a very common occurrence.
But what do you think is the best way to deal with the stress?
You know, it's not easy to deal with it. It is very difficult to deal with the stress that we experience. So, that is a very good way to deal with it. So, I think it's the
Top-p 0.9 (Very creative: Allows for highly diverse and less probable responses.):
What are some effective ways to manage stress in daily life?
There are many things that can be done by daily meditation and practice. As a general rule of thumb, meditation can help you stay mindful of your own needs, goals, feelings, desires, emotions, and the life and emotions around you.
The purpose of meditation is to feel a deep desire to practice more, to be more mindful, and to be more productive. It also serves to enhance your overall well-being.
1. Be active, be creative, be mindful, and be optimistic.
This is where the first step towards meditation comes from. If we're looking for inspiration, there's a whole section on being "active" and "creative."
While I'm not sure I know much about meditation, I know some of its practitioners and some that I never met. My mom used to tell me that she'd always find a way to make her feel more connected and involved.
Ahora vamos a entender la salida.
- Top-P 0.1 – Muy Conservador:Como el modelo selecciona las opciones de palabra siguiente más probables del 10%, hay mucha repetición en el contenido generado. Por lo tanto, esta respuesta carece de diversidad y es también insuficientemente informativa en la mayoría de los casos.
- Top-P 0.3 – Conservador: El modelo selecciona de las primeras 30% de las opciones de palabra más probables, por lo que es un poco menos conservador que el ajuste anterior de Top-P. Como puede ver en la salida, esto no ha mejorado la generación de contenido, y el prompt se repitió a lo largo de la completación. En este caso, la repetición del prompt significa que la continuación más probable después del prompt para el modelo parece ser el propio prompt.
- Top-P 0.5 – Equilibrado: Aquí es donde vemos que el modelo enumera por primera vez algunas estrategias numeradas. Aún se observa alguna repetición en este ajuste también. Pero lo importante es que en este ajuste de Top-P, el modelo comienza a incorporar una gama más amplia de palabras. La salida es una mezcla de consejos estándar con algunas inconsistencias. Este valor de Top-P permite una mejor creatividad pero aún lucha con la profundidad de la información.
- Top-P 0.7 – Creativo: En este caso, el modelo puede seleccionar de una gama más amplia de palabras, y como puede ver, la respuesta se está desplazando hacia un estilo narrativo. El contenido es más creativo ya que ahora se involucra en un escenario donde una persona está lidiando con estrés. El lado negativo es la pérdida de enfoque, ya que el énfasis no fue en manejar el estrés sino en las dificultades en afrontar el estrés.
- Top-P 0.9 – Muy Creativo: En este ajuste, el modelo tiene acceso a una amplia gama de vocabulario y ideas, incluyendo palabras y conceptos menos probables. Este ajuste permite que el modelo use un lenguaje más expresivo. Otra vez, el lado negativo de ser muy creativo es que el modelo se desvía del prompt en la búsqueda de producir contenido rico y variado.
Lo importante que se destaca de la ejercitación anterior es cómo el contenido cambia con el cambio en la configuración de Top-P. También nos da una idea de que este parámetro no es el único que debe ser manejado para variar el contenido y su relevancia.
Ahora, veamos el impacto de Top-P en un par de casos de uso, justo como la parte anterior de esta serie sobre “Generación Creativa de Historias” y “Explicación Técnica”.
import torch
from transformers import GPT2LMHeadModel, GPT2Tokenizer
# Load GPT-2 model and tokenizer
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
model = GPT2LMHeadModel.from_pretrained("gpt2")
# Add pad token to tokenizer (GPT-2 doesn't have it by default)
tokenizer.pad_token = tokenizer.eos_token
# Function to generate response based on top_p
def generate_with_top_p(prompt, top_p, max_length=250):
inputs = tokenizer(prompt, return_tensors='pt')
outputs = model.generate(
inputs.input_ids,
attention_mask=inputs.attention_mask,
do_sample=True,
max_length=max_length,
top_p=top_p,
pad_token_id=tokenizer.eos_token_id,
eos_token_id=tokenizer.eos_token_id,
no_repeat_ngram_size=2 # Prevents repetition of phrases
)
return tokenizer.decode(outputs[0], skip_special_tokens=True)
### USE CASE 1: CREATIVE STORY GENERATION ###
def creative_story_generation():
prompt = ("In the mystical land of Eldoria, a young warrior found an ancient map "
"that led to a hidden treasure guarded by dragons. He knew that courage and "
"wisdom would be his allies on this perilous journey.")
# Negative Impact: Low top_p for creative writing (less creative)
print("\n=== Creative Story with Low top_p (0.2) - Negative Impact: ===")
low_top_p_story = generate_with_top_p(prompt, top_p=0.2)
print(low_top_p_story)
# Perfect Impact: High top_p for creative writing (more creative)
print("\n=== Creative Story with High top_p (0.95) - Perfect Impact: ===")
high_top_p_story = generate_with_top_p(prompt, top_p=0.95)
print(high_top_p_story)
### USE CASE 2: TECHNICAL EXPLANATION ###
def technical_explanation():
prompt = ("Explain step by step how the internet works, focusing on how computers "
"use IP addresses and data packets to communicate with each other.")
# Negative Impact: High top_p for technical writing (less precise)
print("\n=== Technical Explanation with High top_p (0.95) - Negative Impact: ===")
high_top_p_explanation = generate_with_top_p(prompt, top_p=0.95)
print(high_top_p_explanation)
# Perfect Impact: Optimal top_p for technical writing (accurate)
print("\n=== Technical Explanation with Optimal top_p (0.5) - Perfect Impact: ===")
optimal_top_p_explanation = generate_with_top_p(prompt, top_p=0.5)
print(optimal_top_p_explanation)
# Run both use cases
creative_story_generation()
technical_explanation()
Salida:
python top_p_multiple.py
=== Creative Story with Low top_p (0.2) - Negative Impact: ===
In the mystical land of Eldoria, a young warrior found an ancient map that led to a hidden treasure guarded by dragons. He knew that courage and wisdom would be his allies on this perilous journey.
The Dragon King
...
(The Book of the Dragon)
,
-
: The Dragon Lord is a legendary warrior who has been the focus of many legends. The dragon king is the most powerful of all the dragons in the world. In the magical land, he is known as the "Dragon King". He is also known to be the leader of a group of dragons called the Black Dragons. His name is derived from the dragon's name, "the dragon".
"The Black Dragon" is an important symbol of power and powerlessness. It is said that the black dragon is able to create a dragon that can defeat the strongest of his enemies. However, the true power of this dragon lies in his ability to manipulate the minds of others. This ability is called "The Dark Dragon". The Dark dragon has a powerful sense of self-preservation and is capable of manipulating others to his will. When he has control over others, his power is so great that he can destroy entire cities. As a result
=== Creative Story with High top_p (0.95) - Perfect Impact: ===
In the mystical land of Eldoria, a young warrior found an ancient map that led to a hidden treasure guarded by dragons. He knew that courage and wisdom would be his allies on this perilous journey.
Spirits are like gods. In this world, there are no gods without secrets. There are also no secrets about being a fighter or a thief. But every dragon has a special hidden skill, and he or she can use that skill to destroy and gain strength or hide something hidden in the secret. Many dragons are skilled at their martial arts, while most are unaware of the secrets of their true power. These dragons cannot only use these skills, but that will only allow them to escape the dragons' clutches. Because their training will be tested before they're even born, dragon fighting has never been so hard, even without training, so they should be able to break a dragon's body.
=== Technical Explanation with High top_p (0.95) - Negative Impact: ===
Explain step by step how the internet works, focusing on how computers use IP addresses and data packets to communicate with each other. If a person with the same identity as a user on the US government's private network uses the online address bar, then this data is sent to a server on a computer on your local network. Your IP address is a small byte in the string. The IP and network address are identical. Do you remember, you just want to do that instead of using IPs or numbers. In addition, remember that IP can be used to verify a particular IP for you and your computer. For instance, your name does not always match an address on our government network and you should have your public IP in this country. This does seem quite unusual and perhaps a bit bizarre.
There was a time in Silicon Valley when you could set your identity out. But in most of today's world, how do you set up your own address and how does one look for it? What about the public? The internet itself was different. It was just a set of rules around data flow that you were supposed to follow. Now, even in today the "internet in general" seems a little more complicated to define. Let's say
=== Technical Explanation with Optimal top_p (0.5) - Perfect Impact: ===
Explain step by step how the internet works, focusing on how computers use IP addresses and data packets to communicate with each other.
"We've been trying to understand how it works and what it means for the future," says James. "It's not just about the IP address, it's about how people communicate. It's also about what's going on with the data. We want to see how this works. What is the Internet going to look like in the next 10 years?"
, the director of the Computer Science and Artificial Intelligence Laboratory at the University of Michigan, says that while there's still a lot of work to be done, "we've got to start to think about it."
Ahora veamos y analizemos la salida para la generación creativa de historias y la explicación técnica basada en las configuraciones de Top-P y cómo la salida fue afectada.
Para demostrar efectivamente el impacto de Top-P, hemos integrado mejores prompts para dirigir la salida de manera que el impacto sea fácil de observar.
Generación Creativa de Historias
- Bajo Top-P (Impacto Negativo):Como pueden ver con el bajo Top-P, el modelo se ve restringido al uso de palabras o frases y, por lo tanto, causa repetición y redundancia. La creatividad también se limita en este caso, ya que el modelo intenta no introduce ideas nuevas. Sin embargo, si notan, se mantiene aún la fluidez lógica y el modelo se mantiene en el tema, lo cual es típico de valores bajos de Top-P.
- Alto Top-P (Impacto Perfecto):En este caso, el modelo introduce conceptos nuevos y añade un ángulo creativo a la narración. Se utiliza un vocabulario más amplio, agregando profundidad y riqueza al texto. Sin embargo, debido a la creatividad aumentada, se ha reprimido la fluidez lógica.
La contraste entre las dos narrativas muestra claramente el impacto de Top-P, haciendo fácil comprender cómo afecta a la escritura creativa.
Explicación Técnica
- Alta Top-P (Impacto Negativo):Como puede verse, una alta Top-P tiene un impacto negativo en las explicaciones técnicas, ya que impide un flujo lógico y distrae del tema. El modelo también introduce información irrelevante que no pertenece a la explicación.
- Top-P Óptimo (Impacto Perfecto):La explicación es más coherente y cercana al tema con un Top-P óptimo. El contenido se alinea más con el prompt y equilibra bien la precisión y la expresión. La confiabilidad de la información se mejora porque el modelo se limita a palabras más probables.
Conclusión
Con este experimento, hemos exitosamente mostrado la importancia del parámetro Top-P para controlar la randomidad y la creatividad del texto generado. Primero examinamos un solo prompt y cómo varía la salida con diferentes Top-P, y luego adoptamos un enfoque basado en casos de uso para ver cómo Top-P controla la salida según el caso de uso.
Sin embargo, como se ha notado en partes anteriores y en esta parte de la serie, individualmente, cada parámetro no hace justicia a la calidad de la generación de contenido. Es por eso que es fundamental considerar el impacto de todos estos parámetros, y lo haremos en la parte final de esta serie.
Source:
https://dzone.com/articles/decoding-llm-parameters-top-p