-
Notifications
You must be signed in to change notification settings - Fork 371
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Former-commit-id: b6a37f5
- Loading branch information
Showing
6 changed files
with
115 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,86 @@ | ||
# | ||
""" | ||
TODO: docs | ||
""" | ||
|
||
from __future__ import absolute_import | ||
from __future__ import division | ||
from __future__ import print_function | ||
|
||
from texar.hyperparams import HParams | ||
|
||
# pylint: disable=invalid-name | ||
|
||
class ExplorationBase(object): | ||
"""Base class inherited by all exploration classes. | ||
Args: | ||
class ExplorationBase: | ||
""" | ||
def __init__(self, hparams=None): | ||
self._hparams = HParams(hparams, self.default_hparams()) | ||
|
||
@staticmethod | ||
def default_hparams(): | ||
"""Returns a dictionary of default hyperparameters. | ||
TODO: docs | ||
""" | ||
return { | ||
'name': 'exploration_base' | ||
} | ||
|
||
@property | ||
def epsilon(self): | ||
raise NotImplementedError | ||
def get_epsilon(self, timestep): | ||
"""Returns the epsilon value. | ||
def add_timestep(self): | ||
Args: | ||
timestep (int): The time step. | ||
Returns: | ||
float: the epsilon value. | ||
""" | ||
raise NotImplementedError | ||
|
||
@property | ||
def hparams(self): | ||
"""The hyperparameter. | ||
""" | ||
return self._hparams | ||
|
||
|
||
class EpsilonDecayExploration(ExplorationBase): | ||
class EpsilonLinearDecayExploration(ExplorationBase): | ||
"""TODO: docs | ||
Args: | ||
""" | ||
def __init__(self, hparams=None): | ||
ExplorationBase.__init__(self, hparams=hparams) | ||
self._epsilon = self._hparams.initial_epsilon | ||
self.timestep = 0 | ||
self.initial_epsilon = self._hparams.initial_epsilon | ||
self.final_epsilon = self._hparams.final_epsilon | ||
self.decay_steps = self._hparams.decay_steps | ||
|
||
@staticmethod | ||
def default_hparams(): | ||
"""TODO | ||
""" | ||
return { | ||
'name': 'epsilon_decay_exploration', | ||
'name': 'epsilon_linear_decay_exploration', | ||
'initial_epsilon': 0.1, | ||
'final_epsilon': 0.0, | ||
'decay_steps': 20000 | ||
'decay_timesteps': 20000, | ||
'start_timestep': 0 | ||
} | ||
|
||
def epsilon(self): | ||
return self._epsilon | ||
def get_epsilon(self, timestep): | ||
nsteps = self._hparams.decay_timesteps | ||
st = self._hparams.start_timestep | ||
et = st + nsteps | ||
|
||
if timestep <= st: | ||
return self._hparams.initial_epsilon | ||
if timestep > et: | ||
return self._hparams.final_epsilon | ||
|
||
r = (timestep - st) * 1.0 / nsteps | ||
epsilon = (1 - r) * self._hparams.initial_epsilon + \ | ||
r * self._hparams.final_epsilon | ||
|
||
def add_timestep(self): | ||
self.timestep += 1 | ||
self.epsilon_decay() | ||
return epsilon | ||
|
||
def epsilon_decay(self): | ||
if self._epsilon > 0.: | ||
self._epsilon -= (self.initial_epsilon - self.final_epsilon) / self.decay_steps |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters