MLP Rendimiento β PyTorch :8080
RegresiΓ³n supervisada tabular: deporte, sueno -> rendimiento (600 filas). Modelo MLP 2-32-16-1, ReLU, MSELoss, AdamW.
1. RegresiΓ³n supervisada tabular
flowchart TD
CSV["π Rendimiento.csv
600 filas
deporte 0-5h | sueno 3-9h
rendimiento 52-284"]
DEF["π― Tipo: Regresion supervisada tabular
X 2 numericos -> y continua
NO encoder | NO refuerzo"]
SPLIT["βοΈ Split 70/15/15
train 420 | val 90 | test 90
stratified NO, shuffle SI"]
SCALER["βοΈ StandardScaler
fit solo en train
z = x - mean / std"]
TRAIN["ποΈ Train: X_train, y_train"]
VAL["π Val: tuning + EarlyStopping"]
TEST["π§ͺ Test: MAE RMSE R2 final"]
CSV --> DEF
DEF --> SPLIT
SPLIT --> SCALER
SCALER --> TRAIN
SCALER --> VAL
SCALER --> TEST
NOENC["β Encoder = compresion/embeddings
para texto/secuencias, NO 2 columnas"]
NORL["β Refuerzo = agente + recompensa
sin y etiquetada, NO aplica"]
DEF -.-> NOENC
DEF -.-> NORL
2. Arquitectura MLP PyTorch (641 params)
flowchart TD
IN["π’ Input 2
deporte_norm, sueno_norm
Tensor float32 shape B,2"]
L1["π΅ Linear 2->32
96 params: 2x32+32
nn.Linear + Kaiming init"]
R1["π‘ ReLU 1
ReLU x = max 0,x
32 activaciones"]
L2["π΅ Linear 32->16
528 params: 32x16+16
capa oculta 2"]
R2["π‘ ReLU 2
ReLU x = max 0,x
16 activaciones"]
OUT["π΄ Output Linear 16->1
17 params: 16x1+1
SIN activacion, y continua"]
TOT["π¦ Total 641 params
96+528+17
MLP regresion PyTorch"]
LOSS["π Loss MSELoss
mean pred-real al cuadrado
reporte MAE RMSE R2"]
OPT["βοΈ Optim AdamW
lr 1e-3, wd 1e-4
ReduceLROnPlateau"]
IN --> L1
L1 --> R1
R1 --> L2
L2 --> R2
R2 --> OUT
OUT --> TOT
TOT --> LOSS
LOSS --> OPT
NOTE["π‘ Sin ReLU colapsa a lineal
Final lineal porque rendimiento 50-284 no se recorta"]
OUT -.-> NOTE
| Capa | Tipo | Neuronas | Params |
| Input | Tensor norm. | 2 | 0 |
| Oculta 1 | Linear + ReLU | 32 | 96 |
| Oculta 2 | Linear + ReLU | 16 | 528 |
| Output | Linear | 1 | 17 |
3. Entrenamiento y evaluaciΓ³n
flowchart TD
DL["π₯ DataLoader batch 32
TensorDataset X_norm, y
shuffle train SI"]
FWD["β‘οΈ Forward
pred = MLP X
2->32 ReLU ->16 ReLU ->1"]
MSE["π MSELoss train
loss = mean pred-real 2
gradiente prop al error"]
BWD["β¬
οΈ Backward + AdamW step
loss.backward + optimizer.step
zero_grad cada batch"]
VALM["π Val cada epoch
MAE puntos | RMSE | R2
SmoothL1 opcional si picos"]
SCHED["β³ Scheduler + EarlyStopping
ReduceLROnPlateau patience 20
stop patience 50, max 500 epochs"]
TESTM["π§ͺ Test final
MAE RMSE R2 + scatter
real vs pred + curva loss"]
SAVE["πΎ Guardar model.pt + scaler.pt
para predecir nuevos casos"]
BASE["π Baseline Ridge lineal
R2 aprox 0.92 esperado
MLP debe igualar o superar"]
DL --> FWD
FWD --> MSE
MSE --> BWD
BWD --> VALM
VALM --> SCHED
SCHED -->|mejora| DL
SCHED -->|converge| TESTM
TESTM --> SAVE
TESTM --> BASE
Decisiones
- Loss: MSE para entrenar (suave, penaliza picos), MAE/RMSE/R2 para reportar.
- Optimizador: AdamW lr=1e-3, wd=1e-4. Adaptativo, sin tunear scheduler manual como SGD.
- ReLU:
max(0,x), evita vanishing de sigmoid/tanh, barato.
4. Arquitectura API
flowchart TD
CLI["π€ Cliente
POST predict | GET health metrics
JSON por HTTP :8000"]
API["π FastAPI api/main.py
uvicorn main:app --port 8000
schemas.py valida pydantic"]
EP_PRED["β‘οΈ POST /predict
body deporte 0-5, sueno 3-9
resp rendimiento 200 | 422"]
EP_HEALTH["π GET /health
status ok + model_loaded
liveness del servicio"]
EP_METRICS["π GET /metrics
MAE 4.33 RMSE 5.22 R2 0.9884
params 641 arch 2-32-16-1"]
MOD["π¦ models solo lectura
best_model.pt 5.6KB
scaler.pt 2.3KB"]
MLP["π§ MLPRegressor 2-32-16-1
cargado 1 vez en startup
eval + no_grad en inferencia"]
NOTRAIN["β training fuera de inferencia
train.py dataset.py NO se importan
API nunca re-entrena"]
CLI --> API
API --> EP_PRED
API --> EP_HEALTH
API --> EP_METRICS
EP_PRED --> MOD
MOD --> MLP
MLP --> EP_PRED
API -.-> NOTRAIN
5. Flujo POST /predict
flowchart TD
REQ["π₯ POST /predict
deporte 3.0, sueno 6.0
JSON 2 campos float"]
VAL["β
Valida pydantic
deporte 0-5 | sueno 3-9
tipo float requerido"]
ERR["β 422 Unprocessable
fuera de rango o tipo mal
ej deporte -1 o sueno texto"]
NORM["βοΈ Normaliza
z = x - mean / std
con scaler.pt de train"]
FWD["π§ Forward MLP
2-32 ReLU -16 ReLU -1
torch.no_grad, CPU"]
DENORM["π€ Desnormaliza
y = pred_zt * y_std + y_mean
rendimiento continuo"]
RESP["π¦ 200 OK
rendimiento 171.9
ej 1h/4h 89.3 | 4.5h/8h 249.4"]
REQ --> VAL
VAL -->|ok| NORM
VAL -->|falla| ERR
NORM --> FWD
FWD --> DENORM
DENORM --> RESP
6. Health, metrics y despliegue
flowchart TD
BOOT["π Startup uvicorn
main:app puerto 8000
1 solo worker CPU"]
LOAD["π Carga modelo 1 vez
load_state_dict best_model.pt
map_location cpu + eval"]
FLAG["π© model_loaded bool
true si .pt ok
false si falta archivo"]
H["π GET /health
status ok + model_loaded
200 siempre si API viva"]
M["π GET /metrics fijo
MAE 4.33 RMSE 5.22 R2 0.9884
de training.evaluate en test"]
SEQ["π Secuencia por request
cliente -API -modelo
sin recargar .pt nunca"]
BOOT --> LOAD
LOAD --> FLAG
FLAG --> H
FLAG --> M
H --> SEQ
M --> SEQ
NOTE["π‘ metrics NO recalcula
son constantes del test 90 filas
predict usa no_grad siempre"]
M -.-> NOTE
Archivos