Search code examples
pythontensorflowmachine-learningpredict

Creating a manual confusion matrix by using model.predict() output


I have a model already working and have a loop for it's training. You can see the loop here:

for idx, (x, y) in enumerate(train_dataset):
  pred = model.predict_on_batch(x)
  print(model.test_on_batch(x, pred, reset_metrics=False, return_dict=True))
  print(model.train_on_batch(x, y, reset_metrics=False))
  print(f"After {idx} entries")

I would like to create a custom confusion matrix and be able to compute model's accuracy from that matrix too. When running this block of code:

for x, y in train_dataset.take(10):
  print(model.predict(x))

I get this as output:

[[-0.00407019]]
[[-0.01000004]]
[[-0.00080154]]
[[-0.0159038]]
[[-0.00301645]]
[[-0.0147643]]
[[-0.00481013]]
[[-0.00032348]]
[[-0.00791026]]
[[-0.00795541]]

How can I compare this solution output from model.predict() to 0 or 1 to dedicate what I should add to the confusion matrix.

I guess I have to create something like that if I'm able to output 0 or 1 from model.predict()

if model.predict(x) == 0:
    foo()
if model.predict(x) == 1:
    foo1()

You can see the model here:

```python
model = tf.keras.Sequential([
    encoder,
    tf.keras.layers.Embedding(
        input_dim=len(encoder.get_vocabulary()),
        output_dim=64,
        # Use masking to handle the variable sequence lengths
        mask_zero=True),
    tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64)),
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dense(1)
])

Thank you in advance!


Solution

  • The (main) problem with your model is the activation function of the last layer. There is none, hence the output is passed linearly.

    Modify the final layer as

    tf.keras.layers.Dense(1, activation='sigmoid')
    

    Then by

    for x, y in train_dataset.take(10):
      print(model.predict(x))
    

    You will get a 10-dimensional array, corresponding to the probability (always between 0 and 1) for each of the examples to be aggressive or not.

    Then to assign a "class" C to each example you set a threshold a, such that if model.predict(x) > a => assign x to C.

    If your threshold is 0.5 (as usually is) you can achieve what you want by

    if round(model.predict(x)) == 0:
        foo()
    if round(model.predict(x)) == 1:
        foo1()
    

    Note: You are doing the prediction over training examples, this makes sense from the computational point of view (these are legit inputs, hence a prediction is calculated correctly), however, you maybe want to predict new data, thus on the test set.