[rllib] Evaluators and Optimizers Refactoring (#1339)

This commit is contained in:
Richard Liaw
2017-12-30 00:24:54 -08:00
committed by GitHub
parent 22c7c87e14
commit 3304099cc4
28 changed files with 633 additions and 350 deletions
+9 -4
View File
@@ -23,6 +23,7 @@ class SampleBatch(object):
assert type(k) == str, self
lengths.append(len(v))
assert len(set(lengths)) == 1, "data columns must be same length"
self.count = lengths[0]
@staticmethod
def concat_samples(samples):
@@ -56,8 +57,7 @@ class SampleBatch(object):
{"a": 3, "b": 6}
"""
num_rows = len(list(self.data.values())[0])
for i in range(num_rows):
for i in range(self.count):
row = {}
for k in self.data.keys():
row[k] = self[k][i]
@@ -77,11 +77,16 @@ class SampleBatch(object):
out.append(self.data[k])
return out
def shuffle(self):
permutation = np.random.permutation(self.count)
for key, val in self.data.items():
self.data[key] = val[permutation]
def __getitem__(self, key):
return self.data[key]
def __str__(self):
return str(self.data)
return "SampleBatch({})".format(str(self.data))
def __repr__(self):
return str(self.data)
return "SampleBatch({})".format(str(self.data))
-33
View File
@@ -1,33 +0,0 @@
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import time
# TODO(ekl) move this to a common location
from ray.rllib.ppo.filter import RunningStat
class TimerStat(RunningStat):
"""A running stat for conveniently logging the duration of a code block.
Example:
wait_timer = TimeStat()
with wait_timer:
ray.wait(...)
Note that this class is *not* thread-safe.
"""
def __init__(self):
RunningStat.__init__(self, ())
self._start_time = None
def __enter__(self):
assert self._start_time is None, "concurrent updates not supported"
self._start_time = time.monotonic()
def __exit__(self, type, value, tb):
assert self._start_time is not None
self.push(time.monotonic() - self._start_time)
self._start_time = None