本文整理汇总了Python中keras.wrappers.scikit_learn.KerasClassifier方法的典型用法代码示例。如果您正苦于以下问题:Python scikit_learn.KerasClassifier方法的具体用法?Python scikit_learn.KerasClassifier怎么用?Python scikit_learn.KerasClassifier使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.wrappers.scikit_learn
的用法示例。
在下文中一共展示了scikit_learn.KerasClassifier方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: load_pipeline_keras
# 需要导入模块: from keras.wrappers import scikit_learn [as 别名]
# 或者: from keras.wrappers.scikit_learn import KerasClassifier [as 别名]
def load_pipeline_keras() -> Pipeline:
"""Load a Keras Pipeline from disk."""
dataset = joblib.load(config.PIPELINE_PATH)
build_model = lambda: load_model(config.MODEL_PATH)
classifier = KerasClassifier(build_fn=build_model,
batch_size=config.BATCH_SIZE,
validation_split=10,
epochs=config.EPOCHS,
verbose=2,
callbacks=m.callbacks_list,
# image_size = config.IMAGE_SIZE
)
classifier.classes_ = joblib.load(config.CLASSES_PATH)
classifier.model = build_model()
return Pipeline([
('dataset', dataset),
('cnn_model', classifier)
])
示例2: create_keras_model
# 需要导入模块: from keras.wrappers import scikit_learn [as 别名]
# 或者: from keras.wrappers.scikit_learn import KerasClassifier [as 别名]
def create_keras_model():
"""
This function compiles and returns a Keras model.
Should be passed to KerasClassifier in the Keras scikit-learn API.
"""
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adadelta', metrics=['accuracy'])
return model
# create the classifier
示例3: execute
# 需要导入模块: from keras.wrappers import scikit_learn [as 别名]
# 或者: from keras.wrappers.scikit_learn import KerasClassifier [as 别名]
def execute():
train_df, holdout_df = prep_data()
env = Environment(
train_dataset=train_df,
results_path="HyperparameterHunterAssets",
metrics=["roc_auc_score"],
target_column=[f"target_{_}" for _ in range(10)], # 10 classes (one-hot-encoded output)
holdout_dataset=holdout_df,
cv_type="StratifiedKFold",
cv_params=dict(n_splits=3, shuffle=True, random_state=True),
)
exp = CVExperiment(KerasClassifier, build_fn_exp, dict(batch_size=64, epochs=10, verbose=1))
opt = BayesianOptPro(iterations=10, random_state=32)
opt.forge_experiment(KerasClassifier, build_fn_opt, dict(batch_size=64, epochs=10, verbose=0))
opt.go()
示例4: test_classify_build_fn
# 需要导入模块: from keras.wrappers import scikit_learn [as 别名]
# 或者: from keras.wrappers.scikit_learn import KerasClassifier [as 别名]
def test_classify_build_fn():
clf = KerasClassifier(
build_fn=build_fn_clf, hidden_dims=hidden_dims,
batch_size=batch_size, epochs=epochs)
assert_classification_works(clf)
assert_string_classification_works(clf)
示例5: test_classify_class_build_fn
# 需要导入模块: from keras.wrappers import scikit_learn [as 别名]
# 或者: from keras.wrappers.scikit_learn import KerasClassifier [as 别名]
def test_classify_class_build_fn():
class ClassBuildFnClf(object):
def __call__(self, hidden_dims):
return build_fn_clf(hidden_dims)
clf = KerasClassifier(
build_fn=ClassBuildFnClf(), hidden_dims=hidden_dims,
batch_size=batch_size, epochs=epochs)
assert_classification_works(clf)
assert_string_classification_works(clf)
示例6: test_classify_inherit_class_build_fn
# 需要导入模块: from keras.wrappers import scikit_learn [as 别名]
# 或者: from keras.wrappers.scikit_learn import KerasClassifier [as 别名]
def test_classify_inherit_class_build_fn():
class InheritClassBuildFnClf(KerasClassifier):
def __call__(self, hidden_dims):
return build_fn_clf(hidden_dims)
clf = InheritClassBuildFnClf(
build_fn=None, hidden_dims=hidden_dims,
batch_size=batch_size, epochs=epochs)
assert_classification_works(clf)
assert_string_classification_works(clf)
示例7: make_pipeline
# 需要导入模块: from keras.wrappers import scikit_learn [as 别名]
# 或者: from keras.wrappers.scikit_learn import KerasClassifier [as 别名]
def make_pipeline(**kw):
# In the case of this Iris dataset, our targets are string labels,
# and KerasClassifier doesn't like that. So we transform the
# targets into a one-hot encoding instead using PipeLineY.
return PipelineY([
('clf', KerasClassifier(build_fn=keras_model, **kw)),
],
y_transformer=LabelBinarizer(),
predict_use_inverse=False,
)
示例8: _execute
# 需要导入模块: from keras.wrappers import scikit_learn [as 别名]
# 或者: from keras.wrappers.scikit_learn import KerasClassifier [as 别名]
def _execute():
env = Environment(
train_dataset=prep_data(),
results_path="HyperparameterHunterAssets",
metrics=["roc_auc_score"],
target_column=[f"target_{_}" for _ in range(10)],
cv_type="StratifiedKFold",
cv_params=dict(n_splits=10, shuffle=True, random_state=True),
)
experiment = CVExperiment(
model_initializer=KerasClassifier,
model_init_params=build_fn,
model_extra_params=dict(batch_size=32, epochs=10, verbose=0, shuffle=True),
)
示例9: execute
# 需要导入模块: from keras.wrappers import scikit_learn [as 别名]
# 或者: from keras.wrappers.scikit_learn import KerasClassifier [as 别名]
def execute():
env = Environment(
train_dataset=get_breast_cancer_data(),
results_path="HyperparameterHunterAssets",
target_column="diagnosis",
metrics=["roc_auc_score"],
cv_type="StratifiedKFold",
cv_params=dict(n_splits=5, shuffle=True, random_state=32),
)
experiment = CVExperiment(
model_initializer=KerasClassifier,
model_init_params=build_fn,
model_extra_params=dict(
callbacks=[
ModelCheckpoint(
filepath=os.path.abspath("foo_checkpoint"), save_best_only=True, verbose=1
),
ReduceLROnPlateau(patience=5),
],
batch_size=32,
epochs=10,
verbose=0,
shuffle=True,
),
)
示例10: _execute
# 需要导入模块: from keras.wrappers import scikit_learn [as 别名]
# 或者: from keras.wrappers.scikit_learn import KerasClassifier [as 别名]
def _execute():
env = Environment(
train_dataset=prep_data(),
results_path="HyperparameterHunterAssets",
metrics=["roc_auc_score"],
cv_type="StratifiedKFold",
cv_params=dict(n_splits=3, shuffle=True, random_state=True),
)
experiment = CVExperiment(
model_initializer=KerasClassifier,
model_init_params=build_fn,
model_extra_params=dict(batch_size=32, epochs=3, verbose=0, shuffle=True),
)
示例11: _execute
# 需要导入模块: from keras.wrappers import scikit_learn [as 别名]
# 或者: from keras.wrappers.scikit_learn import KerasClassifier [as 别名]
def _execute():
#################### Environment ####################
env = Environment(
train_dataset=get_breast_cancer_data(target="target"),
results_path="HyperparameterHunterAssets",
metrics=["roc_auc_score"],
cv_type="StratifiedKFold",
cv_params=dict(n_splits=5, shuffle=True, random_state=32),
)
#################### Experimentation ####################
experiment = CVExperiment(
model_initializer=KerasClassifier,
model_init_params=dict(build_fn=_build_fn_experiment),
model_extra_params=dict(
callbacks=[ReduceLROnPlateau(patience=5)], batch_size=32, epochs=10, verbose=0
),
)
#################### Optimization ####################
optimizer = BayesianOptPro(iterations=10)
optimizer.forge_experiment(
model_initializer=KerasClassifier,
model_init_params=dict(build_fn=_build_fn_optimization),
model_extra_params=dict(
callbacks=[ReduceLROnPlateau(patience=Integer(5, 10))],
batch_size=Categorical([32, 64], transform="onehot"),
epochs=10,
verbose=0,
),
)
optimizer.go()
示例12: run_initialization_matching_optimization_0
# 需要导入模块: from keras.wrappers import scikit_learn [as 别名]
# 或者: from keras.wrappers.scikit_learn import KerasClassifier [as 别名]
def run_initialization_matching_optimization_0(build_fn):
optimizer = DummyOptPro(iterations=1)
optimizer.forge_experiment(
model_initializer=KerasClassifier,
model_init_params=dict(build_fn=build_fn),
model_extra_params=dict(epochs=1, batch_size=128, verbose=0),
)
optimizer.go()
return optimizer
#################### `glorot_normal` (`VarianceScaling`) ####################
示例13: fit
# 需要导入模块: from keras.wrappers import scikit_learn [as 别名]
# 或者: from keras.wrappers.scikit_learn import KerasClassifier [as 别名]
def fit(self, X, y):
if self.multi_class:
self.n_classes_ = len(set(y))
else:
self.n_classes_ = 1
build_callable = lambda: self.build_function(X.shape[1], self.n_classes_)
keras_params=copy(self.keras_params)
keras_params['build_fn']=build_callable
self.classifier_ = KerasClassifier(**keras_params)
self.classifier_.fit(X, y)