File size: 12,461 Bytes
4f61f94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
"""
Visualization for Representation Learning Dynamics experiment.
================================================================
Generates publication-quality figures from experiment results.
"""

import json
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from pathlib import Path
from typing import Dict, List, Optional
import argparse


def load_results(results_path: str) -> Dict:
    with open(results_path) as f:
        return json.load(f)


def extract_metric_series(history: List[Dict], metric_name: str) -> tuple:
    """Extract (steps, values) for a metric from history."""
    steps = [h['step'] for h in history if metric_name in h]
    values = [h[metric_name] for h in history if metric_name in h]
    return np.array(steps), np.array(values)


def plot_training_curves(results: Dict, output_dir: str):
    """Plot training loss and task accuracies across all phases."""
    fig, axes = plt.subplots(2, 2, figsize=(14, 10))

    # Phase 1
    p1 = results['phase1_history']
    steps_p1 = [h['step'] for h in p1]
    loss_p1 = [h['train_loss'] for h in p1]
    acc_add_p1 = [h.get('eval/add_test_acc', 0) for h in p1]
    acc_sub_p1 = [h.get('eval/subtract_test_acc', 0) for h in p1]

    # Phase 2 A→A
    p2aa = results['phase2_aa_history']
    steps_aa = [h['step'] + steps_p1[-1] for h in p2aa] if p2aa else []
    loss_aa = [h['train_loss'] for h in p2aa]
    acc_add_aa = [h.get('eval/add_test_acc', 0) for h in p2aa]
    acc_sub_aa = [h.get('eval/subtract_test_acc', 0) for h in p2aa]

    # Phase 2 A→B
    p2ab = results['phase2_ab_history']
    steps_ab = [h['step'] + steps_p1[-1] for h in p2ab] if p2ab else []
    loss_ab = [h['train_loss'] for h in p2ab]
    acc_add_ab = [h.get('eval/add_test_acc', 0) for h in p2ab]
    acc_sub_ab = [h.get('eval/subtract_test_acc', 0) for h in p2ab]

    # Training loss
    ax = axes[0, 0]
    ax.plot(steps_p1, loss_p1, 'k-', label='Phase 1 (Add)', linewidth=2)
    if steps_aa:
        ax.plot(steps_aa, loss_aa, 'b-', label='A→A (Continue Add)', linewidth=2)
    if steps_ab:
        ax.plot(steps_ab, loss_ab, 'r-', label='A→B (Switch to Sub)', linewidth=2)
    ax.axvline(x=steps_p1[-1] if steps_p1 else 0, color='gray', linestyle='--',
               alpha=0.5, label='Phase transition')
    ax.set_xlabel('Training Step')
    ax.set_ylabel('Loss')
    ax.set_title('Training Loss')
    ax.legend()
    ax.set_yscale('log')

    # Addition accuracy
    ax = axes[0, 1]
    ax.plot(steps_p1, acc_add_p1, 'k-', label='Phase 1', linewidth=2)
    if steps_aa:
        ax.plot(steps_aa, acc_add_aa, 'b-', label='A→A', linewidth=2)
    if steps_ab:
        ax.plot(steps_ab, acc_add_ab, 'r-', label='A→B', linewidth=2)
    ax.axvline(x=steps_p1[-1] if steps_p1 else 0, color='gray',
               linestyle='--', alpha=0.5)
    ax.set_xlabel('Training Step')
    ax.set_ylabel('Accuracy')
    ax.set_title('Task A (Addition) Accuracy')
    ax.legend()
    ax.set_ylim(-0.05, 1.05)

    # Subtraction accuracy
    ax = axes[1, 0]
    ax.plot(steps_p1, acc_sub_p1, 'k-', label='Phase 1', linewidth=2)
    if steps_aa:
        ax.plot(steps_aa, acc_sub_aa, 'b-', label='A→A', linewidth=2)
    if steps_ab:
        ax.plot(steps_ab, acc_sub_ab, 'r-', label='A→B', linewidth=2)
    ax.axvline(x=steps_p1[-1] if steps_p1 else 0, color='gray',
               linestyle='--', alpha=0.5)
    ax.set_xlabel('Training Step')
    ax.set_ylabel('Accuracy')
    ax.set_title('Task B (Subtraction) Accuracy')
    ax.legend()
    ax.set_ylim(-0.05, 1.05)

    # Gradient alignment
    ax = axes[1, 1]
    ga_p1 = [h.get('gradient_alignment_a_vs_b', 0) for h in p1]
    ga_aa = [h.get('gradient_alignment_a_vs_b', 0) for h in p2aa]
    ga_ab = [h.get('gradient_alignment_a_vs_b', 0) for h in p2ab]
    ax.plot(steps_p1, ga_p1, 'k-', label='Phase 1', linewidth=2)
    if steps_aa:
        ax.plot(steps_aa, ga_aa, 'b-', label='A→A', linewidth=2)
    if steps_ab:
        ax.plot(steps_ab, ga_ab, 'r-', label='A→B', linewidth=2)
    ax.axvline(x=steps_p1[-1] if steps_p1 else 0, color='gray',
               linestyle='--', alpha=0.5)
    ax.axhline(y=0, color='gray', linestyle=':', alpha=0.3)
    ax.set_xlabel('Training Step')
    ax.set_ylabel('Cosine Similarity')
    ax.set_title('Gradient Alignment (Task A vs Task B)')
    ax.legend()

    plt.tight_layout()
    plt.savefig(f'{output_dir}/training_curves.png', dpi=150, bbox_inches='tight')
    plt.close()
    print(f"Saved: {output_dir}/training_curves.png")


def plot_cka_dynamics(results: Dict, output_dir: str):
    """Plot CKA drift from Phase 1 end across all layers."""
    fig, axes = plt.subplots(1, 2, figsize=(14, 5))

    n_layers = results['config']['n_layers'] + 1

    for layer_idx in range(n_layers):
        metric = f'layer_{layer_idx}/cka_vs_phase1'

        # A→A branch
        p2aa = results['phase2_aa_history']
        steps_aa = [h['step'] for h in p2aa if metric in h]
        vals_aa = [h[metric] for h in p2aa if metric in h]

        # A→B branch
        p2ab = results['phase2_ab_history']
        steps_ab = [h['step'] for h in p2ab if metric in h]
        vals_ab = [h[metric] for h in p2ab if metric in h]

        label = f'Layer {layer_idx}' if layer_idx > 0 else 'Embedding'
        axes[0].plot(steps_aa, vals_aa, '-', label=label, linewidth=1.5)
        axes[1].plot(steps_ab, vals_ab, '-', label=label, linewidth=1.5)

    axes[0].set_title('Branch A→A: CKA vs Phase 1 End')
    axes[0].set_xlabel('Training Step')
    axes[0].set_ylabel('CKA Similarity')
    axes[0].legend()
    axes[0].set_ylim(0, 1.05)

    axes[1].set_title('Branch A→B: CKA vs Phase 1 End')
    axes[1].set_xlabel('Training Step')
    axes[1].set_ylabel('CKA Similarity')
    axes[1].legend()
    axes[1].set_ylim(0, 1.05)

    plt.tight_layout()
    plt.savefig(f'{output_dir}/cka_dynamics.png', dpi=150, bbox_inches='tight')
    plt.close()
    print(f"Saved: {output_dir}/cka_dynamics.png")


def plot_attention_entropy(results: Dict, output_dir: str):
    """Plot attention entropy per head over training."""
    n_layers = results['config']['n_layers']
    n_heads = results['config']['n_heads']

    fig, axes = plt.subplots(n_layers, 2, figsize=(14, 4 * n_layers))
    if n_layers == 1:
        axes = axes.reshape(1, 2)

    for layer_idx in range(n_layers):
        for head_idx in range(n_heads):
            metric = f'layer_{layer_idx+1}/head_{head_idx}_entropy'

            # A→A
            p2aa = results['phase2_aa_history']
            steps_aa = [h['step'] for h in p2aa if metric in h]
            vals_aa = [h[metric] for h in p2aa if metric in h]
            axes[layer_idx, 0].plot(steps_aa, vals_aa, label=f'Head {head_idx}')

            # A→B
            p2ab = results['phase2_ab_history']
            steps_ab = [h['step'] for h in p2ab if metric in h]
            vals_ab = [h[metric] for h in p2ab if metric in h]
            axes[layer_idx, 1].plot(steps_ab, vals_ab, label=f'Head {head_idx}')

        axes[layer_idx, 0].set_title(f'Layer {layer_idx+1} — A→A')
        axes[layer_idx, 0].set_ylabel('Entropy (bits)')
        axes[layer_idx, 0].legend()
        axes[layer_idx, 1].set_title(f'Layer {layer_idx+1} — A→B')
        axes[layer_idx, 1].legend()

    axes[-1, 0].set_xlabel('Training Step')
    axes[-1, 1].set_xlabel('Training Step')

    plt.tight_layout()
    plt.savefig(f'{output_dir}/attention_entropy.png', dpi=150, bbox_inches='tight')
    plt.close()
    print(f"Saved: {output_dir}/attention_entropy.png")


def plot_cka_heatmaps(results: Dict, output_dir: str):
    """Plot CKA cross-layer heatmaps for final model comparisons."""
    heatmaps = results['cka_heatmaps']

    fig, axes = plt.subplots(1, 3, figsize=(18, 5))

    titles = ['A→A vs A→B', 'A→A vs Phase 1 End', 'A→B vs Phase 1 End']
    keys = ['aa_vs_ab', 'aa_vs_p1', 'ab_vs_p1']

    for ax, title, key in zip(axes, titles, keys):
        hm = np.array(heatmaps[key])
        im = ax.imshow(hm, cmap='viridis', vmin=0, vmax=1, aspect='auto')
        ax.set_title(title)
        ax.set_xlabel('Layer (model 2)')
        ax.set_ylabel('Layer (model 1)')
        # Add text annotations
        for i in range(hm.shape[0]):
            for j in range(hm.shape[1]):
                color = 'white' if hm[i, j] < 0.5 else 'black'
                ax.text(j, i, f'{hm[i,j]:.2f}', ha='center', va='center',
                        fontsize=8, color=color)
        plt.colorbar(im, ax=ax, fraction=0.046, pad=0.04)

    plt.tight_layout()
    plt.savefig(f'{output_dir}/cka_heatmaps.png', dpi=150, bbox_inches='tight')
    plt.close()
    print(f"Saved: {output_dir}/cka_heatmaps.png")


def plot_subspace_angles(results: Dict, output_dir: str):
    """Plot subspace angle divergence between branches."""
    n_layers = results['config']['n_layers'] + 1

    fig, ax = plt.subplots(figsize=(10, 5))

    for layer_idx in range(n_layers):
        metric = f'layer_{layer_idx}/subspace_angle_vs_phase1'

        p2aa = results['phase2_aa_history']
        steps_aa = [h['step'] for h in p2aa if metric in h]
        vals_aa = [h[metric] for h in p2aa if metric in h]

        p2ab = results['phase2_ab_history']
        steps_ab = [h['step'] for h in p2ab if metric in h]
        vals_ab = [h[metric] for h in p2ab if metric in h]

        label = f'Layer {layer_idx}' if layer_idx > 0 else 'Embedding'
        if steps_aa:
            ax.plot(steps_aa, vals_aa, '--', label=f'{label} (A→A)',
                    alpha=0.7, linewidth=1.5)
        if steps_ab:
            ax.plot(steps_ab, vals_ab, '-', label=f'{label} (A→B)',
                    linewidth=2)

    ax.set_xlabel('Training Step')
    ax.set_ylabel('Mean Subspace Angle (degrees)')
    ax.set_title('Subspace Angle Drift from Phase 1 End')
    ax.legend(bbox_to_anchor=(1.05, 1), loc='upper left')
    plt.tight_layout()
    plt.savefig(f'{output_dir}/subspace_angles.png', dpi=150, bbox_inches='tight')
    plt.close()
    print(f"Saved: {output_dir}/subspace_angles.png")


def plot_weight_changes(results: Dict, output_dir: str):
    """Plot weight change magnitude per block."""
    n_blocks = results['config']['n_layers']

    fig, axes = plt.subplots(1, 2, figsize=(14, 5))

    for block_idx in range(n_blocks):
        metric_init = f'block_{block_idx}/weight_change_from_init'
        metric_p1 = f'block_{block_idx}/weight_change_from_phase1'

        # A→A
        p2aa = results['phase2_aa_history']
        steps = [h['step'] for h in p2aa if metric_p1 in h]
        vals = [h[metric_p1] for h in p2aa if metric_p1 in h]
        axes[0].plot(steps, vals, label=f'Block {block_idx}', linewidth=2)

        # A→B
        p2ab = results['phase2_ab_history']
        steps = [h['step'] for h in p2ab if metric_p1 in h]
        vals = [h[metric_p1] for h in p2ab if metric_p1 in h]
        axes[1].plot(steps, vals, label=f'Block {block_idx}', linewidth=2)

    axes[0].set_title('A→A: Weight Change from Phase 1')
    axes[0].set_xlabel('Training Step')
    axes[0].set_ylabel('L2 Norm of Weight Delta')
    axes[0].legend()

    axes[1].set_title('A→B: Weight Change from Phase 1')
    axes[1].set_xlabel('Training Step')
    axes[1].set_ylabel('L2 Norm of Weight Delta')
    axes[1].legend()

    plt.tight_layout()
    plt.savefig(f'{output_dir}/weight_changes.png', dpi=150, bbox_inches='tight')
    plt.close()
    print(f"Saved: {output_dir}/weight_changes.png")


def generate_all_plots(results_path: str, output_dir: str = None):
    """Generate all visualization plots from experiment results."""
    results = load_results(results_path)
    if output_dir is None:
        output_dir = str(Path(results_path).parent)

    Path(output_dir).mkdir(parents=True, exist_ok=True)

    plot_training_curves(results, output_dir)
    plot_cka_dynamics(results, output_dir)
    plot_attention_entropy(results, output_dir)
    plot_cka_heatmaps(results, output_dir)
    plot_subspace_angles(results, output_dir)
    plot_weight_changes(results, output_dir)

    print(f"\nAll plots saved to {output_dir}/")


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--results', type=str, default='results/experiment_results.json')
    parser.add_argument('--output-dir', type=str, default=None)
    args = parser.parse_args()
    generate_all_plots(args.results, args.output_dir)