我不能用scikeras来执行cross_瓦尔_score,

dxpyg8gm  于 2023-01-02  发布在  其他
关注(0)|答案(1)|浏览(150)
from tensorflow import keras
from sklearn.model_selection import cross_val_score
from sklearn.datasets import make_regression
from scikeras.wrappers import KerasRegressor
X, y = make_regression(n_samples=10_000)
input_shape = X.shape[1]
model = keras.Sequential([
    keras.layers.Dense(100, activation='relu', input_dim=input_shape),
    keras.layers.Dense(200, activation='relu'),
    keras.layers.Dense(200, activation='relu'),
    keras.layers.Dense(1, activation='linear')])
model.compile(keras.optimizers.Adam(), loss='mse')

model = KerasRegressor(model, batch_size=256, verbose=1, epochs=10)

val_score = cross_val_score(model, X, y, cv=5)
plt.plot(val_score)

当我运行附加代码正常它应该工作,但由于某种原因,它显示这个错误:
-————————————————————————————————————————————————————————————————————————————————————————————-
Empty Traceback (most recent call last) /usr/local/lib/python3.8/dist-packages/joblib/parallel.py in dispatch_one_batch(self, iterator) 861 try: --> 862 tasks = self.ready_batches.get(block=False) 863 except queue.Empty:
13帧空帧:
在处理上述异常的过程中,发生了另一个异常:
属性错误跟踪(最近的调用最后一次)/usr/local/lib/python3.8/dist-packages/keras/optimizers/optimizer_v2/optimizer_v2.py位于
* getattribute (self,name)864 """已覆盖以支持超参数访问。""" 865尝试:- -〉866返回super(优化器V2,自身)。 获取属性 (名称)867,但属性错误为e:868 #需要使用 setattr *_来避免无限递归。
属性错误:"Adam"对象没有"build"属性

fnx2tebb

fnx2tebb1#

TensorFlow 2.11)确保您正在执行以下操作:

from tensorflow import keras

import kerasfrom tensorflow import keras之间存在差异:

>>> import keras
>>> keras.optimizers.Adam.build
AttributeError: type object 'Adam' has no attribute 'build'

>>> from tensorflow import keras
>>> keras.optimizers.Adam.build
<function Adam.build at 0x7f1ff29e7b50>

tensorflow 2.9
Package 在get_model函数中的样板文件似乎可以解决这个问题:

from tensorflow import keras
from sklearn.model_selection import cross_val_score
from sklearn.datasets import make_regression
from scikeras.wrappers import KerasRegressor
X, y = make_regression(n_samples=10_000)

def get_model(meta):
  X_shape_ = meta["X_shape_"]
  model = keras.Sequential()
  model.add(keras.layers.Dense(100, activation='relu', input_shape=X_shape_[1:]))
  model.add(keras.layers.Dense(200, activation='relu'))
  model.add(keras.layers.Dense(200, activation='relu'))
  model.add(keras.layers.Dense(1, activation='linear'))
  return model

model = KerasRegressor(model=get_model, loss="mse", batch_size=256, verbose=1, epochs=10)

cross_val_score(model, X, y, cv=5)

相关问题