ERROR:The first argument to `Layer.call` must always be passed

Issue

I am using version 2.5.0.

I defined my sequence of layers for a regression problem as a function, to be wrapped using the KerasRegressor class. This was done to allow me to perform a RandomizedSearchCV.

import os
import tensorflow as tf
import tensorflow_addons as tfa
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense, Dropout
from tensorflow.keras.layers import Input, InputLayer
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, TensorBoard
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import RandomizedSearchCV

def build_model(n_hidden = 1, n_neurons = 32, input_shape = (X_train.shape[1],), dropout = 0):
  model = Sequential()
  model.add(InputLayer(input_shape = input_shape))
  for i in range(n_hidden):
    model.add(Dense(n_neurons, activation = 'relu'))
    model.add(Dropout(dropout))
  model.add(Dense(1))
  model.compile(loss = 'mean_squared_error', optimizer = 'adam', metrics = [tfa.metrics.RSquare(y_shape=(1,))])
  return model

keras_reg = KerasRegressor(build_fn = build_model)

history = keras_reg.fit(X_train, y_train, epochs = 200, batch_size = 64, 
              validation_data = (X_valid, y_valid),
              callbacks = callbacks)
mse_test = keras_reg.score(X_test, y_test)


The code above works. However, when I try to define another KerasRegressor called keras_reg_A, and override the defaults for build_model, I get the error "The first argument to Layer.call must always be passed" (I left the callbacks out on purpose)

build_model_A = build_model(n_hidden = 2, n_neurons = 64, input_shape = (X_train.shape[1],), dropout = 0)
keras_reg_A = KerasRegressor(build_model_A)

history_A = keras_reg_A.fit(X_train, y_train, epochs = 200, batch_size = 64, 
              validation_data = (X_valid, y_valid))
mse_test = keras_reg_A.score(X_test, y_test)

Can someone please explain why this is?

Solution

The build_fn argument of KerasRegressor expects a function, not a tf.keras.Model. If you need to override the default parameter of build_model, you can pass them directly to the constructor of the KerasRegressor.

For example:

keras_reg = KerasRegressor(build_fn=build_model, n_hidden=2, n_neurons=32, dropout=0.5)

An other option is to define a new function, for example using a lambda:

build_model_override = lambda: build_model(n_hiddens=2)
keras_reg = KerasRegressor(build_fn=build_model_override)

Answered By – Lescurel

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published