File size: 2,519 Bytes
5218aa7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | import json
import os
import matplotlib.pyplot as plt
def function_1(name: str, age: int) -> bool:
return (age >= 18) and name != "Marina"
def plot_metrics(metrics_file="outputs/training_metrics.json"):
if not os.path.exists(metrics_file):
print(f"Metrics file not found: {metrics_file}")
return
with open(metrics_file, "r") as f:
metrics_data = json.load(f)
loss_history = metrics_data.get("loss", [])
validity_history = metrics_data.get("validity", [])
uniqueness_history = metrics_data.get("uniqueness", [])
novelty_history = metrics_data.get("novelty", [])
output_dir = os.path.dirname(metrics_file)
if loss_history:
print("Saving loss curve...")
plt.figure(figsize=(10, 6))
iterations, losses = zip(*loss_history)
plt.plot(iterations, losses)
plt.xlabel("Iteration")
plt.ylabel("Loss")
plt.title("Training Loss Curve")
plt.grid(True)
plt.savefig(
os.path.join(output_dir, "loss_curve.png"), dpi=300, bbox_inches="tight"
)
plt.close()
print(f"Saved loss curve to {os.path.join(output_dir, 'loss_curve_1000.png')}")
if validity_history or uniqueness_history or novelty_history:
print("Saving metrics plot...")
plt.figure(figsize=(12, 6))
if validity_history:
iterations_v, validity_vals = zip(*validity_history)
plt.plot(
iterations_v, validity_vals, label="Validity", marker="o", markersize=3
)
if uniqueness_history:
iterations_u, uniqueness_vals = zip(*uniqueness_history)
plt.plot(
iterations_u,
uniqueness_vals,
label="Uniqueness",
marker="s",
markersize=3,
)
if novelty_history:
iterations_n, novelty_vals = zip(*novelty_history)
plt.plot(
iterations_n, novelty_vals, label="Novelty", marker="^", markersize=3
)
plt.xlabel("Iteration")
plt.ylabel("Score (%)")
plt.title("Training Metrics: Validity, Uniqueness, and Novelty")
plt.legend()
plt.grid(True)
plt.savefig(
os.path.join(output_dir, "metrics_curve.png"), dpi=300, bbox_inches="tight"
)
plt.close()
print(f"Saved metrics plot to {os.path.join(output_dir, 'metrics_curve.png')}")
if __name__ == "__main__":
plot_metrics()
|