generated from rochacbruno/python-project-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
87 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from .action_selector import * | ||
|
||
|
||
class EpsilonSample(ActionSelector): | ||
|
||
def __init__(self, epsilon: float, min_epsilon: float, decay_factor: float, decay_steps: int): | ||
super().__init__() | ||
self.min_epsilon = min_epsilon | ||
self.decay_factor = decay_factor | ||
self.decay_steps = decay_steps | ||
self.epsilon = epsilon | ||
self.steps = 0 | ||
|
||
def __call__(self, distribution: torch.Tensor) -> Tuple[int, torch.Tensor]: | ||
self.decay() | ||
|
||
distribution = torch.atleast_1d(distribution) | ||
|
||
action = torch.argmax(distribution).item() | ||
policy = torch.zeros_like(distribution) + self.epsilon / distribution.size(0) | ||
policy[action] += 1 - self.epsilon | ||
|
||
if torch.rand(1) < self.epsilon: | ||
distribution = torch.distributions.Categorical(logits=distribution) | ||
|
||
return distribution.sample((1,)).item(), distribution.probs | ||
|
||
return action, policy | ||
|
||
def decay(self): | ||
self.steps += 1 | ||
|
||
if self.steps % self.decay_steps == 0: | ||
self.epsilon = max(self.epsilon * self.decay_factor, self.min_epsilon) | ||
|
||
self.log_info(f'Set exploration rate to { self.epsilon }. Total decay steps: { self.steps }') | ||
|
||
@staticmethod | ||
def from_cli(parameters: Dict): | ||
return EpsilonGreedy( | ||
epsilon=parameters['epsilon'], | ||
min_epsilon=parameters.get('min_epsilon', parameters['epsilon']), | ||
decay_factor=parameters.get('decay_factor', 1.0), | ||
decay_steps=parameters.get('decay_steps', 10000000) | ||
) |
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
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