From e733b13a8f67ab86d8869dbd3bc05674f32536f9 Mon Sep 17 00:00:00 2001 From: Shangtong Zhang Date: Sat, 21 Apr 2018 10:26:44 -0600 Subject: [PATCH] Update plotter and scheduler --- utils/__init__.py | 1 + utils/plot.py | 7 ++++--- utils/schedule.py | 27 +++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 utils/schedule.py diff --git a/utils/__init__.py b/utils/__init__.py index 0b89911..366f00a 100644 --- a/utils/__init__.py +++ b/utils/__init__.py @@ -3,6 +3,7 @@ from .normalizer import * from .misc import * from .tf_logger import Logger from .plot import Plotter +from .schedule import * import logging logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s: %(message)s') logger = logging.getLogger('MAIN') diff --git a/utils/plot.py b/utils/plot.py index 2a62cfe..62c75d3 100644 --- a/utils/plot.py +++ b/utils/plot.py @@ -46,16 +46,17 @@ class Plotter: ts = ts[ts.l.cumsum() <= max_timesteps] tslist.append(ts) xy_list = [self.ts2xy(ts, x_axis) for ts in tslist] - xy_list = [[x, y, self.window_func(x, y, episode_window, np.mean)] for x, y in xy_list] + if episode_window: + xy_list = [self.window_func(x, y, episode_window, np.mean) for x, y in xy_list] return xy_list def plot_results(self, dirs, max_timesteps=1e8, x_axis=X_TIMESTEPS, episode_window=100, title=None): import matplotlib.pyplot as plt plt.ticklabel_format(axis='x', style='sci', scilimits=(1, 1)) xy_list = self.load_results(dirs, max_timesteps, x_axis, episode_window) - for (i, (x, y, smoothed)) in enumerate(xy_list): + for (i, (x, y)) in enumerate(xy_list): color = Plotter.COLORS[i] - plt.plot(smoothed[0], smoothed[1], color=color) + plt.plot(x, y, color=color) plt.xlabel(x_axis) plt.ylabel("Episode Rewards") if title is not None: diff --git a/utils/schedule.py b/utils/schedule.py new file mode 100644 index 0000000..7a191e0 --- /dev/null +++ b/utils/schedule.py @@ -0,0 +1,27 @@ +####################################################################### +# Copyright (C) 2017 Shangtong Zhang(zhangshangtong.cpp@gmail.com) # +# Permission given to modify the code as long as you keep this # +# declaration at the top # +####################################################################### + +class ConstantSchedule: + def __init__(self, val): + self.val = val + + def __call__(self): + return self.val + +class LinearSchedule: + def __init__(self, start, end, steps): + self.inc = (end - start) / float(steps) + self.current = start + self.end = end + if end > start: + self.bound = min + else: + self.bound = max + + def __call__(self): + val = self.current + self.current = self.bound(self.current + self.inc, self.end) + return val