You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Keras Tuner优化LSTM时遇AttributeError: activations无get属性

问题:AttributeError: module 'keras.src.activations' has no attribute 'get'

版本信息

依赖库版本号
Python3.11.7
Keras3.4.1
TensorFlow2.16.2
Keras Tuner1.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),
)

错误回溯

C:\Workspace\Python_Runtime\Envs\bbk\Lib\site-packages\keras\src\layers\rnn\rnn.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:\Workspace\Python_Runtime\Envs\bbk\Lib\site-packages\keras_tuner\src\tuners\randomsearch.py:174 in __init__
    super().__init__(oracle, hypermodel, **kwargs)

  File C:\Workspace\Python_Runtime\Envs\bbk\Lib\site-packages\keras_tuner\src\engine\tuner.py:122 in __init__
    super().__init__(

  File C:\Workspace\Python_Runtime\Envs\bbk\Lib\site-packages\keras_tuner\src\engine\base_tuner.py:132 in __init__
    self._populate_initial_space()

  File C:\Workspace\Python_Runtime\Envs\bbk\Lib\site-packages\keras_tuner\src\engine\base_tuner.py:192 in _populate_initial_space
    self._activate_all_conditions()

  File C:\Workspace\Python_Runtime\Envs\bbk\Lib\site-packages\keras_tuner\src\engine\base_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:\Workspace\Python_Runtime\Envs\bbk\Lib\site-packages\keras\src\layers\core\dense.py:89 in __init__
    self.activation = activations.get(activation)

AttributeError: module 'keras.src.activations' has no attribute 'get'

情况更新

重启内核后代码正常运行,错误不再出现,希望了解该错误的根本原因。

错误原因分析

  • Keras 3.x API变更:Keras 3.x中已移除activations.get()方法,改为直接识别激活函数字符串或接收激活函数对象。而Keras Tuner 1.0.5是基于旧版Keras API开发的,在初始化时预构建模型获取超参数空间的过程中,触发了旧API调用,导致兼容性错误。
  • 模块加载冲突:运行时同时导入了keras和tensorflow.keras,导致激活函数模块的引用路径混乱,重启内核后模块重新加载,路径恢复正确,临时解决了冲突。
  • 缓存或状态残留:内核运行过程中积累的某些缓存或异常状态,干扰了Keras模块的正常加载,重启后清除了这些残留状态,使得代码可以正常执行。

内容的提问来源于stack exchange,提问作者Joe94

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 18:54:54