I have this code bellow as a backend of my jupyter notebook when I called the model:
imports
import numpy as np
import pandas as pd
from scipy import stats
Sklearn
from sklearn.metrics import r2_score
from ML.ml_utils import *
from sklearn.model_selection import train_test_split
FNN
import tensorflow as tf
import keras_tuner as kt
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, BatchNormalization
from tensorflow.keras.optimizers import SGD, Adam
from tensorflow.keras.callbacks import EarlyStopping
from keras_tuner import HyperParameters
from keras_tuner import Objective
from keras_tuner.tuners import GridSearch, RandomSearch
from your_module import create_model
def model_predict(self, data):
if self.reg_class == "regression":
if self.model_id == "FNN":
data_features = data.features
else:
'Prediction error'
if self.model_loaded is not None:
y_prediction = self.model.predict(data_features)
else:
y_prediction = self.model.model.predict(data_features)
labels = self.data.labels
predictions = pd.DataFrame(list(zip(data.cid, labels, y_prediction)),
columns=["Cid", "Experimental", "Predicted"])
predictions['Target ID'] = data.target[0]
predictions['Algorithm'] = self.model_id
predictions['Residuals'] = [label_i - prediction_i for label_i, prediction_i in zip(labels, y_prediction)]
return labels, y_prediction, predictions
def prediction_performance(self, data, nantozero=False) -> pd.DataFrame:
if self.reg_class == "regression":
labels = self.labels
pred = self.y_pred
fill = 0 if nantozero else np.nan
if len(pred) == 0:
mae = fill
mse = fill
rmse = fill
r2 = fill
r = fill
else:
mae = tf.keras.metrics.mean_absolute_error(labels, pred).numpy().tolist()
mse = tf.keras.metrics.mean_squared_error(labels, pred).numpy().tolist()
rmse = np.sqrt(mse)
target = data.target[0]
model_name = self.model_id
#Calculate r and r2
self.labels1 = self.labels.reshape(-1, 1)
self.y_pred1 = self.y_pred.reshape(-1, 1)
correlation_matrix = np.corrcoef(self.labels1, self.y_pred1, rowvar=False)
correlation_xy = correlation_matrix[0,1]
r = correlation_xy**2
r2 = r2_score(self.labels, self.y_pred)
result_list = [{"MAE": mae,
"MSE": mse,
"RMSE": rmse,
"R2": r2,
"r": r,
"Dataset size": len(labels),
"Target ID": target,
"Algorithm": model_name}
]
# Prepare result dataset
results = pd.DataFrame(result_list)
results.set_index(["Target ID", "Algorithm", "Dataset size"], inplace=True)
results.columns = pd.MultiIndex.from_product([["Value"], ["MAE", "MSE", "RMSE", "R2", "r"]],
names=["Value", "Metric"])
results = results.stack().reset_index().set_index("Target ID")
return results
I don't know why, but the code run without the hyperparameter search and in search_space_sumary() appers only this:
Search space summary
Default search space size: 0
Anyone can help me how to correct my code? I think is something related to the hp=kt.HyperParameters, but I tried all ways possible for me and didn't have any difference.
I have this code bellow as a backend of my jupyter notebook when I called the model:
imports
import numpy as np import pandas as pd from scipy import stats
Sklearn
from sklearn.metrics import r2_score from ML.ml_utils import * from sklearn.model_selection import train_test_split
FNN
import tensorflow as tf import keras_tuner as kt from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, Dropout, BatchNormalization from tensorflow.keras.optimizers import SGD, Adam from tensorflow.keras.callbacks import EarlyStopping from keras_tuner import HyperParameters from keras_tuner import Objective from keras_tuner.tuners import GridSearch, RandomSearch from your_module import create_model
class FeedForwardNN(tf.keras.Model): def init(self, input_dim=None, random_seed=42): super(FeedForwardNN, self).init() self.seed = random_seed input_dim = 2048 self.input_dim = input_dim self.model = self.build_model()
class MLModel: def init(self, data, ml_algorithm, reg_class="regression", cv_fold=10, random_seed=42):
class Model_Evaluation: def init(self, model, data, model_id=None, model_loaded=None, reg_class="regression"): self.reg_class = reg_class self.model_id = model_id self.model = model self.data = data self.model_loaded = model_loaded self.labels, self.y_pred, self.predictions = self.model_predict(data) self.pred_performance = self.prediction_performance(data)
I don't know why, but the code run without the hyperparameter search and in search_space_sumary() appers only this: Search space summary Default search space size: 0 Anyone can help me how to correct my code? I think is something related to the hp=kt.HyperParameters, but I tried all ways possible for me and didn't have any difference.