pythontensorflowmachine-learningtensorflow-federated

Find the true and predicted labels in Tensorflow Federated


I am having a multi-class classification problem and trying to evaluate the federated learning model by analyzing the True and Predicted values and producing the classification report.

But I am stuck with the y_true and y_pred, I don't know how to extract them for the federated computation. The block of my federated model training:

  for round_num in range(0, NUM_ROUNDS):
    train_metrics = eval_process(state.model, test_data)['eval']
    state, _= iterative_process.next(state, train_data)

    print(f'Round {round_num:3d}: {train_metrics}')
    data_frame = data_frame.append({'Round': round_num,
                                      **train_metrics}, ignore_index=True)
  

  test_metrics = eval_process(state.model, test_data)
  print("The final evaluation is: ")
  print(test_metrics)

  return data_frame
  

The classification report I want to reach to:

from sklearn.metrics import classification_report

y_pred = model.predict(x_test, batch_size=64, verbose=1)
y_pred_bool = np.argmax(y_pred, axis=1)

print(classification_report(y_test, y_pred_bool))

Any help will be so much appreciated. Thanks


Solution

  • While I'm not an expert in sklearn, it looks like the classification report computes things like precision and recall. You could simply add those relevant metrics to your model (for example, through tf.keras if you're using a Keras model) and standard training/eval processes in TFF (eg. tff.learning.build_federated_evaluation) will compute them and aggregate them across clients.