Predict_on_batch function of keras.RandomForestModel throws AssertionError

Hello, I am having trouble getting a prediction for set of accelerometer data from predict_on_batch function of keras.RandomForestModel.

This is the line on which exception occurs:

#test prediction for an example of data input
print(model.predict_on_batch(np.array([1.008, 1.015, 0.017, 1.177, 0.878, 0.916, 1.384, 0.77, 0.77, 0.207, 0.207, 0.081], dtype=np.float32).reshape((1,12))))

This is the stack trace:
AssertionError Traceback (most recent call last)
in ()
56
57 #test prediction for an example of data input
—> 58 print(model.predict_on_batch(np.array([1.008, 1.015, 0.017, 1.177, 0.878, 0.916, 1.384, 0.77, 0.77, 0.207, 0.207, 0.081], dtype=np.float32).reshape((1,12))))

2 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
1145 except Exception as e: # pylint:disable=broad-except
1146 if hasattr(e, “ag_error_metadata”):
→ 1147 raise e.ag_error_metadata.to_exception(e)
1148 else:
1149 raise

AssertionError: in user code:

File "/usr/local/lib/python3.7/dist-packages/tensorflow_decision_forests/keras/core.py", line 621, in run_step  *
    outputs = model.predict_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1751, in predict_step  **
    return self(x, training=False)
File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
    raise e.with_traceback(filtered_tb) from None

AssertionError: Exception encountered when calling layer "random_forest_model_11" (type RandomForestModel).

in user code:

    File "/usr/local/lib/python3.7/dist-packages/tensorflow_decision_forests/keras/core.py", line 791, in call  *
        normalized_inputs = self._build_normalized_inputs(inputs)
    File "/usr/local/lib/python3.7/dist-packages/tensorflow_decision_forests/keras/core.py", line 747, in _build_normalized_inputs  *
        assert len(self._semantics) == 1

    AssertionError: 


Call arguments received:
  • inputs=tf.Tensor(shape=(1, 12), dtype=float32)
  • training=False

Does anyone know how I can solve this error? I have been looking for a solution, but haven’t found it yet.

Hi Ivan,

This error is not very informative and is improved in the next TF-DF release.

Essentially, the error indicates that the input of “predict_on_batch” (an array) does not match the input provided to the model during training (e.g. a nested structure of tensors?). Can you share details about the training code?

I am taking training data from a csv file. Columns in that file are as follows:
myMove mean rms sd meanFiX meanFiY meanFiZ rmsFiX rmsFiY rmsFiZ sdFiX sdFiY sdFiZ

Part of code that transforms this data into tensorflow dataset is:

label = "myMove"

# Split the dataset into a training and a testing dataset.
def split_dataset(dataset, test_ratio=0.30):
  """Splits a panda dataframe in two."""
  test_indices = np.random.rand(len(dataset)) < test_ratio
  return dataset[~test_indices], dataset[test_indices]

train_ds_pd, test_ds_pd = split_dataset(dataset_df)

train_ds = tfdf.keras.pd_dataframe_to_tf_dataset(train_ds_pd, label=label)
test_ds = tfdf.keras.pd_dataframe_to_tf_dataset(test_ds_pd, label=label)

pd_dataframe_to_tf_dataset exports features as a dictionary of tensors.
Therefore, the argument of predict/predict_step should also be a dictionary of tensors/arrays (not a single array).

For example, try model.predict(test_ds)

This helped! Thank you!

There was a mismatch between the wanted and given data.

Amazing Content on this website. I Like it. Thanks for sharing.

I guess you didn’t want your generator to produce only 1 pixel since your discriminator takes 100 inputs. The error came from the fact that your first model outputs a tensor with shape (batch_size, 1) and your second model input takes a input shape of (batch_size, 100). Hence the assert error. It’s compiling on my laptop now.