AttributeError: 'Sequential' object has no attribute compiled

Hi, I am trying to run this piece of code. I am getting the error ‘AttributeError: ‘Sequential’ object has no attribute ‘compiled’’. I am using TensorFlow version 2.14.0, Keras version 2.14.0, and SciKeras version 0.13.0.

import numpy as np
import tensorflow as tf
from sklearn.model_selection import cross_val_score
from keras.models import Sequential
from scikeras.wrappers import KerasRegressor
from math import floor
from sklearn.metrics import make_scorer, mean_squared_error
from sklearn.model_selection import StratifiedKFold, KFold
from keras.callbacks import EarlyStopping, ModelCheckpoint
from keras.layers import Dense
score_acc = make_scorer(mean_squared_error)
X_train = np.random.randn(10,3)
y_train = np.random.randn(10,1)
def nn_cl_fun():
nn = Sequential()
nn.add(Dense(5, input_dim=3, activation=‘relu’))
nn.add(Dense(5, activation=‘relu’))
nn.add(Dense(1))
nn.compile(loss=‘mean_squared_error’, optimizer=‘Adam’, metrics=[‘mean_squared_error’])
return nn
es = EarlyStopping(monitor=‘mean_squared_error’, mode=‘max’, verbose=0, patience=20)
nn1 = KerasRegressor(model=nn_cl_fun, epochs=10, verbose=0)
kfold1 = KFold(n_splits=2, shuffle=True, random_state=123)
score = cross_val_score(nn1, X_train, y_train, scoring=score_acc, error_score=‘raise’, fit_params={‘callbacks’:[es]}).mean()

Hi @Alok_Kumar_Samantara ,

I am able to run the above code without any issues. Please find the colab gist which ran successfully with above versions. I modified the code with minimal changes like double quotes instead of wrong type single quotes you mentioned in the hidden layers.

Make sure while installing packages, check the compatibility versions with tf-keras and Tensorflow. This is small code doesn’t make any difference but if you run big models it will have impact.

Thanks.

Hi, I tried to run the colab. But, I am getting the same error there also.
The error is: /usr/local/lib/python3.10/dist-packages/scikeras/wrappers.py in _ensure_compiled_model(self)
437 def ensure_compiled_model(self) → None:
438 # compile model if user gave us an un-compiled model
→ 439 if not self.model
.compiled:
440 kw = self.get_compile_kwargs()
441 self.model
.compile(**kw)

AttributeError: ‘Sequential’ object has no attribute ‘compiled’

Like @Mayur_Laxmanrao, I pasted your code in some random Colab that was opened on my screen, and it is working.

import numpy as np

import tensorflow as tf

from sklearn.model_selection import cross_val_score

from keras.models import Sequential

from scikeras.wrappers import KerasRegressor

from math import floor

from sklearn.metrics import make_scorer, mean_squared_error

from sklearn.model_selection import StratifiedKFold, KFold

from keras.callbacks import EarlyStopping, ModelCheckpoint

from keras.layers import Dense

score_acc = make_scorer(mean_squared_error)

X_train = np.random.randn(10,3)

y_train = np.random.randn(10,1)

def nn_cl_fun():

nn = Sequential()

nn.add(Dense(5, input_dim=3, activation=‘relu’))

nn.add(Dense(5, activation=‘relu’))

nn.add(Dense(1))

nn.compile(loss=‘mean_squared_error’, optimizer=‘Adam’, metrics=[‘mean_squared_error’])

return nn

es = EarlyStopping(monitor=‘mean_squared_error’, mode=‘max’, verbose=0, patience=20)

nn1 = KerasRegressor(model=nn_cl_fun, epochs=10, verbose=0)

kfold1 = KFold(n_splits=2, shuffle=True, random_state=123)

score = cross_val_score(nn1, X_train, y_train, scoring=score_acc, error_score=‘raise’, fit_params={‘callbacks’:[es]}).mean()