forked from petersn/goodhart
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.py
More file actions
124 lines (90 loc) · 3.66 KB
/
Copy pathplot.py
File metadata and controls
124 lines (90 loc) · 3.66 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
#!/usr/bin/python
from __future__ import print_function
import sys
import os
import glob
import pickle
import collections
import matplotlib.pyplot as plt
import numpy as np
if sys.version_info < (3,):
range = xrange
EPSILON = 0.01
NUM_BUCKETS = 500
def make_bucket(bucket_vals, epsilon, x):
return np.abs(bucket_vals - x) < epsilon
def bucket_mean(bucket_vals, bucket):
return np.mean(bucket_vals[bucket])
def sample_delta(samples, optimized_samples, bucket):
return np.mean(optimized_samples[bucket] - samples[bucket])
def bucket_count(bucket):
count = np.sum(bucket)
return count if count != 0 else np.nan
# def expand_bucket(bucket):
# first_true_i = np.argmax(bucket)
# if not first_true_i:
# return bucket
# new_bucket = np.copy(bucket)
# new_bucket[first_true_i - 1] = True
# return new_bucket
# def sign(x):
# return x / abs(x)
def produce_traces(run):
linear = np.linspace(-1, 1, NUM_BUCKETS)
random_proxy = []
optimized_proxy = []
random_real = []
optimized_real = []
deltas = []
random_samples = []
optimized_samples = []
delta_samples = []
for x in linear:
random_bucket = make_bucket(run["random_proxy_vals"], EPSILON, x)
optimized_bucket = make_bucket(run["optimized_proxy_vals"], EPSILON, x)
random_proxy.append(bucket_mean(run["random_proxy_vals"], random_bucket))
optimized_proxy.append(bucket_mean(run["optimized_proxy_vals"], optimized_bucket))
random_real.append(bucket_mean(run["random_real_vals"], random_bucket))
optimized_real.append(bucket_mean(run["optimized_real_vals"], optimized_bucket))
delta_bucket = make_bucket(run["optimized_proxy_vals"] - run["random_proxy_vals"], EPSILON, x)
deltas.append(bucket_mean(run["optimized_real_vals"] - run["random_real_vals"], delta_bucket))
# optimized_samples.append(bucket_count(optimized_bucket))
# random_samples.append(bucket_count(random_bucket))
# delta_samples.append(bucket_count(delta_bucket))
return {
"linear": linear,
"random_proxy": random_proxy,
"optimized_proxy": optimized_proxy,
"random_real": random_real,
"optimized_real": optimized_real,
"random_samples": random_samples,
"optimized_samples": optimized_samples,
"delta_samples": delta_samples,
"deltas": deltas,
}
def get_traces_for_file(run_path):
print("Processing:", run_path)
with open(run_path, "rb") as f:
return pickle.load(f)
if __name__ == "__main__":
run_paths = glob.glob("runs/*.pickle")
traces = collections.defaultdict(list)
for run_path in run_paths:
run_data = get_traces_for_file(run_path)
for trace_name, trace_array in produce_traces(run_data).items():
traces[trace_name].append(trace_array)
# Average the traces.
for k in traces:
traces[k] = np.nanmean(traces[k], axis=0)
plt.rcParams["figure.figsize"] = 16, 12
plt.plot(traces["linear"], traces["random_real"], label="Real Utility (Random)")
plt.plot(traces["linear"], traces["optimized_real"], label="Real Utility (Optimized)")
plt.plot(traces["linear"], traces["optimized_real"] - traces["random_real"], label="Real Utility (Optimized - Random)")
plt.plot(traces["linear"], traces["optimized_proxy"] - traces["random_proxy"], label="Proxy Utility (Optimized - Random)")
plt.plot(traces["linear"], traces["deltas"], label="Optimized - Random (Real vs. Proxy)")
plt.legend()
plt.xlabel("Proxy Utility")
plt.grid()
plot_file = os.path.join(os.path.dirname(__file__), "output.png")
plt.savefig(plot_file, dpi=600)
plt.show()