Version 2 con funciones incluidas
Browse files- keras_model_functions.py +44 -0
keras_model_functions.py
CHANGED
@@ -57,3 +57,47 @@ def train_model(model, X_train, y_train, X_val, y_val, epochs=200, batch_size=32
|
|
57 |
)
|
58 |
|
59 |
return history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
)
|
58 |
|
59 |
return history
|
60 |
+
|
61 |
+
def add_brownian_noise(X):
|
62 |
+
brownian_noise = tf.random.normal(tf.shape(X), mean=0.0, stddev=0.1, dtype=tf.float32)
|
63 |
+
return X + brownian_noise
|
64 |
+
|
65 |
+
def evaluate_model(model, X_test, y_test):
|
66 |
+
result = model.evaluate(X_test, y_test, verbose=0)
|
67 |
+
print("P茅rdida en el conjunto de prueba:", result)
|
68 |
+
|
69 |
+
def print_model_info(model):
|
70 |
+
print("\nCaracter铆sticas del modelo:")
|
71 |
+
print("N煤mero de capas:", len(model.layers))
|
72 |
+
model.summary()
|
73 |
+
|
74 |
+
def calculate_accuracy(y_true, y_pred):
|
75 |
+
tasa_acierto = mean_absolute_error(y_test, y_pred)
|
76 |
+
print("Tasa de acierto (MAE):", tasa_acierto)
|
77 |
+
|
78 |
+
def plot_predictions(y_test, y_pred, df):
|
79 |
+
plt.figure(figsize=(10, 6))
|
80 |
+
plt.plot(y_test, label='Valor Real')
|
81 |
+
plt.plot(y_pred, label='Predicci贸n', alpha=0.7)
|
82 |
+
plt.title("Comparaci贸n de Predicciones vs. Valoreseales")
|
83 |
+
plt.xlabel("脥ndice")
|
84 |
+
plt.ylabel("Precio Escalado")
|
85 |
+
plt.legend()
|
86 |
+
plt.show()
|
87 |
+
|
88 |
+
# Hacer predicciones
|
89 |
+
train_predict = model.predict(X_train)
|
90 |
+
test_predict = model.predict(X_test)
|
91 |
+
|
92 |
+
# Invertir las predicciones a la escala original
|
93 |
+
train_predict = scaler.inverse_transform(np.hstack([train_predict, np.zeros((train_predict.shape[0], X.shape[2]-1))]))[:, 0]
|
94 |
+
test_predict = scaler.inverse_transform(np.hstack([test_predict, np.zeros((test_predict.shape[0], X.shape[2]-1))]))[:, 0]
|
95 |
+
y_train_inv = scaler.inverse_transform(np.hstack([y_train.reshape(-1, 1), np.zeros((y_train.shape[0], X.shape[2]-1))]))[:, 0]
|
96 |
+
y_test_inv = scaler.inverse_transform(np.hstack([y_test.reshape(-1, 1), np.zeros((y_test.shape[0], X.shape[2]-1))]))[:, 0]
|
97 |
+
|
98 |
+
# Calcular el error cuadr谩tico medio (RMSE)
|
99 |
+
train_rmse = np.sqrt(np.mean((train_predict - y_train_inv)**2))
|
100 |
+
test_rmse = np.sqrt(np.mean((test_predict - y_test_inv)**2))
|
101 |
+
|
102 |
+
print(f"RMSE en entrenamiento: {train_rmse}")
|
103 |
+
print(f"RMSE en prueba: {test_rmse}")
|