ValueError:未知优化器:优化器
Posted
技术标签:
【中文标题】ValueError:未知优化器:优化器【英文标题】:ValueError: Unknown optimizer: optimizer 【发布时间】:2020-11-15 02:50:54 【问题描述】:我想进行超参数调整,所以我应用了 gridsearchCV,但在拟合它的过程中,得到了 ValueError
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import GridSearchCV
def build_classifier(optimizer):
ann = tf.keras.models.Sequential()
ann.add(tf.keras.layers.Dense(units = 6, activation = 'relu'))
ann.add(tf.keras.layers.Dense(units = 6, activation = 'relu'))
ann.add(tf.keras.layers.Dense(units = 1, activation = 'sigmoid')) #softmax in case of more than 2 classes
ann.compile(optimizer = 'optimizer', loss = 'binary_crossentropy', metrics = ['accuracy']) #categorical_crossentropy in case of categories > 2
return ann
ann = KerasClassifier(build_fn = build_classifier)
parameters = 'batch_size': [25,32],
'epochs' : [10,100],
'optimizer' : ['adam', 'rmsprop']
grid_search = GridSearchCV(estimator = ann,
param_grid = parameters,
scoring = 'accuracy',
cv = 10)
grid_search = grid_search.fit(X_train, y_train)
【问题讨论】:
【参考方案1】:而不是将'optimizer'
字符串传递给compile()
,而是传递您的函数参数optimizer
。
import tensorflow as tf
from sklearn.model_selection import GridSearchCV
def build_classifier(optimizer):
ann = tf.keras.models.Sequential()
ann.add(tf.keras.layers.Dense(units = 6, activation = 'relu'))
ann.add(tf.keras.layers.Dense(units = 6, activation = 'relu'))
ann.add(tf.keras.layers.Dense(units = 1, activation = 'sigmoid'))
ann.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy'])
return ann
ann = tf.keras.wrappers.scikit_learn.KerasClassifier(build_fn = build_classifier)
parameters = 'batch_size': [25,32],
'epochs': [10, 100],
'optimizer': ['Adam', 'RMSprop']
grid_search = GridSearchCV(estimator=ann,
param_grid=parameters,
scoring= 'accuracy',
cv=10)
grid_search = grid_search.fit(X, y)
【讨论】:
是的程序正在运行,但是epochs没有停止并且一次又一次地自我重启,我该如何完成它? 你正在运行 100 个 epoch 尝试你的代码小 epoch 看它是否运行,然后运行更长的 epoch。以上是关于ValueError:未知优化器:优化器的主要内容,如果未能解决你的问题,请参考以下文章
Anaconda:ValueError:无法解释优化器标识符
ValueError:您在优化器 RMSprop 上调用了“set_weights(weights)”,权重列表长度为 3,但优化器期望权重为 0