-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_comparison.py
More file actions
162 lines (136 loc) · 5.83 KB
/
Copy pathplot_comparison.py
File metadata and controls
162 lines (136 loc) · 5.83 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import matplotlib.pyplot as plt
import json
import os
def plot_comparison():
"""
Generate comparison plots of AEVB vs Wake-Sleep training efficiency.
Replicates Figure 2 in the Kingma & Welling (2013) paper.
"""
results_dir = "results/metrics"
# All the configurations we implemented and saved previously
mnist_configs = [
{"latent_dim": 3, "dataset": "mnist"},
{"latent_dim": 5, "dataset": "mnist"},
{"latent_dim": 10, "dataset": "mnist"},
{"latent_dim": 20, "dataset": "mnist"},
{"latent_dim": 200, "dataset": "mnist"},
]
frey_configs = [
{"latent_dim": 2, "dataset": "frey_face"},
{"latent_dim": 5, "dataset": "frey_face"},
{"latent_dim": 10, "dataset": "frey_face"},
{"latent_dim": 20, "dataset": "frey_face"},
]
# Create a 2x5 grid
fig = plt.figure(figsize=(16, 7))
stride = 100
# ==========================================
# 1. MNIST PLOTS (Top Row: Subplots 1 to 5)
# ==========================================
for idx, config in enumerate(mnist_configs):
ax = plt.subplot(2, 5, idx + 1)
latent_dim = config["latent_dim"]
dataset = config["dataset"]
aevb_path = os.path.join(results_dir, f"aevb_{dataset}_{latent_dim}d_metrics.json")
ws_path = os.path.join(results_dir, f"wake_sleep_{dataset}_{latent_dim}d_metrics.json")
data_found = False
if os.path.exists(aevb_path) and os.path.exists(ws_path):
with open(aevb_path) as f:
aevb_metrics = json.load(f)
with open(ws_path) as f:
ws_metrics = json.load(f)
data_found = True
ax.semilogx(
aevb_metrics["samples_evaluated"][::stride],
[-x for x in aevb_metrics["train_loss"]][::stride],
"r-", linewidth=2
)
ax.semilogx(
aevb_metrics["samples_evaluated"][::stride],
[-x for x in aevb_metrics["test_loss"]][::stride],
"r--", linewidth=2
)
ax.semilogx(
ws_metrics["samples_evaluated"][::stride],
[-x for x in ws_metrics["train_loss"]][::stride],
"g-", linewidth=2
)
ax.semilogx(
ws_metrics["samples_evaluated"][::stride],
[-x for x in ws_metrics["test_loss"]][::stride],
"g-.", linewidth=2
)
ax.set_xlabel("# Training samples evaluated")
ax.set_ylabel(r"$\mathcal{L}$")
ax.set_title(f"MNIST, $N_z$={latent_dim}")
ax.grid(True, alpha=0.3)
if not data_found:
ax.text(0.5, 0.5, f"No data for Nz={latent_dim}", ha="center", va="center", transform=ax.transAxes)
# ==========================================
# 2. FREY FACE PLOTS (Bottom Row: Subplots 7 to 10)
# ==========================================
for idx, config in enumerate(frey_configs):
ax = plt.subplot(2, 5, idx + 7)
latent_dim = config["latent_dim"]
dataset = config["dataset"]
aevb_path = os.path.join(results_dir, f"aevb_{dataset}_{latent_dim}d_metrics.json")
ws_path = os.path.join(results_dir, f"wake_sleep_{dataset}_{latent_dim}d_metrics.json")
data_found = False
if os.path.exists(aevb_path) and os.path.exists(ws_path):
with open(aevb_path) as f:
aevb_metrics = json.load(f)
with open(ws_path) as f:
ws_metrics = json.load(f)
data_found = True
ax.semilogx(
aevb_metrics["samples_evaluated"][::stride],
[-x for x in aevb_metrics["train_loss"]][::stride],
"r-", linewidth=2
)
ax.semilogx(
aevb_metrics["samples_evaluated"][::stride],
[-x for x in aevb_metrics["test_loss"]][::stride],
"r--", linewidth=2
)
ax.semilogx(
ws_metrics["samples_evaluated"][::stride],
[-x for x in ws_metrics["train_loss"]][::stride],
"g-", linewidth=2
)
ax.semilogx(
ws_metrics["samples_evaluated"][::stride],
[-x for x in ws_metrics["test_loss"]][::stride],
"g-.", linewidth=2
)
ax.set_xlabel("# Training samples evaluated")
ax.set_ylabel(r"$\mathcal{L}$")
ax.set_title(f"Frey Face, $N_z$={latent_dim}")
ax.grid(True, alpha=0.3)
if not data_found:
ax.text(0.5, 0.5, f"No data for Nz={latent_dim}", ha="center", va="center", transform=ax.transAxes)
# ==========================================
# 3. GLOBAL AXIS CROPPING
# ==========================================
for ax in fig.axes:
ax.set_xlim([1e5, 1e8])
title = ax.get_title()
if 'MNIST' in title:
ax.set_ylim([-150, -70])
elif 'Frey Face' in title:
ax.set_ylim([0, 1600])
ax_legend = plt.subplot(2, 5, 6)
ax_legend.axis("off")
# Custom handles to match the plotted styles perfectly
handles = [
plt.Line2D([0], [0], color="g", linestyle="-.", linewidth=2),
plt.Line2D([0], [0], color="g", linestyle="-", linewidth=2),
plt.Line2D([0], [0], color="r", linestyle="--", linewidth=2),
plt.Line2D([0], [0], color="r", linestyle="-", linewidth=2),
]
labels = ["Wake-Sleep (test)", "Wake-Sleep (train)", "AEVB (test)", "AEVB (train)"]
ax_legend.legend(handles, labels, loc="center", fontsize=11, frameon=True, edgecolor="black")
plt.tight_layout()
plt.savefig("results/comparison_aevb_vs_wake_sleep.png", dpi=150, bbox_inches="tight")
print("Comparison plot saved to results/comparison_aevb_vs_wake_sleep.png")
if __name__ == "__main__":
plot_comparison()