Home > Software design >  Python (Scikeras) - ValueError: Invalid parameter layers for estimator KerasClassifier
Python (Scikeras) - ValueError: Invalid parameter layers for estimator KerasClassifier

Time:05-26

I am trying to create a convolutional neural network using GridSearchCV and the scikeras wrapper, but I have been receiving an error that I cannot figure out the cause of.

The core of the error is this:

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()

Please find the full error after the code. I have tried changing a few line, or adding different parameters, however nothing seems to change the error I am receiving. Here is the code:

# 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))

The main section that seems to be the cause of the error is:

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))

From the message I am receiving, I am thinking there is something incorrect within the model, and the layers being established. Any help on narrowing down the cause would be greatly appreciated. I am still unfamiliar with a lot of machine learning.

Thanks in advance.

Full Error Message:

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()`

CodePudding user response:

It works now after making the change stated in the error. The code is changed from:

# 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)

To

# 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)

Although there is now another issue, 'layers' is no longer defined for 'param_grid = dict(layers = layers)', but the model still produces results despite this.

  • Related