gpt4 book ai didi

python-3.x - 无法克隆对象
转载 作者:行者123 更新时间:2023-12-03 16:12:09 28 4
gpt4 key购买 nike

这是关于 TF 2.0。

请在我的代码下方找到使用 sklearn.model_selection.GridSearchCV 执行 GridSearch 和交叉验证的代码对于完美运行的 mnist 数据集。

# Build Function to create model, required by KerasClassifier

def create_model(optimizer_val='RMSprop',hidden_layer_size=16,activation_fn='relu',dropout_rate=0.1,regularization_fn=tf.keras.regularizers.l1(0.001),kernel_initializer_fn=tf.keras.initializers.glorot_uniform,bias_initializer_fn=tf.keras.initializers.zeros):
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(units=hidden_layer_size, activation=activation_fn,kernel_regularizer=regularization_fn,kernel_initializer=kernel_initializer_fn,bias_initializer=bias_initializer_fn),
tf.keras.layers.Dropout(dropout_rate),
tf.keras.layers.Dense(units=hidden_layer_size,activation='softmax',kernel_regularizer=regularization_fn,kernel_initializer=kernel_initializer_fn,bias_initializer=bias_initializer_fn)
])
optimizer_val_final=optimizer_val
model.compile(optimizer=optimizer_val, loss='sparse_categorical_crossentropy', metrics=['accuracy'])
return model

#Create the model with the wrapper
model = tf.keras.wrappers.scikit_learn.KerasClassifier(build_fn=create_model, epochs=100, batch_size=10, verbose=2)

#Initialize the parameter grid
nn_param_grid = {
'epochs': [10],
'batch_size':[128],
'optimizer_val': ['Adam','SGD'],
'hidden_layer_size': [128],
'activation_fn': ['relu'],
'dropout_rate': [0.2],
'regularization_fn':['l1','l2','L1L2'],
'kernel_initializer_fn':['glorot_normal', 'glorot_uniform'],
'bias_initializer_fn':[tf.keras.initializers.zeros]
}
#Perform GridSearchCV
grid = GridSearchCV(estimator=model, param_grid=nn_param_grid, verbose=2, cv=3,scoring=precision_custom,return_train_score=False,n_jobs=-1)
grid_result = grid.fit(x_train, y_train)

我的想法是通过具有不同学习率的不同优化器,比如 Adam 的学习率 0.1、0.01 和 0.001。我还想尝试使用不同学习率和动量值的 SGD。

在那种情况下,当我通过 'optimizer_val': [tf.keras.optimizers.Adam(0.1)], ,我得到如下错误:
Cannot clone object <tensorflow.python.keras.wrappers.scikit_learn.KerasClassifier object at 0x7fe08b210e10>, as the constructor either does not set or modifies parameter optimizer_val

请告知我如何纠正此错误。

最佳答案

这是sklearn错误。您应该减少 sklearn 的版本:

conda install scikit-learn==0.21.2

没关系!

关于python-3.x - 无法克隆对象 <tensorflow.python.keras.wrappers.scikit_learn.KerasClassifier 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59746974/

28 4 0

Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com