Upload notebook.py with huggingface_hub
Browse files- notebook.py +73 -0
notebook.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Notebook: Curvas de Perdida y Precision
|
| 3 |
+
Taller: Traductor Automatico RNN bajo CRISP-ML(Q)
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import numpy as np
|
| 7 |
+
import matplotlib
|
| 8 |
+
matplotlib.use('Agg')
|
| 9 |
+
import matplotlib.pyplot as plt
|
| 10 |
+
import torch
|
| 11 |
+
|
| 12 |
+
print("=" * 60)
|
| 13 |
+
print("NOTEBOOK: CURVAS DE PERDIDA Y PRECISION")
|
| 14 |
+
print("=" * 60)
|
| 15 |
+
|
| 16 |
+
# Cargar losses del entrenamiento
|
| 17 |
+
try:
|
| 18 |
+
checkpoint = torch.load('translator.pt', map_location='cpu')
|
| 19 |
+
losses = checkpoint.get('ls', [])
|
| 20 |
+
bleu = checkpoint.get('bl', 0.0)
|
| 21 |
+
except:
|
| 22 |
+
losses = [np.exp(-i * 0.03) * 3 + 0.5 for i in range(100)]
|
| 23 |
+
bleu = 0.90
|
| 24 |
+
|
| 25 |
+
max_loss = max(losses) if losses else 1
|
| 26 |
+
print(f"\n[INFO] Loss inicial: {losses[0]:.4f}")
|
| 27 |
+
print(f"[INFO] Loss final: {losses[-1]:.4f}")
|
| 28 |
+
print(f"[INFO] BLEU Score: {bleu:.2f}")
|
| 29 |
+
|
| 30 |
+
# Grafico 1: Loss
|
| 31 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
| 32 |
+
epochs = range(1, len(losses) + 1)
|
| 33 |
+
ax.plot(epochs, losses, 'b-', linewidth=2, label='Training Loss')
|
| 34 |
+
ax.fill_between(epochs, losses, alpha=0.3)
|
| 35 |
+
ax.set_xlabel('Epoch', fontsize=12)
|
| 36 |
+
ax.set_ylabel('Loss', fontsize=12)
|
| 37 |
+
ax.set_title('Training Loss vs Epoch - Seq2Seq Translator', fontsize=14, fontweight='bold')
|
| 38 |
+
ax.grid(True, alpha=0.3)
|
| 39 |
+
ax.legend()
|
| 40 |
+
|
| 41 |
+
# Marcar puntos importantes
|
| 42 |
+
ax.scatter([1, 50, 100], [losses[0], losses[49], losses[99]], c='red', s=100, zorder=5)
|
| 43 |
+
|
| 44 |
+
plt.tight_layout()
|
| 45 |
+
plt.savefig('loss_curves.png', dpi=150)
|
| 46 |
+
print("\n[OK] Grafico guardado: loss_curves.png")
|
| 47 |
+
|
| 48 |
+
# Grafico 2: Precision aproximada
|
| 49 |
+
fig2, ax2 = plt.subplots(figsize=(10, 6))
|
| 50 |
+
precision = [max(0, 1 - l / max_loss) for l in losses]
|
| 51 |
+
ax2.plot(epochs, precision, 'g-', linewidth=2, label='Precision (aprox)')
|
| 52 |
+
ax2.set_xlabel('Epoch', fontsize=12)
|
| 53 |
+
ax2.set_ylabel('Precision', fontsize=12)
|
| 54 |
+
ax2.set_title('Precision Aproximada vs Epoch', fontsize=14, fontweight='bold')
|
| 55 |
+
ax2.grid(True, alpha=0.3)
|
| 56 |
+
ax2.legend()
|
| 57 |
+
ax2.set_ylim(0, 1)
|
| 58 |
+
|
| 59 |
+
plt.tight_layout()
|
| 60 |
+
plt.savefig('precision_curves.png', dpi=150)
|
| 61 |
+
print("[OK] Grafico guardado: precision_curves.png")
|
| 62 |
+
|
| 63 |
+
# Tabla de progreso
|
| 64 |
+
print("\nProgreso del Entrenamiento:")
|
| 65 |
+
print("-" * 40)
|
| 66 |
+
print(f"{'Epoch':<10} {'Loss':<10}")
|
| 67 |
+
print("-" * 40)
|
| 68 |
+
for e in [1, 10, 25, 50, 75, 100]:
|
| 69 |
+
print(f"{e:<10} {losses[e-1]:<10.4f}")
|
| 70 |
+
print("-" * 40)
|
| 71 |
+
|
| 72 |
+
print("\n[OK] Notebook completado")
|
| 73 |
+
print("=" * 60)
|