Update README.md
Browse files
README.md
CHANGED
|
@@ -30,8 +30,82 @@ TODO: Add your code
|
|
| 30 |
|
| 31 |
|
| 32 |
```python
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
...
|
| 37 |
```
|
|
|
|
| 30 |
|
| 31 |
|
| 32 |
```python
|
| 33 |
+
# ==============================================
|
| 34 |
+
# 🚀 PLANTILLA COMPLETA PARA GOOGLE COLAB (LunarLander-v2)
|
| 35 |
+
# Entrena y evalúa un modelo PPO para LunarLander-v2
|
| 36 |
+
# ==============================================
|
| 37 |
+
|
| 38 |
+
# 📌 Paso 1: Instalar Dependencias
|
| 39 |
+
!apt install swig cmake ffmpeg python3-opengl xvfb -y
|
| 40 |
+
!pip install stable-baselines3[extra] gymnasium[box2d] huggingface_sb3 pyvirtualdisplay shimmy
|
| 41 |
+
|
| 42 |
+
# 📌 Paso 2: Importar Librerías
|
| 43 |
+
import os
|
| 44 |
+
import gymnasium as gym
|
| 45 |
+
import numpy as np
|
| 46 |
+
from stable_baselines3 import PPO
|
| 47 |
+
from stable_baselines3.common.env_util import make_vec_env
|
| 48 |
+
from stable_baselines3.common.monitor import Monitor
|
| 49 |
+
from stable_baselines3.common.evaluation import evaluate_policy
|
| 50 |
+
from pyvirtualdisplay import Display
|
| 51 |
+
|
| 52 |
+
# 📌 Configurar una pantalla virtual (para renderizar el entorno en Google Colab)
|
| 53 |
+
os.system('Xvfb :1 -screen 0 1024x768x24 &')
|
| 54 |
+
os.environ['DISPLAY'] = ':1'
|
| 55 |
+
|
| 56 |
+
print("✅ Dependencias instaladas y pantalla virtual configurada.")
|
| 57 |
+
|
| 58 |
+
# 📌 Paso 3: Forzar el uso de LunarLander-v2 (Evitar el error de Gymnasium)
|
| 59 |
+
try:
|
| 60 |
+
env = make_vec_env("LunarLander-v2", n_envs=16)
|
| 61 |
+
print("✅ Entorno vectorizado creado correctamente.")
|
| 62 |
+
except:
|
| 63 |
+
print("⚠️ Error con LunarLander-v2, intentando con compatibilidad.")
|
| 64 |
+
import shimmy
|
| 65 |
+
env = make_vec_env("LunarLander-v2", n_envs=16, render_mode="rgb_array")
|
| 66 |
+
|
| 67 |
+
print("✅ Entorno configurado correctamente.")
|
| 68 |
+
|
| 69 |
+
# 📌 Paso 4: Definir y Entrenar el Modelo PPO
|
| 70 |
+
model = PPO(
|
| 71 |
+
policy="MlpPolicy",
|
| 72 |
+
env=env,
|
| 73 |
+
n_steps=1024,
|
| 74 |
+
batch_size=64,
|
| 75 |
+
n_epochs=4,
|
| 76 |
+
gamma=0.999,
|
| 77 |
+
gae_lambda=0.98,
|
| 78 |
+
ent_coef=0.01,
|
| 79 |
+
verbose=1
|
| 80 |
+
)
|
| 81 |
+
print("✅ Modelo PPO definido correctamente.")
|
| 82 |
+
|
| 83 |
+
# 🏋️♂️ Entrenamiento
|
| 84 |
+
print("🏋️♂️ Entrenando el modelo... esto tomará tiempo ☕")
|
| 85 |
+
model.learn(total_timesteps=1000000)
|
| 86 |
+
print("✅ Entrenamiento completado.")
|
| 87 |
+
|
| 88 |
+
# 📌 Paso 5: Guardar el Modelo
|
| 89 |
+
model_name = "ppo-LunarLander-v2"
|
| 90 |
+
model.save(model_name)
|
| 91 |
+
print(f"✅ Modelo guardado como {model_name}.zip")
|
| 92 |
+
|
| 93 |
+
# 📌 Descargar el modelo a tu PC
|
| 94 |
+
from google.colab import files
|
| 95 |
+
files.download(f"{model_name}.zip")
|
| 96 |
+
|
| 97 |
+
# 📌 Paso 6: Evaluar el Modelo
|
| 98 |
+
eval_env = Monitor(gym.make("LunarLander-v2")) # Asegurar compatibilidad
|
| 99 |
+
mean_reward, std_reward = evaluate_policy(model, eval_env, n_eval_episodes=10, deterministic=True)
|
| 100 |
+
print(f"📊 Evaluación completada: mean_reward = {mean_reward:.2f} +/- {std_reward:.2f}")
|
| 101 |
+
|
| 102 |
+
# 📌 Validar si pasamos la certificación
|
| 103 |
+
if mean_reward - std_reward >= 200:
|
| 104 |
+
print("🎉 ¡Modelo aprobado para la certificación de Hugging Face! 🚀")
|
| 105 |
+
else:
|
| 106 |
+
print("⚠️ No alcanzaste el mínimo de 200, intenta entrenar más tiempo.")
|
| 107 |
+
|
| 108 |
+
print("✅ Todo el proceso ha finalizado correctamente.")
|
| 109 |
|
| 110 |
...
|
| 111 |
```
|