'KerasClassifier' 对象没有属性 'loss'
'KerasClassifier' object has no attribute 'loss'
我正在使用 keras 进行客户流失预测。我使用过 Sklearn 的列变换器。我的代码是--
import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
def keras_classifier_wrapper():
classifier = Sequential()
classifier.add(Dense(9, input_dim=13, activation='relu'))
classifier.add(Dense(8, activation='relu'))
classifier.add(Dense(1, activation='sigmoid'))
classifier.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
return clf
clf = KerasClassifier(keras_classifier_wrapper, epochs=20, batch_size=50, verbose=0)
categorical_pipe = Pipeline([
('onehot', OneHotEncoder(handle_unknown='ignore'))
])
numerical_pipe = Pipeline([
('imputer', SimpleImputer(strategy='median'))
])
preprocessing = ColumnTransformer(
[('cat', categorical_pipe, cat_var1),
('num', numerical_pipe, num_var1)])
model3 = Pipeline([
('preprocess', preprocessing),
('keras_clf', clf)
])
model3.fit(X_train, y_train)
但是显示错误-
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-162-1f0472b386ae> in <module>()
----> 1 model3.fit(X_train, y_train)
2 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/wrappers/scikit_learn.py in fit(self, x, y, **kwargs)
157 self.model = self.build_fn(**self.filter_sk_params(self.build_fn))
158
--> 159 if (losses.is_categorical_crossentropy(self.model.loss) and
160 len(y.shape) != 2):
161 y = to_categorical(y)
AttributeError: 'KerasClassifier' object has no attribute 'loss'
你能告诉我为什么会出现这个错误以及如何解决它吗?
提前致谢
问题出在你的keras_classifier_wrapper函数
def keras_classifier_wrapper():
classifier = Sequential()
classifier.add(Dense(9, input_dim=13, activation='relu'))
classifier.add(Dense(8, activation='relu'))
classifier.add(Dense(1, activation='sigmoid'))
classifier.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
return clf # should be return classifier
您正在尝试 return clf 但没有 clf 它是之后定义的。尝试 return 分类器然后它将起作用
我正在使用 keras 进行客户流失预测。我使用过 Sklearn 的列变换器。我的代码是--
import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
def keras_classifier_wrapper():
classifier = Sequential()
classifier.add(Dense(9, input_dim=13, activation='relu'))
classifier.add(Dense(8, activation='relu'))
classifier.add(Dense(1, activation='sigmoid'))
classifier.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
return clf
clf = KerasClassifier(keras_classifier_wrapper, epochs=20, batch_size=50, verbose=0)
categorical_pipe = Pipeline([
('onehot', OneHotEncoder(handle_unknown='ignore'))
])
numerical_pipe = Pipeline([
('imputer', SimpleImputer(strategy='median'))
])
preprocessing = ColumnTransformer(
[('cat', categorical_pipe, cat_var1),
('num', numerical_pipe, num_var1)])
model3 = Pipeline([
('preprocess', preprocessing),
('keras_clf', clf)
])
model3.fit(X_train, y_train)
但是显示错误-
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-162-1f0472b386ae> in <module>()
----> 1 model3.fit(X_train, y_train)
2 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/wrappers/scikit_learn.py in fit(self, x, y, **kwargs)
157 self.model = self.build_fn(**self.filter_sk_params(self.build_fn))
158
--> 159 if (losses.is_categorical_crossentropy(self.model.loss) and
160 len(y.shape) != 2):
161 y = to_categorical(y)
AttributeError: 'KerasClassifier' object has no attribute 'loss'
你能告诉我为什么会出现这个错误以及如何解决它吗?
提前致谢
问题出在你的keras_classifier_wrapper函数
def keras_classifier_wrapper():
classifier = Sequential()
classifier.add(Dense(9, input_dim=13, activation='relu'))
classifier.add(Dense(8, activation='relu'))
classifier.add(Dense(1, activation='sigmoid'))
classifier.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
return clf # should be return classifier
您正在尝试 return clf 但没有 clf 它是之后定义的。尝试 return 分类器然后它将起作用