2022-08-23

TF WideDeepModel - Shape Error when Passing Different Features for Wide and Deep Models

I am attempting to recreate the Wide and Deep model using Tensorflow's WideDeepModel library; however, I am encountering an issue when attempting to differentiate between the wide model inputs and the deep model inputs. Referenced below is the code that I am using.

# Create LinearModel and DNN Model as in Examples 1 and 2
  optimizer = tf.keras.optimizers.Ftrl(
          l1_regularization_strength=0.001,
          learning_rate=tf.keras.optimizers.schedules.ExponentialDecay(
              initial_learning_rate=0.1, decay_steps=10000, decay_rate=0.9))

  linear_model = tf.compat.v1.keras.experimental.LinearModel()
  linear_model.compile(loss='mse', optimizer=optimizer, metrics=['accuracy'])
  linear_model.fit(X_train[wideInputs], y_train, epochs=50)

  dnn_model = tf.keras.models.Sequential([
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(1)
  ])
  dnn_model.compile(loss='mse', optimizer=optimizer, metrics=['accuracy'])

  combined_model = tf.compat.v1.keras.experimental.WideDeepModel(linear_model,
                                                                dnn_model)
  combined_model.compile(
      optimizer=[optimizer, optimizer], loss='mse', metrics=['accuracy'])
  combined_model.fit([X_train[wideInputs], X_train[wideInputs] + X_train[dnnInputs]], y_train, epochs=50)
  print('Accuracy', combined_model.evaluate(X_test, y_test, return_dict=True))

My goal is to fit the combined_model variable with the linear inputs only in the first model (wide) and the deep inputs (both wide and deep inputs); however, I encounter an error with mismatching shapes between the two inputs. I assume that the number of rows need to remain the same but the features can vary between the two models as we are defining two separate set of features that should be used. However, when I use a different set of features, I am returned with the following error:

ValueError: Exception encountered when calling layer "linear_model" (type LinearModel).

    Input 0 of layer "dense" is incompatible with the layer: expected axis -1 of input shape to have value 3004, but received input with shape (None, 3009)

    Call arguments received by layer "linear_model" (type LinearModel):
      • inputs=tf.Tensor(shape=(None, 3009), dtype=float32)

Any feedback would be greatly appreciated.

Just to note, when I do not differentiate the features (fit using combined_model.fit([X_train, X_train], y_train, epochs=50)), the model runs; however, it is not using the expected wide and deep inputs this way.

I also referenced the following pages to work with the code.

  1. https://www.tensorflow.org/guide/migrate/canned_estimators#tf2_using_keras_widedeepmodel
  2. https://www.tensorflow.org/api_docs/python/tf/keras/experimental/WideDeepModel

Additionally, the dataset that I am passing in looks like the following: https://i.stack.imgur.com/h1ae1.png



No comments:

Post a Comment