-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathkeras-rl_FileLogger_processing.py
executable file
·177 lines (132 loc) · 5.33 KB
/
keras-rl_FileLogger_processing.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
#! /usr/bin/env python
###############################################################################
# kerasRL_FileLogger_processing.py
#
# Script to process the json data generated by the FileLogger callback in
# the keras-rl python module
#
# NOTE: Any plotting is set up for output, not viewing on screen.
# So, it will likely be ugly on screen. The saved PDFs should look
# better.
#
# Created: 07/13/17
# - Joshua Vaughan
# - http://www.ucs.louisiana.edu/~jev9637
#
# Modified:
# *
#
# TODO:
# *
###############################################################################
import numpy as np
import matplotlib.pyplot as plt
import json # the data files generated are json
# TODO: 07/13/17 - JEV - Add GUI, argparse, or CLI for selecting file
FILENAME = 'logs/ddpg_planar_crane_continuous-v0_log_32_3_100000_2017-07-20_012022.json'
with open(FILENAME) as data_file:
data = json.load(data_file)
# This is the key data that we're interested in plotting. You can use the
# method data.keys() to see others
duration = np.array(data['duration'])
episode = np.array(data['episode'])
episode_reward = np.array(data['episode_reward'])
episode_length = np.array(data['nb_episode_steps'])
loss = np.array(data['loss'])
mean_abs_error = np.array(data['mean_absolute_error'])
mean_q = np.array(data['mean_q'])
episode_steps = np.array(data['nb_episode_steps'])
cumulative_steps = np.array(data['nb_steps'])
# time =
# ---- Plot evolution of reward over episodes --------------------------------
# Set the plot size - 3x2 aspect ratio is best
fig = plt.figure(figsize=(6,4))
ax = plt.gca()
plt.subplots_adjust(bottom=0.17, left=0.17, top=0.96, right=0.96)
# Change the axis units font
plt.setp(ax.get_ymajorticklabels(),fontsize=18)
plt.setp(ax.get_xmajorticklabels(),fontsize=18)
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
# Turn on the plot grid and set appropriate linestyle and color
ax.grid(True,linestyle=':', color='0.75')
ax.set_axisbelow(True)
# Define the X and Y axis labels
plt.xlabel('Episode', fontsize=22, weight='bold', labelpad=5)
plt.ylabel('Reward', fontsize=22, weight='bold', labelpad=10)
plt.plot(episode, episode_reward, linewidth=2, linestyle='-', label=r'Data 1')
# uncomment below and set limits if needed
# plt.xlim(0,5)
# plt.ylim(-1e4,1e3)
# Create the legend, then fix the fontsize
# leg = plt.legend(loc='upper right', ncol = 1, fancybox=True)
# ltext = leg.get_texts()
# plt.setp(ltext,fontsize=18)
# Adjust the page layout filling the page using the new tight_layout command
plt.tight_layout(pad=0.5)
# save the figure as a high-res pdf in the current folder
# plt.savefig('episode_reward.pdf')
# ---- Plot evolution of reward over episodes --------------------------------
# Set the plot size - 3x2 aspect ratio is best
fig = plt.figure(figsize=(6,4))
ax = plt.gca()
plt.subplots_adjust(bottom=0.17, left=0.17, top=0.96, right=0.96)
# Change the axis units font
plt.setp(ax.get_ymajorticklabels(),fontsize=18)
plt.setp(ax.get_xmajorticklabels(),fontsize=18)
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
# Turn on the plot grid and set appropriate linestyle and color
ax.grid(True,linestyle=':', color='0.75')
ax.set_axisbelow(True)
# Define the X and Y axis labels
plt.xlabel('Episode', fontsize=22, weight='bold', labelpad=5)
plt.ylabel('Loss', fontsize=22, weight='bold', labelpad=10)
plt.plot(episode, loss, linewidth=2, linestyle='-', label=r'Loss')
# uncomment below and set limits if needed
# plt.xlim(0,5)
# plt.ylim(-1e4,1e3)
# Create the legend, then fix the fontsize
# leg = plt.legend(loc='upper right', ncol = 1, fancybox=True)
# ltext = leg.get_texts()
# plt.setp(ltext,fontsize=18)
# Adjust the page layout filling the page using the new tight_layout command
plt.tight_layout(pad=0.5)
# save the figure as a high-res pdf in the current folder
# plt.savefig('episode_loss.pdf')
# ---- Plot evolution of reward over episodes --------------------------------
# Set the plot size - 3x2 aspect ratio is best
fig = plt.figure(figsize=(6,4))
ax = plt.gca()
plt.subplots_adjust(bottom=0.17, left=0.17, top=0.96, right=0.96)
# Change the axis units font
plt.setp(ax.get_ymajorticklabels(),fontsize=18)
plt.setp(ax.get_xmajorticklabels(),fontsize=18)
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
# Turn on the plot grid and set appropriate linestyle and color
ax.grid(True,linestyle=':', color='0.75')
ax.set_axisbelow(True)
# Define the X and Y axis labels
plt.xlabel('Episode', fontsize=22, weight='bold', labelpad=5)
plt.ylabel('Mean($Q$)', fontsize=22, weight='bold', labelpad=10)
plt.plot(episode, mean_q, linewidth=2, linestyle='-', label=r'Loss')
# uncomment below and set limits if needed
# plt.xlim(0,5)
# plt.ylim(-1e4,1e3)
# Create the legend, then fix the fontsize
# leg = plt.legend(loc='upper right', ncol = 1, fancybox=True)
# ltext = leg.get_texts()
# plt.setp(ltext,fontsize=18)
# Adjust the page layout filling the page using the new tight_layout command
plt.tight_layout(pad=0.5)
# save the figure as a high-res pdf in the current folder
# plt.savefig('episode_meanQ.pdf')
plt.show()