I have been trying to tune my model hyperparameteres through Keras Tuner, but after trials, i get this error:
Windows fatal exception: access violation
Main thread:
Current thread 0x00009004 (most recent call first):
File "C:\Users\ldsp_\miniconda3\envs\tf-gpu\lib\site-packages\tensorflow\python\ops\gen_dataset_ops.py", line 3314 in make_iterator
File "C:\Users\ldsp_\miniconda3\envs\tf-gpu\lib\site-packages\tensorflow\python\data\ops\iterator_ops.py", line 787 in _create_iterator
File "C:\Users\ldsp_\miniconda3\envs\tf-gpu\lib\site-packages\tensorflow\python\data\ops\iterator_ops.py", line 755 in __init__
File "C:\Users\ldsp_\miniconda3\envs\tf-gpu\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py", line 486 in __iter__
File "C:\Users\ldsp_\miniconda3\envs\tf-gpu\lib\site-packages\keras\engine\data_adapter.py", line 1191 in enumerate_epochs
File "C:\Users\ldsp_\miniconda3\envs\tf-gpu\lib\site-packages\keras\engine\training.py", line 1710 in evaluate
File "C:\Users\ldsp_\miniconda3\envs\tf-gpu\lib\site-packages\keras\utils\traceback_utils.py", line 64 in error_handler
File "C:\Users\ldsp_\miniconda3\envs\tf-gpu\lib\site-packages\keras\engine\training.py", line 1420 in fit
File "C:\Users\ldsp_\miniconda3\envs\tf-gpu\lib\site-packages\keras\utils\traceback_utils.py", line 64 in error_handler
File "C:\Users\ldsp_\miniconda3\envs\tf-gpu\lib\site-packages\keras_tuner\src\engine\hypermodel.py", line 149 in fit
File "C:\Users\ldsp_\miniconda3\envs\tf-gpu\lib\site-packages\keras_tuner\src\engine\tuner.py", line 233 in _build_and_fit_model
File "C:\Users\ldsp_\miniconda3\envs\tf-gpu\lib\site-packages\keras_tuner\src\engine\tuner.py", line 314 in run_trial
File "C:\Users\ldsp_\miniconda3\envs\tf-gpu\lib\site-packages\keras_tuner\src\engine\base_tuner.py", line 238 in _run_and_update_trial
File "C:\Users\ldsp_\miniconda3\envs\tf-gpu\lib\site-packages\keras_tuner\src\engine\base_tuner.py", line 273 in _try_run_and_update_trial
File "C:\Users\ldsp_\miniconda3\envs\tf-gpu\lib\site-packages\keras_tuner\src\engine\base_tuner.py", line 233 in search
File "c:\users\ldsp_\miniconda3\envs\tf-gpu\irish\lstm\lstm_m5_gs.py", line 170 in <module>
File "C:\Users\ldsp_\miniconda3\envs\tf-gpu\lib\site-packages\spyder_kernels\py3compat.py", line 356 in compat_exec
File "C:\Users\ldsp_\miniconda3\envs\tf-gpu\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 473 in exec_code
File "C:\Users\ldsp_\miniconda3\envs\tf-gpu\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 615 in _exec_file
File "C:\Users\ldsp_\miniconda3\envs\tf-gpu\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 528 in runfile
File "C:\Users\ldsp_\AppData\Local\Temp\ipykernel_26680\3951896565.py", line 1 in <module>
I am using tensorflow
2.8.0
, keras
2.8.0
and keras-tuner
1.4.6
.
Here is my code:
import time
import keras_tuner
import numpy as np
import pandas as pd
from keras.callbacks import Callback
from keras.callbacks import EarlyStopping
from keras.layers import LSTM
from keras.layers import Dense
from keras.layers import InputLayer
from keras.layers import Dropout
from keras.losses import SparseCategoricalCrossentropy
from keras.models import Sequential
from sklearn.model_selection import train_test_split
from contextlib import redirect_stdout
percentuais_amostras = [0.5]
rede = 'LSTM'
dataset = 'D4'
modelo = 'M5_GS'
for perc_amostras in percentuais_amostras:
df = pd.read_csv(f'C:/Users/ldsp_/miniconda3/envs/tf-gpu/Irish/Dataset_{dataset}/Dataset_{dataset}_{perc_amostras}.txt', sep=' ')
x = df.iloc[:, :-1].values
y = df['FLAG'].values
del df
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)
del x, y
x_train = x_train.reshape(x_train.shape[0], x_train.shape[1], 1)
# %% LSTM
def criar_modelo(hp):
classifier_LSTM = Sequential(name=f'{rede}_{modelo}_{perc_amostras}')
classifier_LSTM.add(InputLayer((x_train.shape[1], 1), name='Camada_Entrada'))
classifier_LSTM.add(LSTM(hp.Int('first_lstm', min_value=32, max_value=64, step=32),
return_sequences=True, name='1_Camada_LSTM'))
classifier_LSTM.add(LSTM(hp.Int('last_lstm', min_value=32, max_value=64, step=32),
return_sequences=False, name='2_Camada_LSTM'))
for k in range(hp.Int("num_layers", 1, 2)):
classifier_LSTM.add(Dense(units=hp.Int(f'units_{k}',
min_value=2 * len(np.unique(y_train)),
max_value=3 * len(np.unique(y_train)),
step=len(np.unique(y_train))),
activation='relu',
name=f'{k}_dense'))
if hp.Boolean("dropout"):
classifier_LSTM.add(Dropout(rate=0.2))
classifier_LSTM.add(Dense(units=len(np.unique(y_train)),
activation='softmax',
name='Camada_Saida'))
classifier_LSTM.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
return classifier_LSTM
tuner = keras_tuner.GridSearch(
hypermodel=criar_modelo,
objective='val_loss',
max_trials=33,
executions_per_trial=1,
overwrite=True,
directory=f'C:/Users/ldsp_/miniconda3/envs/tf-gpu/Irish/{rede}/Resultados {rede}_{modelo}/',
project_name='tuning')
print(tuner.search_space_summary())
# %% Callbacks
earlystop = EarlyStopping(monitor='val_loss',
min_delta=1e-4,
patience=50,
verbose=1,
mode='min',
restore_best_weights=True)
cb = [earlystop]
val_size = 0.2
tuner.search(x_train,
y_train,
batch_size=256,
epochs=300,
callbacks=cb,
validation_split=val_size,
shuffle=True)
# %% Results
trials = tuner.oracle.get_best_trials(num_trials=5)
HP_list = []
for trial in trials:
hyperparameters_config = trial.hyperparameters.get_config()["values"]
hyperparameters_config.update({"val_loss": trial.score})
HP_list.append(hyperparameters_config)
HP_df = pd.DataFrame(HP_list)
writer = pd.ExcelWriter(f'C:/Users/ldsp_/miniconda3/envs/tf-gpu/Irish/{rede}/Resultados {rede}_{modelo}/Results_GS_{perc_amostras}.xlsx',
engine='xlsxwriter')
HP_df.to_excel(writer, sheet_name='Results')
writer._save()