Python (Scikeras) - ValueError: Invalid parameter layers for estimator KerasClassifier
Python (Scikeras) - ValueError: Invalid parameter layers for estimator KerasClassifier
我正在尝试使用 GridSearchCV 和 scikeras 包装器创建一个卷积神经网络,但我一直收到一个错误,我无法找出原因。
错误的核心是:
ValueError: Invalid parameter layers for estimator KerasClassifier.
This issue can likely be resolved by setting this parameter in the
KerasClassifier constructor: KerasClassifier(layers=[128])
Check the
list of available parameters with estimator.get_params().keys()
请在代码后找到完整的错误。我尝试更改几行或添加不同的参数,但似乎没有任何改变我收到的错误。这是代码:
# first model using the chosen parameters
# Part 1: Create the model
def cnn_model0(layers):
cnn = tf.keras.models.Sequential() # initialising the CNN
# model layers
cnn.add( # Step 1 - Convolution
tf.keras.layers.Conv2D(filters=32, kernel_size=3, padding="same", activation="relu", input_shape=[50, 50, 3]))
cnn.add( # Step 2 - Pooling
tf.keras.layers.MaxPool2D(pool_size=2, strides=2, padding='valid'))
cnn.add( # Second convolutional layer
tf.keras.layers.Conv2D(filters=32, kernel_size=3, padding="same", activation="relu"))
cnn.add( # Second pooling layer
tf.keras.layers.MaxPool2D(pool_size=2, strides=2, padding='valid'))
cnn.add( # Step 3 - Flattening
tf.keras.layers.Flatten())
# Step 4 - Full connection (FC)
for i, nodes in enumerate(layers):
cnn.add(tf.keras.layers.Dense(units = nodes, activation = 'relu'))
cnn.add(tf.keras.layers.Dense(units = 43, activation = 'softmax'))
# Compiling the CNN
cnn.compile(optimizer = 'Adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])
return cnn
# Part 2: Fitting the CNN model
model = KerasClassifier(build_fn = cnn_model0, verbose = 1)
# establish the grid parameters
layers = [[128], (256, 128), (200, 150, 120)]
param_grid = dict(layers = layers)
# fit GridSearchCV
grid = GridSearchCV(estimator = model, param_grid = param_grid, verbose = 1)
grid_results = grid.fit(X_train, y_train, validation_data = (X_val, y_val))
# Part 3: Printing the results
print("Best: {0}, using {1}".format(grid_results.best_score_, grid_results.best_params_))
# result values
means = grid_results.cv_results_['mean_test_score']
stds = grid_results.cv_results_['std_test_score']
params = grid_results.cv_results_['params']
for mean, stdev, param in zip(means, stds, params):
print('{0} ({1}) with: {2}'.format(mean, stdev, param))
似乎是错误原因的主要部分是:
model = KerasClassifier(build_fn = cnn_model0, verbose = 1)
layers = [[128], (256, 128), (200, 150, 120)]
param_grid = dict(layers = layers)
grid = GridSearchCV(estimator = model, param_grid = param_grid, verbose = 1)
grid_results = grid.fit(X_train, y_train, validation_data = (X_val, y_val))
从我收到的消息来看,我认为模型中存在某些不正确的地方,并且正在建立图层。任何有助于缩小原因的帮助将不胜感激。我对很多机器学习还是很陌生。
提前致谢。
完整错误信息:
Fitting 5 folds for each of 3 candidates, totalling 15 fits
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-33-b6a6389b51ee> in <module>()
35 # fit GridSearchCV
36 grid = GridSearchCV(estimator = model, param_grid = param_grid, verbose = 1)
---> 37 grid_results = grid.fit(X_train, y_train, validation_data = (X_val, y_val))
38
39 # Part 3: Printing the results
12 frames
/usr/local/lib/python3.7/dist-packages/scikeras/wrappers.py in set_params(self, **params)
1153 "\nCheck the list of available parameters with"
1154 " `estimator.get_params().keys()`"
-> 1155 ) from None
1156 return self
1157
ValueError: Invalid parameter layers for estimator KerasClassifier.
This issue can likely be resolved by setting this parameter in the KerasClassifier constructor:
`KerasClassifier(layers=[128])`
Check the list of available parameters with `estimator.get_params().keys()`
在进行错误中所述的更改后,它现在可以工作了。代码改自:
# Part 2: Fitting the CNN model
model = KerasClassifier(build_fn = cnn_model0, verbose = 1)
# establish the grid parameters
layers = [[128], (256, 128), (200, 150, 120)]
param_grid = dict(layers = layers)
到
# Part 2: Fitting the CNN model
model = KerasClassifier(build_fn = cnn_model0, verbose = 1, layers = [[128], (256, 128), (200, 150, 120)])
# establish the grid parameters
param_grid = dict(layers = layers)
虽然现在还有另一个问题,'layers' 不再为 'param_grid = dict(layers = layers)' 定义,但模型仍然会产生结果。
我正在尝试使用 GridSearchCV 和 scikeras 包装器创建一个卷积神经网络,但我一直收到一个错误,我无法找出原因。
错误的核心是:
ValueError: Invalid parameter layers for estimator KerasClassifier. This issue can likely be resolved by setting this parameter in the KerasClassifier constructor:
KerasClassifier(layers=[128])
Check the list of available parameters withestimator.get_params().keys()
请在代码后找到完整的错误。我尝试更改几行或添加不同的参数,但似乎没有任何改变我收到的错误。这是代码:
# first model using the chosen parameters
# Part 1: Create the model
def cnn_model0(layers):
cnn = tf.keras.models.Sequential() # initialising the CNN
# model layers
cnn.add( # Step 1 - Convolution
tf.keras.layers.Conv2D(filters=32, kernel_size=3, padding="same", activation="relu", input_shape=[50, 50, 3]))
cnn.add( # Step 2 - Pooling
tf.keras.layers.MaxPool2D(pool_size=2, strides=2, padding='valid'))
cnn.add( # Second convolutional layer
tf.keras.layers.Conv2D(filters=32, kernel_size=3, padding="same", activation="relu"))
cnn.add( # Second pooling layer
tf.keras.layers.MaxPool2D(pool_size=2, strides=2, padding='valid'))
cnn.add( # Step 3 - Flattening
tf.keras.layers.Flatten())
# Step 4 - Full connection (FC)
for i, nodes in enumerate(layers):
cnn.add(tf.keras.layers.Dense(units = nodes, activation = 'relu'))
cnn.add(tf.keras.layers.Dense(units = 43, activation = 'softmax'))
# Compiling the CNN
cnn.compile(optimizer = 'Adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])
return cnn
# Part 2: Fitting the CNN model
model = KerasClassifier(build_fn = cnn_model0, verbose = 1)
# establish the grid parameters
layers = [[128], (256, 128), (200, 150, 120)]
param_grid = dict(layers = layers)
# fit GridSearchCV
grid = GridSearchCV(estimator = model, param_grid = param_grid, verbose = 1)
grid_results = grid.fit(X_train, y_train, validation_data = (X_val, y_val))
# Part 3: Printing the results
print("Best: {0}, using {1}".format(grid_results.best_score_, grid_results.best_params_))
# result values
means = grid_results.cv_results_['mean_test_score']
stds = grid_results.cv_results_['std_test_score']
params = grid_results.cv_results_['params']
for mean, stdev, param in zip(means, stds, params):
print('{0} ({1}) with: {2}'.format(mean, stdev, param))
似乎是错误原因的主要部分是:
model = KerasClassifier(build_fn = cnn_model0, verbose = 1)
layers = [[128], (256, 128), (200, 150, 120)]
param_grid = dict(layers = layers)
grid = GridSearchCV(estimator = model, param_grid = param_grid, verbose = 1)
grid_results = grid.fit(X_train, y_train, validation_data = (X_val, y_val))
从我收到的消息来看,我认为模型中存在某些不正确的地方,并且正在建立图层。任何有助于缩小原因的帮助将不胜感激。我对很多机器学习还是很陌生。
提前致谢。
完整错误信息:
Fitting 5 folds for each of 3 candidates, totalling 15 fits
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-33-b6a6389b51ee> in <module>()
35 # fit GridSearchCV
36 grid = GridSearchCV(estimator = model, param_grid = param_grid, verbose = 1)
---> 37 grid_results = grid.fit(X_train, y_train, validation_data = (X_val, y_val))
38
39 # Part 3: Printing the results
12 frames
/usr/local/lib/python3.7/dist-packages/scikeras/wrappers.py in set_params(self, **params)
1153 "\nCheck the list of available parameters with"
1154 " `estimator.get_params().keys()`"
-> 1155 ) from None
1156 return self
1157
ValueError: Invalid parameter layers for estimator KerasClassifier.
This issue can likely be resolved by setting this parameter in the KerasClassifier constructor:
`KerasClassifier(layers=[128])`
Check the list of available parameters with `estimator.get_params().keys()`
在进行错误中所述的更改后,它现在可以工作了。代码改自:
# Part 2: Fitting the CNN model
model = KerasClassifier(build_fn = cnn_model0, verbose = 1)
# establish the grid parameters
layers = [[128], (256, 128), (200, 150, 120)]
param_grid = dict(layers = layers)
到
# Part 2: Fitting the CNN model
model = KerasClassifier(build_fn = cnn_model0, verbose = 1, layers = [[128], (256, 128), (200, 150, 120)])
# establish the grid parameters
param_grid = dict(layers = layers)
虽然现在还有另一个问题,'layers' 不再为 'param_grid = dict(layers = layers)' 定义,但模型仍然会产生结果。