0

I was trying to created a simple model, wrapped in keras wrapper. When I test it with simple input, I get the TypeError: object of type 'NoneType' has no len(). I was using scikeras wrapper.

Also, the error is because of the wrapper, not exatly sure why though, when I remove the wrapper the above code works fine.

The input features are coming from pipelines, after one hot encoding and hence prefer to avoid specifying the input size if there is a chance. The model is working if not wrapped in the regressor, but not sure why it is not working in the wrapper.

Can someone please explain how to fix this ?

from tensorflow.keras.models import Sequential
from scikeras.wrappers import KerasRegressor

def build_net(net_loss = 'mean_squared_error', net_optimizer='adam',):

    model = Sequential()
    model.add(Dense(25, activation='relu'))
    model.add(Dense(9, activation='relu'))
    model.add(Dense(4, activation='relu'))
    model.add(Dense(1, activation='linear'))
    model.compile(loss=net_loss, optimizer=net_optimizer, metrics=['root_mean_squared_error','mean_absolute_error'])

    return model

net = KerasRegressor(model=build_net, epochs = 5, verbose=0)

net.fit(np.random.randint(50, size=(2000, 4)), np.random.randint(1, size=2000))
tjt
  • 620
  • 2
  • 7
  • 17

1 Answers1

0

You need to add an input shape or layer, for example

model = Sequential()
model.add(Input(shape=(4,)))
model.add(Dense(25, activation='relu'))
...

or like this

model = Sequential()
model.add(Dense(25, input_shape=(4,), activation='relu'))
model.add(Dense(9, activation='relu'))
...
AndrzejO
  • 1,502
  • 1
  • 9
  • 12
  • Thank you, but would it be possible to skip the input size ? Since the model is part of a sklearn pipeline and there are are other transformers and one hot encoding columns that impact the input size. I can get the number of columns, but wondering if this can be avoided. Also, the error is because of the wrapper, when I remove the wrapper the above code works fine. – tjt Sep 21 '22 at 19:31
  • You could declare only the channel number, like `input_shape=(None, 3)` or `input_shape=(None, None, 3)`. You could use conv layers, but before using a Dense layer, you would need to have a GlobalAveragePooling layer or something similar – AndrzejO Sep 21 '22 at 20:07