-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmachine_error.py
More file actions
76 lines (56 loc) · 2.03 KB
/
Copy pathmachine_error.py
File metadata and controls
76 lines (56 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import time
import numpy as np
import logging
import time
start = time.time()
from sklearn.linear_model import SGDClassifier
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, log_loss
# ---------------- LOGGER ----------------
logging.basicConfig(
level=logging.INFO,
format='[%(levelname)s] %(message)s'
)
logger = logging.getLogger(__name__)
logger.info("Corriendo machine_MAL.py...")
logger.info("Iniciando entrenamiento...")
# ---------------- DATA ----------------
X, y = make_classification(
n_samples=200000,
n_features=20,
random_state=42
)
X_train, X_val, y_train, y_val = train_test_split(
X, y, test_size=0.2, random_state=42
)
# ---------------- MODEL ----------------
model = SGDClassifier(loss="log_loss", random_state=42)
epochs = 60
for epoch in range(epochs):
logger.info(f"Época {epoch+1}/{epochs}")
# chequeo de datos antes de entrenar
if np.isnan(X_train).any() or np.isinf(X_train).any():
logger.error("Dataset corrupto detectado (NaN/Inf). Deteniendo entrenamiento.")
break
try:
model.partial_fit(X_train, y_train, classes=np.unique(y))
train_acc = accuracy_score(y_train, model.predict(X_train))
val_acc = accuracy_score(y_val, model.predict(X_val))
train_loss = log_loss(y_train, model.predict_proba(X_train))
val_loss = log_loss(y_val, model.predict_proba(X_val))
logger.info(
f"Train acc: {train_acc:.4f} | loss: {train_loss:.4f} || "
f"Val acc: {val_acc:.4f} | loss: {val_loss:.4f}"
)
except Exception as e:
logger.error(f"Error crítico en época {epoch+1}: {e}")
break # aquí se detiene el entrenamiento
# simulación de fallo
if epoch == 30:
logger.error("Corrupción de datos introducida")
X_train[0:1000] = np.nan
time.sleep(0.5)
end = time.time()
logger.info(f"Tiempo total: {round(end - start, 2)} segundos")
logger.info("Proceso terminado")