forked from EleutherAI/pythia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mem_graph.py
216 lines (179 loc) · 7.63 KB
/
mem_graph.py
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
import os
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from tqdm import tqdm
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
models = ['19m', '125m', '350m', '800m', '1.3b', '2.7b', '6.7b', '13b']
checkpoints = ['23000', '43000', '63000', '83000', '103000', '123000', '143000']
checkpoint_names = ['23m', '44m', '65m', '85m', '105m', '126m', '146m']
memorization_results = {}
folderpath = "/fsx/orz/memorization-evals/"
for model in tqdm(models):
for idx, checkpoint in enumerate(tqdm(checkpoints)):
modelpath = os.path.join(folderpath, f'memorization_{model}_{checkpoint}.hdf')
memorization_results[f'{model}-{checkpoint_names[idx]}'] = pd.read_hdf(modelpath, key="memorization")
memorization_results[f'{model}-{checkpoint_names[idx]}'].sort_values(by='index', inplace=True)
def process_memorization_over_time(models, checkpoints):
cm_rate_df = pd.DataFrame(
data={
"checkpoint": [],
"model": [],
"TP": [],
"FP": [],
"FN": [],
"TN": [],
"TPR": [],
"FPR": [],
"FNR": [],
}
)
# We only consider Sequence indicies that are evaluated by all checkpoints
max_sequence_index = 23000*1024
for model in models:
evals = memorization_results[f'{model}-146m']
evals = evals[evals['index'] < max_sequence_index]
ground_truth = evals['accuracy'] == 1
for idx, checkpoint in enumerate(checkpoints):
evals = memorization_results[f'{model}-{checkpoint}']
evals = evals[evals['index'] < max_sequence_index]
prediction = evals['accuracy'] == 1
matrix = confusion_matrix(prediction, ground_truth)
TN, FP, FN, TP = matrix.ravel()
N = max_sequence_index
ax = sns.heatmap(np.array([[TP/N, FP/N],[FN/N, TN/N]]), annot=True, fmt=".5%")
# set x-axis label and ticks.
ax.set_xlabel("Actual Labels")
ax.xaxis.set_ticklabels(['1', '0'])
# set y-axis label and ticks
ax.set_ylabel("Predicted Labels")
ax.yaxis.set_ticklabels(['1', '0'])
ax.set_title("Predicting Memorization of Last Checkpoint\nModel Size {}, Checkpoint {}".format(model, checkpoint))
plt.savefig('../../results/graphs/memorization_early_checkpoint_predict_last_checkpoint/graph_{}_{}.svg'.format(model, checkpoint), dpi=300)
plt.clf()
cm_rate_df = pd.concat(
[
cm_rate_df,
pd.DataFrame(
{
"checkpoint": [checkpoint],
"model": [model],
"TP": [TP],
"FP": [FP],
"FN": [FN],
"TN": [TN],
"TPR": [TP/(TP+FN)],
"FPR": [FP/(FP+TN)],
"FNR": [FN/(FN+TP)],
}
)
],
ignore_index=True
)
return cm_rate_df
cm_rate_df = process_memorization_over_time(models, checkpoint_names)
_df = cm_rate_df[cm_rate_df['checkpoint'] != '146m']
sns.lineplot(data=_df, x="checkpoint", y="TPR", hue="model")
ax.set_title("True Positive Rate of Memorization")
plt.savefig('../../results/graphs/memorization_rates_through_time/graph_tpr.svg', dpi=300)
plt.clf()
sns.lineplot(data=_df, x="checkpoint", y="FPR", hue="model")
ax.set_title("False Positive Rate of Memorization")
plt.savefig('../../results/graphs/memorization_rates_through_time/graph_fpr.svg', dpi=300)
plt.clf()
sns.lineplot(data=_df, x="checkpoint", y="FNR", hue="model")
ax.set_title("False Negative Rate of Memorization")
plt.savefig('../../results/graphs/memorization_rates_through_time/graph_fnr.svg', dpi=300)
plt.clf()
def process_memorization_over_size(models, checkpoints):
cm_rate_df = pd.DataFrame(
data={
"checkpoint": [],
"model": [],
"TP": [],
"FP": [],
"FN": [],
"TN": [],
"TPR": [],
"FPR": [],
"FNR": [],
}
)
# We only consider Sequence indicies that are evaluated by all checkpoints
max_sequence_index = 23000*1024
for checkpoint in checkpoints:
evals = memorization_results[f'13b-{checkpoint}']
evals = evals[evals['index'] < max_sequence_index]
ground_truth = evals['accuracy'] == 1
for model in models:
evals = memorization_results[f'{model}-{checkpoint}']
evals = evals[evals['index'] < max_sequence_index]
prediction = evals['accuracy'] == 1
matrix = confusion_matrix(prediction, ground_truth)
TN, FP, FN, TP = matrix.ravel()
N = max_sequence_index
ax = sns.heatmap(np.array([[TP/N, FP/N],[FN/N, TN/N]]), annot=True, fmt=".5%")
# set x-axis label and ticks.
ax.set_xlabel("Actual Labels")
ax.xaxis.set_ticklabels(['1', '0'])
# set y-axis label and ticks
ax.set_ylabel("Predicted Labels")
ax.yaxis.set_ticklabels(['1', '0'])
ax.set_title("Predicting Memorization of Largest Size\nModel Size {}, Checkpoint {}".format(model, checkpoint))
plt.savefig('../../results/graphs/memorization_small_model_predict_large_model/graph_{}_{}.svg'.format(model, checkpoint), dpi=300)
plt.clf()
cm_rate_df = pd.concat(
[
cm_rate_df,
pd.DataFrame(
{
"checkpoint": [checkpoint],
"model": [model],
"TP": [TP],
"FP": [FP],
"FN": [FN],
"TN": [TN],
"TPR": [TP/(TP+FN)],
"FPR": [FP/(FP+TN)],
"FNR": [FN/(FN+TP)],
}
)
],
ignore_index=True
)
return cm_rate_df
def rate_of_memorization(models, checkpoints):
df = pd.DataFrame(
data={
"checkpoint": [],
"model": [],
"num_memorization": [],
}
)
# n_steps = ['23000', '43000', '63000', '83000', '103000', '123000', '143000']
# We only consider Sequence indicies that are evaluated by all checkpoints
max_sequence_index = 23000*1024
for model in models:
for idx, checkpoint in enumerate(checkpoints):
# max_sequence_index = int(n_steps[idx]) * 1024
evals = memorization_results[f'{model}-{checkpoint}']
evals = evals[evals['index'] < max_sequence_index]
prediction = evals['accuracy'] == 1
df = pd.concat(
[df,
pd.DataFrame({
"checkpoint": [checkpoint],
"model": [model],
"num_memorization": [prediction.mean()*100],
})],
ignore_index=True
)
return df
df = rate_of_memorization(models, checkpoint_names)
ax = sns.lineplot(data=df, x="checkpoint", y="num_memorization", hue="model")
ax.set_title("Number of Memorized Sequences Seen from Earliest Checkpoint")
ax.set_xlabel("Checkpoint") #, fontsize=14) #, labelpad=20)
ax.set_ylabel("Memorized Sequences (%)") #, fontsize=14) #, labelpad=20)
plt.savefig('graph_num_memorized.svg', dpi=300)
plt.clf()