-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualization.py
129 lines (99 loc) · 3.89 KB
/
visualization.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
import re
import os
import sys
import numpy as np
import cv2
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
def visualize(state: np.ndarray, enlarging_factor=5) -> None:
"""
Visualize the state of the game using OpenCV
:param state: state of the game
:param enlarging_factor: factor to enlarge the state for better visualization
:return: None
"""
# Remove the batch dimension
state = np.squeeze(state)
size = 84 * enlarging_factor
# get the most recent frame
state = state[..., -1]
resized_frame = cv2.resize(state, (size, size))
color_frame = np.zeros((size, size, 3), dtype=np.uint8)
# Set yellow color for the ball and paddle
color_frame[resized_frame > 0] = [28, 184, 255]
# Set purple color for the background
color_frame[resized_frame == 0] = [75, 0, 88]
cv2.imshow('Catch Game', color_frame)
cv2.waitKey(50) # Adjust the delay between frames if needed (in milliseconds)
def destroy() -> None:
"""
Destroy all windows
:return: None
"""
cv2.destroyAllWindows()
def plot_running_average_all_files():
directory = 'performances/default_hyperparameter_tests/'
window_size = 100
all_scores = []
plt.figure(figsize=(7, 5), dpi=200)
for filename in os.listdir(directory):
file_path = os.path.join(directory, filename)
data = pd.read_csv(file_path)
scores = data['score']
if len(scores) >= 1500:
# if filename == 'learningRate=0.001_batchSize=128_memorySize=5000_prioritizedMemory=True_lrs=True_smartReward=True_betaIncrement=Truedueling=True_double=True_gradientClipping=True_09:28.csv':
running_avg = scores.rolling(window=window_size, min_periods=1).mean()
if running_avg[100] > 0.40:
all_scores.append(scores)
plt.plot(running_avg, linewidth=0.8)
# Calculate the mean of all scores
mean_scores = np.mean(all_scores, axis=0)
# Calculate the rolling mean of the mean scores
mean_rolling_mean = pd.Series(mean_scores).rolling(window=100, min_periods=1).mean()
# Plot the mean line
plt.plot(mean_rolling_mean, linewidth=1.2, color='black', label='Mean')
# dense grid
plt.grid(True)
plt.yticks(np.arange(0, 1.1, 0.1))
# Adjust the legend with a smaller font size and position outside the plot
plt.legend()
plt.ylim(0, 1)
plt.xlim(0, 2500)
plt.ylabel("Avg Reward")
plt.title("Rolling mean of 100 episodes")
plt.xlabel("Episode")
plt.show()
def plot_10_episode_policy_evaluation_all_files():
"""
plot the 10 episode policy evaluation for all files in the directory.
These are already averages of 10 episodes, so no need to do a running average.
"""
directory = 'performances/10_episode_policy_evaluations/'
all_scores = []
for filename in os.listdir(directory):
if filename == "testScore_23:00.csv" or filename == "testScore_14:27.csv":
file_path = os.path.join(directory, filename)
data = pd.read_csv(file_path)
scores = data['test_score']
print(np.mean(scores), filename)
all_scores.append(scores)
plt.plot(scores, linewidth=0.5)
# Calculate the mean of all scores
mean_scores = np.mean(all_scores, axis=0)
# Calculate the rolling mean of the mean scores
mean_rolling_mean = pd.Series(mean_scores).rolling(window=10).mean()
# Plot the mean line
plt.plot(mean_rolling_mean, linewidth=2, color='black', label='Mean')
# dense grid
plt.grid(True)
plt.yticks(np.arange(0, 1.1, 0.1))
plt.ylabel("Reward")
plt.xlabel("Epoch (10 episodes)")
plt.title("Performance policy during training")
# Adjust the legend with a smaller font size and position outside the plot
plt.legend()
plt.ylim(0, 1)
plt.show()
plot_running_average_all_files()
# plot_10_episode_policy_evaluation_all_files()