-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
264 lines (192 loc) · 7.47 KB
/
utils.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
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
"""
CMPUT 652, Fall 2019 - Assignment #2
__author__ = "Hager Radi"
utility functions for plotting
"""
import random
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
def sliding_window(data, N):
"""
For each index, k, in data we average over the window from k-N-1 to k. The beginning handles incomplete buffers,
that is it only takes the average over what has actually been seen.
:param data: A numpy array, length M
:param N: The length of the sliding window.
:return: A numpy array, length M, containing smoothed averaging.
"""
idx = 0
window = np.zeros(N)
smoothed = np.zeros(len(data))
for i in range(len(data)):
window[idx] = data[i]
idx += 1
smoothed[i] = window[0:idx].mean()
if idx == N:
window[0:-1] = window[1:]
idx = N - 1
return smoothed
# plotting averaged runs with their standard deviations
def plot_means_1(returns_over_runs, runs, episodes):
ep_returns_means = []
ep_returns_stds = []
run_3 = random.sample(range(0, runs), 3)
run_10 = random.sample(range(0, runs), 10)
fig, ax = plt.subplots(1)
x = np.arange(1, episodes+1)
# averaging 3 plots
mean = np.mean(returns_over_runs[run_3], axis=0)
std = np.std(returns_over_runs[run_3], axis=0)
y = sliding_window(mean, 100)
ax.plot(x, y, lw=2, color='blue' , label='3 averaged runs')
ax.fill_between(x, y-std , y+std, facecolor='blue', alpha=0.2)
#
# # averaging 10 plots
mean = np.mean(returns_over_runs[run_10], axis=0)
std = np.std(returns_over_runs[run_10], axis=0)
y = sliding_window(mean, 100)
ax.plot(x, y, lw=2, color='green', label='10 averaged runs')
ax.fill_between(x, y-std , y+std, facecolor='green', alpha=0.2)
# averaging 30 plots
mean = np.mean(returns_over_runs, axis=0)
std = np.std(returns_over_runs, axis=0)
y = sliding_window(mean, 100)
ax.plot(x, y, lw=2, color='red', label='30 averaged runs')
ax.fill_between(x, y-std , y+std, facecolor='red', alpha=0.2)
# drawing all in one figure
ax.set_title("Episode Return")
ax.set_ylabel("Average Return (Sliding Window 100)")
ax.set_xlabel("Episode")
ax.set_title('Average runs with their standard deviation')
ax.legend(loc = 'best')
# plt.show()
fig.savefig('1.png')
plt.close(fig)
# plotting averaged runs with their standard error
def plot_means_2(returns_over_runs, runs, episodes):
ep_returns_means = []
ep_returns_stds = []
run_3 = random.sample(range(0, runs), 3)
run_10 = random.sample(range(0, runs), 10)
fig, ax = plt.subplots(1)
x = np.arange(1, episodes+1)
# averaging 3 plots
mean = np.mean(returns_over_runs[run_3], axis=0)
std = np.std(returns_over_runs[run_3], axis=0) / np.sqrt(3)
y = sliding_window(mean, 100)
ax.plot(x, y, lw=2, color='blue' , label='3 averaged runs')
ax.fill_between(x, y-std , y+std, facecolor='blue', alpha=0.2)
# averaging 10 plots
mean = np.mean(returns_over_runs[run_10], axis=0)
std = np.std(returns_over_runs[run_10], axis=0) / np.sqrt(10)
y = sliding_window(mean, 100)
ax.plot(x, y, lw=2, color='green', label='10 averaged runs')
ax.fill_between(x, y-std , y+std, facecolor='green', alpha=0.2)
# averaging 30 plots
mean = np.mean(returns_over_runs, axis=0)
std = np.std(returns_over_runs, axis=0)/ np.sqrt(30)
y = sliding_window(mean, 100)
ax.plot(x, y, lw=2, color='red', label='30 averaged runs')
ax.fill_between(x, y-std , y+std, facecolor='red', alpha=0.2)
# drawing all in one figure
ax.set_title("Episode Return")
ax.set_ylabel("Average Return (Sliding Window 100)")
ax.set_xlabel("Episode")
ax.set_title('Average runs with their standard error')
ax.legend(loc = 'lower right')
# plt.show()
fig.savefig('2.png')
plt.close(fig)
# plotting averaged runs with their min-max value (for self-check)
def plot_means_3(returns_over_runs, runs, episodes):
ep_returns_means = []
ep_returns_stds = []
run_3 = random.sample(range(0, runs), 3)
run_10 = random.sample(range(0, runs), 10)
fig, ax = plt.subplots(1)
x = np.arange(1, episodes+1)
# averaging 3 plots
mean = np.mean(returns_over_runs[run_3], axis=0)
min = np.min(returns_over_runs[run_3], axis=0)
max = np.max(returns_over_runs[run_3], axis=0)
y = sliding_window(mean, 100)
ax.plot(x, y, lw=2, color='blue' , label='3 averaged runs')
ax.fill_between(x, min , max, facecolor='blue', alpha=0.2)
# averaging 10 plots
mean = np.mean(returns_over_runs[run_10], axis=0)
min = np.min(returns_over_runs[run_10], axis=0)
max = np.max(returns_over_runs[run_10], axis=0)
y = sliding_window(mean, 100)
ax.plot(x, y, lw=2, color='green', label='10 averaged runs')
ax.fill_between(x, min , max, facecolor='green', alpha=0.2)
# averaging 30 plots
mean = np.mean(returns_over_runs, axis=0)
min = np.min(returns_over_runs, axis=0)
max = np.max(returns_over_runs, axis=0)
y = sliding_window(mean, 100)
ax.plot(x, y, lw=2, color='red', label='30 averaged runs')
ax.fill_between(x, min , max, facecolor='red', alpha=0.2)
# drawing all in one figure
ax.set_title("Episode Return")
ax.set_ylabel("Average Return (Sliding Window 100)")
ax.set_xlabel("Episode")
ax.set_title('Average runs with their min-max value')
ax.legend(loc = 'best')
# plt.show()
fig.savefig('3.png')
plt.close(fig)
# plotting 3 averaged runs, 10 times
def plot_means_4(returns_over_runs, runs, episodes):
ep_returns = []
indices = list(range(0,runs)) # list of integers from 0 to 29
i = 0
while i < 30:
ep_returns.append(list(np.mean(returns_over_runs[i:i+2], axis=0)))
i += 3
fig, ax = plt.subplots(1)
for means in ep_returns:
ax.plot(np.arange(1, episodes+1) , means, lw=0.5)
ax.set_title("Sampling and avergaing 3 runs (10 times)")
ax.set_xlabel("Episode")
ax.set_ylabel("Average Return (Sliding Window 100)")
# plt.show()
fig.savefig('4.png')
plt.close(fig)
# plotting 30 averaged runs with their standard error
def plot_means_5(data1, data2, runs, episodes):
ep_returns_means = []
ep_returns_stds = []
fig, ax = plt.subplots(1)
x = np.arange(1, episodes+1)
# averaging 30 plots
mean = np.mean(data1, axis=0)
std = np.std(data1, axis=0)/ np.sqrt(30)
y = sliding_window(mean, 100)
ax.plot(x, y, lw=1, color='red',label='Variant')
ax.fill_between(x, y-std , y+std, facecolor='red', alpha=0.2)
mean = np.mean(data2, axis=0)
std = np.std(data2, axis=0)/ np.sqrt(30)
y = sliding_window(mean, 100)
ax.plot(x, y, lw=1, color='blue', label='Baseline')
ax.fill_between(x, y-std , y+std, facecolor='blue', alpha=0.2)
# drawing all in one figure
ax.set_title("Episode Return")
ax.set_ylabel("Average Return (Sliding Window 100)")
ax.set_xlabel("Episode")
ax.set_title('Average runs with their standard error')
ax.legend(loc = 'lower right')
# plt.show()
fig.savefig('5.png')
plt.close(fig)
if __name__ == '__main__':
data1 = np.load('returns_30runs_variant.npy')
data2 = np.load('returns_30runs_baseline.npy')
# print(data.shape)
runs = 30
episodes = 2000
plot_means_1(data2, runs, episodes)
plot_means_2(data2, runs, episodes)
plot_means_3(data2, runs, episodes)
#
# plot_means_4(data2, runs, episodes)
plot_means_5(data1, data2, runs, episodes)