I am running the following error when I try to optimize a LSTM using keras tuner in Python: AttributeError: module ‘keras.src.activations’ has no attribute ‘get’.
I am using the following versions: | Python version: 3.11.7 | Keras version: 3.4.1 | TensorFlow version: 2.16.2 | Keras Tuner version: 1.0.5
#LOADING REQUIRED PACKAGES
import pandas as pd
import math
import keras
import numpy as np
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import save_model
from tensorflow.keras.models import model_from_json
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import LSTM
from tensorflow.keras.layers import Dropout
from kerastuner.tuners import RandomSearch
from kerastuner.engine.hyperparameters import HyperParameters
#GENERATING SAMPLE DATA
# Creating x_train_data with 300 observations and 10 columns
x_train_data = pd.DataFrame(np.random.rand(300, 10))
# Creating y_train_data with 300 observations and 1 column
y_train_data = pd.DataFrame(np.random.rand(300, 1))
# Creating x_test_data with 10 observations and 10 columns
x_test_data = pd.DataFrame(np.random.rand(10, 10))
# Creating y_test_data with 10 observations and 1 column
y_test_data = pd.DataFrame(np.random.rand(10, 1))
#RESHAPING DATA
nrow_xtrain, ncol_xtrain = x_train_data.shape
x_train_data_lstm = x_train_data.reshape(1,nrow_xtrain, ncol_xtrain)
nrow_ytrain= y_train_data.shape[0]
y_train_data_lstm = y_train_data.reshape(1,nrow_ytrain,1)
nrow_ytest= y_test.shape[0]
y_test_data_lstm = y_test.reshape(1,nrow_ytest,1)
nrow_xtest, ncol_xtest = X_test.shape
x_test_data_lstm = X_test.reshape(1,nrow_xtest, ncol_xtest)
#BUILDING AND ESTIMATING MODEL
def build_model(hp):
model = Sequential()
model.add(LSTM(hp.Int('input_unit',min_value=1,max_value=512,step=32),return_sequences=True, input_shape=(x_train_data_lstm.shape[1],x_train_data_lstm.shape[2])))
for i in range(hp.Int('n_layers', 1, 4)):
model.add(LSTM(hp.Int(f'lstm_{i}_units',min_value=1,max_value=512,step=32),return_sequences=True))
model.add(LSTM(hp.Int('layer_2_neurons',min_value=1,max_value=512,step=32)))
model.add(Dropout(hp.Float('Dropout_rate',min_value=0,max_value=0.5,step=0.1)))
model.add(Dense(10, activation=hp.Choice('dense_activation',values=['relu', 'sigmoid',"linear"],default='relu')))
model.compile(loss='mean_squared_error', optimizer='adam',metrics = ['mse'])
return model
tuner= RandomSearch(
build_model,
objective='mse',
max_trials=2,
executions_per_trial=1
)
tuner.search(
x=X_train,
y=Y_train,
epochs=20,
batch_size=128,
validation_data=(x_test_data_lstm,y_test_data_lstm),
)
Full Error Traceback:
C:WorkspacePython_RuntimeEnvsbbkLibsite-packageskerassrclayersrnnrnn.py:204: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.
super().__init__(**kwargs)
Traceback (most recent call last):
Cell In[82], line 12
tuner= RandomSearch(
File C:WorkspacePython_RuntimeEnvsbbkLibsite-packageskeras_tunersrctunersrandomsearch.py:174 in __init__
super().__init__(oracle, hypermodel, **kwargs)
File C:WorkspacePython_RuntimeEnvsbbkLibsite-packageskeras_tunersrcenginetuner.py:122 in __init__
super().__init__(
File C:WorkspacePython_RuntimeEnvsbbkLibsite-packageskeras_tunersrcenginebase_tuner.py:132 in __init__
self._populate_initial_space()
File C:WorkspacePython_RuntimeEnvsbbkLibsite-packageskeras_tunersrcenginebase_tuner.py:192 in _populate_initial_space
self._activate_all_conditions()
File C:WorkspacePython_RuntimeEnvsbbkLibsite-packageskeras_tunersrcenginebase_tuner.py:149 in _activate_all_conditions
self.hypermodel.build(hp)
Cell In[82], line 8 in build_model
model.add(Dense(10, activation=hp.Choice('dense_activation',values=['relu', 'sigmoid',"linear"],default='relu')))
File C:WorkspacePython_RuntimeEnvsbbkLibsite-packageskerassrclayerscoredense.py:89 in __init__
self.activation = activations.get(activation)
AttributeError: module 'keras.src.activations' has no attribute 'get'
UPDATE
After a restart of the kernel, suddenly the code worked and the error did not pop up anymore. However, in order to understand the error, I will still leave the question online.
2