Can't run tf.math.confuse_matrix


I’m practicing tensorflow tutorial :Video classification with a 3D convolutional neural network, the code:

def get_actual_predicted_labels(dataset):

   actual = [labels for _, label in dataset.unbatch()]
   predicted = model.predict(dataset)
   actual = tf.stack(actual, axis=0)
   predicted = tf.concat(predicted, axis=0)
   predicted = tf.argmax(predicted, axis=1)
   return actual, predicted

fg = FrameGenerator(subset_paths[‘train’], n_frames, training=True)
labels = list(fg.class_ids_for_name.keys())

actual, predicted = get_actual_predicted_labels(train_ds)

it’s all OK on the above,

cm = tf.math.confusion_matrix(actual, predicted)

The error:
UnimplementedError: {{function_node _wrapped__Cast_device/job:localhost/replica:0/task:0/device:CPU:0}} Cast string to int64 is not supported [Op:Cast]

Has someone can help me? Thank you very much.

The error is due to TensorFlow trying to cast string labels to integers, which it can’t do. To fix it, ensure both actual and predicted labels are integers before using them in tf.math.confusion_matrix . If your actual labels are strings, map them to their corresponding integer IDs first.