plot_metrics.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # Copyright (c) Meta Platforms, Inc. and affiliates.
  2. # This software may be used and distributed according to the terms of the Llama 2 Community License Agreement.
  3. import json
  4. import matplotlib.pyplot as plt
  5. import argparse
  6. import os
  7. def plot_metric(data, metric_name, x_label, y_label, title, colors):
  8. plt.figure(figsize=(7, 6))
  9. plt.plot(data[f'train_epoch_{metric_name}'], label=f'Train Epoch {metric_name.capitalize()}', color=colors[0])
  10. plt.plot(data[f'val_epoch_{metric_name}'], label=f'Validation Epoch {metric_name.capitalize()}', color=colors[1])
  11. plt.xlabel(x_label)
  12. plt.ylabel(y_label)
  13. plt.title(f'Train and Validation Epoch {title}')
  14. plt.legend()
  15. plt.tight_layout()
  16. def plot_single_metric_by_step(data, metric_name, x_label, y_label, title, color):
  17. plt.plot(data[f'{metric_name}'], label=f'{title}', color=color)
  18. plt.xlabel(x_label)
  19. plt.ylabel(y_label)
  20. plt.title(title)
  21. plt.legend()
  22. plt.tight_layout()
  23. def plot_metrics_by_step(data, metric_name, x_label, y_label, colors):
  24. plt.figure(figsize=(14, 6))
  25. plt.subplot(1, 2, 1)
  26. plot_single_metric_by_step(data, f'train_step_{metric_name}', x_label, y_label, f'Train Step {metric_name.capitalize()}', colors[0])
  27. plt.subplot(1, 2, 2)
  28. plot_single_metric_by_step(data, f'val_step_{metric_name}', x_label, y_label, f'Validation Step {metric_name.capitalize()}', colors[1])
  29. plt.tight_layout()
  30. def plot_metrics(file_path):
  31. if not os.path.exists(file_path):
  32. print(f"File {file_path} does not exist.")
  33. return
  34. with open(file_path, 'r') as f:
  35. try:
  36. data = json.load(f)
  37. except json.JSONDecodeError:
  38. print("Invalid JSON file.")
  39. return
  40. directory = os.path.dirname(file_path)
  41. filename_prefix = os.path.basename(file_path).split('.')[0]
  42. plot_metric(data, 'loss', 'Epoch', 'Loss', 'Loss', ['b', 'r'])
  43. plt.savefig(os.path.join(directory, f"{filename_prefix}_train_and_validation_loss.png"))
  44. plt.close()
  45. plot_metric(data, 'perplexity', 'Epoch', 'Perplexity', 'Perplexity', ['g', 'm'])
  46. plt.savefig(os.path.join(directory, f"{filename_prefix}_train_and_validation_perplexity.png"))
  47. plt.close()
  48. plot_metrics_by_step(data, 'loss', 'Step', 'Loss', ['b', 'r'])
  49. plt.savefig(os.path.join(directory, f"{filename_prefix}_train_and_validation_loss_by_step.png"))
  50. plt.close()
  51. plot_metrics_by_step(data, 'perplexity', 'Step', 'Loss', ['g', 'm'])
  52. plt.savefig(os.path.join(directory, f"{filename_prefix}_train_and_validation_perplexity_by_step.png"))
  53. plt.close()
  54. if __name__ == "__main__":
  55. parser = argparse.ArgumentParser(description='Plot metrics from JSON file.')
  56. parser.add_argument('--file_path', required=True, type=str, help='Path to the metrics JSON file.')
  57. args = parser.parse_args()
  58. plot_metrics(args.file_path)