File size: 6,906 Bytes
4ca4e4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
import torch
import matplotlib.pyplot as plt
from collections import defaultdict
import os

# colors = {"TF": {"TF": "blue", "SSM": "orange"}, "SSM": {"TF": "green", "SSM": "red"}, "TF-nC": {"TF-nC": "brown"}}
colors = {"TF_TF": "blue", "TF_SSM": "orange", "SSM_TF": "green", "SSM_SSM": "red", "TF-nC_TF-nC": "brown"}



def Int(s): return int("".join([c for c in s if c.isnumeric()]))
def Empty(): return []



def get_val_and_bounds(data):
    mean = np.mean(data, axis=0)
    median = np.median(data, axis=0)
    
    # return mean, np.min(data, axis=0), np.max(data, axis=0)
    # return mean, mean-np.std(data, axis=0), mean+np.std(data, axis=0)
    # return median, np.min(data, axis=0), np.max(data, axis=0)
    return median, np.quantile(data, 0.10, axis=0), np.quantile(data, 0.90, axis=0)


def savefig(taskname, filename):
    if "fig" not in os.listdir("results/" + taskname):
        os.mkdir("results/" + taskname + "/fig")
        
    plt.savefig("results/" + taskname + "/fig/" + filename + ".png")



split_array = ['_', 'task_name', 'layer1', 'layer2', 'window', 'dim', 'num_heads', 'state_dim']
def plot(data, params, ind_var, diff_lines="layers", param_counts=None, x_axis=None):
    fig, ax = plt.subplots()
    if x_axis == 'epochs':
        xs = defaultdict(Empty)
        ys = defaultdict(Empty)
        ys_lower = defaultdict(Empty)
        ys_upper = defaultdict(Empty)
        
        # Get the relevant data for these params
        for k in data.keys():
            d = dict(zip(split_array, k.split('_')))
            if diff_lines != 'layer1'    and params['layer1']    != d['layer1']:         continue
            if diff_lines != 'layer2'    and params['layer2']    != d['layer2']:         continue
            if diff_lines != 'window'    and params['window']    != Int(d['window']):    continue
            if diff_lines != 'dim'       and params['dim']       != Int(d['dim']):       continue
            if diff_lines != 'num_heads' and params['num_heads'] != Int(d['num_heads']): continue
            if diff_lines != 'state_dim' and params['state_dim'] != Int(d['state_dim']): continue

            key = Int(d[diff_lines])

            xs[key] = np.arange(0, data[k].shape[1])
            ys[key], ys_lower[key], ys_upper[key] = get_val_and_bounds(data[k])

        legend = []
        keys = sorted(list(ys.keys()))
        for key in keys:
            plt.plot(xs[key], ys[key])
            legend.append(key)
        
        plt.legend(legend)

        for key in ys.keys():
            plt.fill_between(xs[key], ys_lower[key], ys_upper[key], color='lightblue', alpha=0.08)
                

    elif diff_lines == 'layers':
        xs = defaultdict(Empty)
        ys = defaultdict(Empty)
        ys_lower = defaultdict(Empty)
        ys_upper = defaultdict(Empty)

        # Get the relevant data for these params
        for k in data.keys():
            d = dict(zip(split_array, k.split('_')))
            if ind_var != 'window'    and params['window']    != Int(d['window']):    continue
            if ind_var != 'dim'       and params['dim']       != Int(d['dim']):       continue
            if ind_var != 'num_heads' and params['num_heads'] != Int(d['num_heads']): continue
            if ind_var != 'state_dim' and params['state_dim'] != Int(d['state_dim']): continue

            key = d['layer1'] + '_' + d['layer2']

            if d['layer2'].isnumeric():
                print("Ignoring", key)
                continue

            if x_axis == 'params':
                xs[key].append(param_counts[k])
            else:
                xs[key].append(Int(d[ind_var]))
                
            r1, r2, r3 = get_val_and_bounds(data[k][:, -1])
            ys[key].append(r1)
            ys_lower[key].append(r2)
            ys_upper[key].append(r3)

        # Sort the data so it is in order on the x axis
        for key in ys.keys():
            ys[key] = [a[1] for a in sorted(zip(xs[key], ys[key]))]
            ys_lower[key] = [a[1] for a in sorted(zip(xs[key], ys_lower[key]))]
            ys_upper[key] = [a[1] for a in sorted(zip(xs[key], ys_upper[key]))]
            xs[key].sort()

        # Plot the lines
        legend = []
        for key in ys.keys():
            if key == "SSM_SSM" and ind_var == "num_heads" or key == "TF_TF" and ind_var == "state_dim":
                plt.axhline(y=ys[key][0], color=colors[key], linestyle='dashed')
            else:
                plt.plot(xs[key], ys[key], c=colors[key])
            legend.append(key)
        plt.legend(legend)

        # Plot the error bars
        for key in ys.keys():
            if key == "SSM_SSM" and ind_var == "num_heads" or key == "TF_TF" and ind_var == "state_dim":
                plt.fill_between(ax.get_xlim(), ys_lower[key][0], ys_upper[key][0], color=colors[key], alpha=0.08)
            else:
                plt.fill_between(xs[key], ys_lower[key], ys_upper[key], color=colors[key], alpha=0.08)


    elif diff_lines == 'depths':
        assert False # TODO: Doesn't plot across depth
        xs = defaultdict(Empty)
        ys = defaultdict(Empty)
        ys_lower = defaultdict(Empty)
        ys_upper = defaultdict(Empty)

        # Get the relevant data for these params
        for k in data.keys():
            d = dict(zip(split_array, k.split('_')))
            if ind_var != 'window'    and params['window']    != Int(d['window']):    continue
            if ind_var != 'dim'       and params['dim']       != Int(d['dim']):       continue
            if ind_var != 'num_heads' and params['num_heads'] != Int(d['num_heads']): continue
            if ind_var != 'state_dim' and params['state_dim'] != Int(d['state_dim']): continue

            key = d['layer1'] + '_' + d['layer2']

            if x_axis == 'params':
                xs[key].append(param_counts[key])
            else:
                xs[key].append(Int(d[ind_var]))
                
            r1, r2, r3 = get_val_and_bounds(data[k][:, -1])
            ys[key].append(r1)
            ys_lower[key].append(r2)
            ys_upper[key].append(r3)

        # Sort the data so it is in order on the x axis
        for key in ys.keys():
            ys[key] = [a[1] for a in sorted(zip(xs[key], ys[key]))]
            ys_lower[key] = [a[1] for a in sorted(zip(xs[key], ys_lower[key]))]
            ys_upper[key] = [a[1] for a in sorted(zip(xs[key], ys_upper[key]))]
            xs[key].sort()

        # Plot the lines
        legend = []
        for key in ys.keys():
            plt.plot(xs[key], ys[key], c=colors[key.split("_")[0] + "_" + key.split("_")[0]])
            legend.append(key)
        plt.legend(legend)

        # Plot the error bars
        for key in ys.keys():
            plt.fill_between(xs[key], ys_lower[key], ys_upper[key], color=colors[key.split("_")[0] + "_" + key.split("_")[0]], alpha=0.08)

    return diff_lines + "_" + ind_var