Tensorflow model not training when using functional API, but training when using equivalent Sequential model

38 views Asked by At

I have the Sequential model

def model():
    model = keras.Sequential([
        layers.Dense(64, activation="relu"),
        layers.Dense(64, activation="relu"),
        layers.Dense(1)
    ])
    model.compile(optimizer="rmsprop", loss="mse", metrics=["mae"])
    return model
model_train = model()

And the functional API model

inputs = keras.Input(shape=X_train.shape)
x = inputs
x = layers.Dense(64, activation="relu")(x)
x = layers.Dense(64, activation="relu")(x)
outputs = layers.Dense(1)(x)

model = keras.Model(inputs=inputs, outputs=outputs)

The Sequential model runs perfectly when I use the following code: history = model_train.fit(X_train, Y_train, validation_data=(X_val, Y_val), epochs=60, batch_size=32)

But I can't train the functional API model when I run

adam = Adam(lr=0.001)
model.compile(optimizer=adam, loss='mse', metrics=['accuracy'])

history = model.fit(X_train, Y_train, batch_size=16, epochs=60)

I get the following error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[19], line 1
----> 1 history = model.fit(train_players, train_set, batch_size=16, epochs=60)

File ~/.local/lib/python3.10/site-packages/keras/utils/traceback_utils.py:70, in filter_traceback..error_handler(*args, **kwargs)
     67     filtered_tb = _process_traceback_frames(e.__traceback__)
     68     # To get the full stack trace, call:
     69     # `tf.debugging.disable_traceback_filtering()`
---> 70     raise e.with_traceback(filtered_tb) from None
     71 finally:
     72     del filtered_tb

File ~/.local/lib/python3.10/site-packages/keras/engine/input_spec.py:298, in assert_input_compatibility(input_spec, inputs, layer_name)
    296 if spec_dim is not None and dim is not None:
    297     if spec_dim != dim:
--> 298         raise ValueError(
    299             f'Input {input_index} of layer "{layer_name}" is '
    300             "incompatible with the layer: "
    301             f"expected shape={spec.shape}, "
    302             f"found shape={display_shape(x.shape)}"
    303         )

ValueError: Input 0 of layer "model" is incompatible with the layer: expected shape=(None, 5761, 2, 1), found shape=(16, 2, 1)

(Note: X_train.shape=(5761, 2, 1), and that is why it is present in the error message.)

I suspect this has something to do with the way the data is being batched, but not matter how I try to change the batching, it will not train.

To fix the issue, I have tried augmenting the batching and the shape of X_train, such that it fits with the expected value. Neither have worked. What really confuses me in this, is that the shape of the input to the model isn't what is being found in the error message.

I have never encounted this error before, so I hope there is someone willing to help. Thanks in advance

0

There are 0 answers