This repository has been archived by the owner on Dec 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 462
/
parameter_noise.py
91 lines (72 loc) · 3.97 KB
/
parameter_noise.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
#
# Copyright (c) 2017 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from typing import List, Dict
import numpy as np
from rl_coach.architectures.layers import NoisyNetDense
from rl_coach.base_parameters import AgentParameters, NetworkParameters
from rl_coach.spaces import ActionSpace, BoxActionSpace, DiscreteActionSpace
from rl_coach.core_types import ActionType
from rl_coach.exploration_policies.exploration_policy import ExplorationPolicy, ExplorationParameters
class ParameterNoiseParameters(ExplorationParameters):
def __init__(self, agent_params: AgentParameters):
super().__init__()
if not agent_params.algorithm.supports_parameter_noise:
raise ValueError("Currently only DQN variants are supported for using an exploration type of "
"ParameterNoise.")
self.network_params = agent_params.network_wrappers
@property
def path(self):
return 'rl_coach.exploration_policies.parameter_noise:ParameterNoise'
class ParameterNoise(ExplorationPolicy):
"""
The ParameterNoise exploration policy is intended for both discrete and continuous action spaces.
It applies the exploration policy by replacing all the dense network layers with noisy layers.
The noisy layers have both weight means and weight standard deviations, and for each forward pass of the network
the weights are sampled from a normal distribution that follows the learned weights mean and standard deviation
values.
Warning: currently supported only by DQN variants
"""
def __init__(self, network_params: Dict[str, NetworkParameters], action_space: ActionSpace):
"""
:param action_space: the action space used by the environment
"""
super().__init__(action_space)
self.network_params = network_params
self._replace_network_dense_layers()
def get_action(self, action_values: List[ActionType]):
if type(self.action_space) == DiscreteActionSpace:
action = np.argmax(action_values)
one_hot_action_probabilities = np.zeros(len(self.action_space.actions))
one_hot_action_probabilities[action] = 1
return action, one_hot_action_probabilities
elif type(self.action_space) == BoxActionSpace:
action_values_mean = action_values[0].squeeze()
action_values_std = action_values[1].squeeze()
return np.random.normal(action_values_mean, action_values_std)
else:
raise ValueError("ActionSpace type {} is not supported for ParameterNoise.".format(type(self.action_space)))
def get_control_param(self):
return 0
def _replace_network_dense_layers(self):
# replace the dense type for all the networks components (embedders, mw, heads) with a NoisyNetDense
# NOTE: we are changing network params in a non-params class (an already instantiated class), this could have
# been prone to a bug, but since the networks are created very late in the game
# (after agent.init_environment_dependent()_modules is called) - then we are fine.
for network_wrapper_params in self.network_params.values():
for component_params in list(network_wrapper_params.input_embedders_parameters.values()) + \
[network_wrapper_params.middleware_parameters] + \
network_wrapper_params.heads_parameters:
component_params.dense_layer = NoisyNetDense