kyasz
kyasz

Reputation: 11

Attribute error when running tuner.search (keras tuner)

I have an issue when I run the function tuner.search from the keras-tuner library. I am trying to tune my hyperparameters but when I call this function, it stops after 3 tries and in each try it is stopped after the first epoch.

I saw someone saying he had also an error when calling tuner.search on windows (I am on a windows machine) but it was due to the name of the file they was giving, I tried this thing but it changed nothing. I don't know what to do to use this. I also checked if the models were created well and it seems to be the case.

Here is my code :

import pandas as pd
import matplotlib.pyplot as plt

dataframe = pd.read_csv("resultats/resultats11.csv")

var = dataframe.columns.drop(['epidemic peak', 'time to extinction','nombre de noeuds', "nombre d'aretes", 'degre moyen', 'degre minimal', 'degre maximal', 'distance moyenne', 'nombre de smart honeypots', 'nombre de honeypots', 'nombre inf 5', 'nombre res 5'])
X = dataframe[var]
Y = dataframe[['epidemic peak']]

from sklearn.preprocessing import OneHotEncoder, StandardScaler
from sklearn.compose import ColumnTransformer

ct = ColumnTransformer([("PropagationType", OneHotEncoder(), [0])], remainder="passthrough", sparse_threshold=0, verbose=True)
X = ct.fit_transform(X)  ### encodage factice 
sc_x = StandardScaler()
sc_y = StandardScaler()

from sklearn.model_selection import train_test_split

x_train1, x_test, y_train1, y_test = train_test_split(X, Y, test_size=0.15, random_state=1, shuffle=True)
x_train, x_val, y_train, y_val = train_test_split(x_train1, y_train1, test_size=(0.15/0.85), random_state=12, shuffle= True)

x_train = sc_x.fit_transform(x_train)
x_val = sc_x.transform(x_val)
x_test = sc_x.transform(x_test)
y_train = sc_y.fit_transform(y_train)
y_val = sc_y.transform(y_val)
y_test = sc_y.transform(y_test)

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras import optimizers
from tensorflow.keras import metrics
import keras_tuner as kt

def create_model(neurons1, neurons2, lr): 
    model = Sequential()
    model.add(Dense(neurons1, input_dim=10, activation='relu'))
    model.add(Dense(neurons2, activation='relu'))
    model.add(Dense(1, activation='relu'))

    model.compile(optimizer=optimizers.Adam(learning_rate=lr), loss = 'mean_absolute_percentage_error', metrics=[metrics.RootMeanSquaredError()])
    return model

def build_model(hp):
    neurons1 = hp.Int("units1", min_value=32, max_value=512, step=32)
    neurons2 = hp.Int("units2", min_value=32, max_value=512, step=32)
    lr = hp.Float("learning_rate", min_value=1e-4, max_value=1e-2, sampling="log")
    model = create_model(neurons1, neurons2, lr)
    return model

import os

tuner = kt.BayesianOptimization(
    build_model,
    objective = 'val_root_mean_squared_error',
    max_trials=20,
    directory=os.path.normpath(r'C:\Users\yassi\Desktop'),
    overwrite = True)

tuner.search(x_train, y_train, validation_data=(x_val, y_val), epochs = 30, batch_size=32, shuffle=True)

The import are a bit everywhere because I use a jupyter notebook.

And here is the error I have : Traceback (most recent call last): File "c:\Users\yassi.julia\conda\3\x86_64\lib\site-packages\keras_tuner\src\engine\base_tuner.py", line 274, in _try_run_and_update_trial self._run_and_update_trial(trial, *fit_args, **fit_kwargs) File "c:\Users\yassi.julia\conda\3\x86_64\lib\site-packages\keras_tuner\src\engine\base_tuner.py", line 239, in _run_and_update_trial results = self.run_trial(trial, *fit_args, **fit_kwargs) File "c:\Users\yassi.julia\conda\3\x86_64\lib\site-packages\keras_tuner\src\engine\tuner.py", line 314, in run_trial obj_value = self._build_and_fit_model(trial, *args, **copied_kwargs) File "c:\Users\yassi.julia\conda\3\x86_64\lib\site-packages\keras_tuner\src\engine\tuner.py", line 233, in _build_and_fit_model results = self.hypermodel.fit(hp, model, *args, **kwargs) File "c:\Users\yassi.julia\conda\3\x86_64\lib\site-packages\keras_tuner\src\engine\hypermodel.py", line 149, in fit return model.fit(*args, **kwargs) File "c:\Users\yassi.julia\conda\3\x86_64\lib\site-packages\keras\src\utils\traceback_utils.py", line 122, in error_handler raise e.with_traceback(filtered_tb) from None File "c:\Users\yassi.julia\conda\3\x86_64\lib\site-packages\keras_tuner\src\engine\tuner_utils.py", line 76, in on_epoch_end self._save_model() File "c:\Users\yassi.julia\conda\3\x86_64\lib\site-packages\keras_tuner\src\engine\tuner_utils.py", line 86, in _save_model self.model.save_weights(write_filepath) AttributeError: 'NoneType' object has no attribute 'File'

Does anyone have an idea of how to solve this issue or have already met this issue ?

By the way I am using keras_tuner 1.4.7 and tensorflow 2.16.1.

Upvotes: 1

Views: 153

Answers (1)

Des95
Des95

Reputation: 1

I had the same issue. The code is functional and I could excecute it in google colab. I checked the versions of python, keras, tensorflow and keras-tuner and I changed them accordingly: tensorflow version: 2.15.0 Keras version 2.15.0 keras-tuner version 1.4.7 With this versions the error is solved.

Upvotes: 0

Related Questions