0
$\begingroup$

I am building artificial neuron network (ANN) model for predicting values but facing problem:

Input:

def create_model(optimizer = 'rmsprop', units = 16, learning_rate = 0.001):

ann = tf.keras.Sequential() # Initialising ANN
ann.add(tf.keras.layers.Dense(units = units, activation = "relu")) # Adding First Hidden Layer
ann.add(tf.keras.layers.Dense(units = units, activation = "relu")) # Adding Second Hidden Layer
ann.add(tf.keras.layers.Dense(units = Y.shape[1], activation = 'softmax')) # Adding Output Layer

###############################################
# Add optimizer with learning rate
if optimizer == 'rmsprop':
    opt = tf.keras.optimizers.RMSprop(learning_rate = learning_rate)
elif optimizer == 'adam':
    opt = tf.keras.optimizers.Adam(learning_rate = learning_rate)
elif optimizer == 'SGD':
    opt = tf.keras.optimizers.SGD(learning_rate = learning_rate)
else:
    raise ValueError('optimizer {} unrecognized'.format(optimizer))
############################################## 

ann.compile(optimizer = optimizer, loss = 'categorical_crossentropy',  metrics = ['accuracy']) # Compiling ANN
return ann

ann = KerasClassifier(model = create_model, 
                     verbose = 2, 
                     learning_rate = 0.001, 
                     units = 16
                     )

optimizers = ['rmsprop', 'adam', 'SGD']
epoch_values = [10, 25, 50, 100, 150, 200]
batches = [10, 20, 30, 40, 50, 100, 1000]
units = [16, 32, 64, 128, 256]
lr_values = [0.001, 0.01, 0.1, 0.2, 0.3]

hyperparameters = dict(optimizer = optimizers, 
                        epochs = epoch_values, 
                        batch_size = batches, 
                        units = units,
                        learning_rate = lr_values
                        )

grid = GridSearchCV(estimator = ann, cv = 5, param_grid = hyperparameters)

history = grid.fit(X_train, 
                 Y_train, 
                 batch_size = 32, 
                 validation_data = (X_test, Y_test), 
                 epochs = 100
                 ) # Fitting ANN

Output error:

    File c:\Users\dis\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\model_selection\_search.py:875, in BaseSearchCV.fit(self, X, y, groups, **fit_params)
    869     results = self._format_results(
    870         all_candidate_params, n_splits, all_out, all_more_results
    871     )
    873     return results
--> 875 self._run_search(evaluate_candidates)
    877 # multimetric is determined here because in the case of a callable
    878 # self.scoring the return type is only known after calling
...
    self._check_model_compatibility(y)
  File "c:\Users\dis\AppData\Local\Programs\Python\Python310\lib\site-packages\scikeras\wrappers.py", line 551, in _check_model_compatibility
    if self.n_outputs_expected_ != len(self.model_.outputs):
TypeError: object of type 'NoneType' has no len()

Data:

  • X.shape -> (10, 2066)
  • Y.shape -> (10, 4)
  • X_train.shape -> (8, 2066)
  • X_test.shape -> (2, 2066)
  • Y_train.shape -> (8, 4)
  • Y_test.shape -> (2, 4)
$\endgroup$
1
  • $\begingroup$ try and print "self.model_.outputs" and you will see the problem. $\endgroup$
    – Dinu Mihai
    Aug 31 at 13:38

1 Answer 1

1
$\begingroup$

When you use Sequential model in tf.keras you need to provide the input_shape in the first layer or to add the input layer.

Modify your code as follows:

ann = tf.keras.Sequential() # Initialising ANN
ann.add(tf.keras.layers.Dense(units = units, input_shape=(X.shape[0],), activation = "relu")) # Adding First Hidden Layer
ann.add(tf.keras.layers.Dense(units = units, activation = "relu")) # Adding Second Hidden Layer
ann.add(tf.keras.layers.Dense(units = Y.shape[1], activation = 'softmax')) # Adding Output Layer

or adding an input layer as follows:

ann = tf.keras.Sequential() # Initialising ANN
ann.add(tf.keras.layers.Input(shape=(X.shape[0],))) # Input Layer
ann.add(tf.keras.layers.Dense(units = units, activation = "relu")) # Adding First Hidden Layer
ann.add(tf.keras.layers.Dense(units = units, activation = "relu")) # Adding Second Hidden Layer
ann.add(tf.keras.layers.Dense(units = Y.shape[1], activation = 'softmax')) # Adding Output Layer
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.