|
from typing import List, Dict, Any, Tuple, Union |
|
import copy |
|
import torch |
|
|
|
from ding.torch_utils import Adam, to_device |
|
from ding.rl_utils import iqn_nstep_td_data, iqn_nstep_td_error, get_train_sample, get_nstep_return_data |
|
from ding.model import model_wrap |
|
from ding.utils import POLICY_REGISTRY |
|
from ding.utils.data import default_collate, default_decollate |
|
from .dqn import DQNPolicy |
|
from .common_utils import default_preprocess_learn |
|
|
|
|
|
@POLICY_REGISTRY.register('iqn') |
|
class IQNPolicy(DQNPolicy): |
|
r""" |
|
Overview: |
|
Policy class of IQN algorithm. |
|
|
|
Config: |
|
== ==================== ======== ============== ======================================== ======================= |
|
ID Symbol Type Default Value Description Other(Shape) |
|
== ==================== ======== ============== ======================================== ======================= |
|
1 ``type`` str qrdqn | RL policy register name, refer to | this arg is optional, |
|
| registry ``POLICY_REGISTRY`` | a placeholder |
|
2 ``cuda`` bool False | Whether to use cuda for network | this arg can be diff- |
|
| erent from modes |
|
3 ``on_policy`` bool False | Whether the RL algorithm is on-policy |
|
| or off-policy |
|
4 ``priority`` bool True | Whether use priority(PER) | priority sample, |
|
| update priority |
|
6 | ``other.eps`` float 0.05 | Start value for epsilon decay. It's |
|
| ``.start`` | small because rainbow use noisy net. |
|
7 | ``other.eps`` float 0.05 | End value for epsilon decay. |
|
| ``.end`` |
|
8 | ``discount_`` float 0.97, | Reward's future discount factor, aka. | may be 1 when sparse |
|
| ``factor`` [0.95, 0.999] | gamma | reward env |
|
9 ``nstep`` int 3, | N-step reward discount sum for target |
|
[3, 5] | q_value estimation |
|
10 | ``learn.update`` int 3 | How many updates(iterations) to train | this args can be vary |
|
| ``per_collect`` | after collector's one collection. Only | from envs. Bigger val |
|
| valid in serial training | means more off-policy |
|
11 ``learn.kappa`` float / | Threshold of Huber loss |
|
== ==================== ======== ============== ======================================== ======================= |
|
""" |
|
|
|
config = dict( |
|
|
|
type='iqn', |
|
|
|
cuda=False, |
|
|
|
on_policy=False, |
|
|
|
priority=False, |
|
|
|
discount_factor=0.97, |
|
|
|
nstep=1, |
|
learn=dict( |
|
|
|
|
|
|
|
update_per_collect=3, |
|
batch_size=64, |
|
learning_rate=0.001, |
|
|
|
|
|
|
|
|
|
target_update_freq=100, |
|
|
|
kappa=1.0, |
|
|
|
ignore_done=False, |
|
), |
|
|
|
collect=dict( |
|
|
|
|
|
|
|
unroll_len=1, |
|
), |
|
eval=dict(), |
|
|
|
other=dict( |
|
|
|
eps=dict( |
|
|
|
type='exp', |
|
start=0.95, |
|
end=0.1, |
|
|
|
decay=10000, |
|
), |
|
replay_buffer=dict(replay_buffer_size=10000, ) |
|
), |
|
) |
|
|
|
def default_model(self) -> Tuple[str, List[str]]: |
|
return 'iqn', ['ding.model.template.q_learning'] |
|
|
|
def _init_learn(self) -> None: |
|
r""" |
|
Overview: |
|
Learn mode init method. Called by ``self.__init__``. |
|
Init the optimizer, algorithm config, main and target models. |
|
""" |
|
self._priority = self._cfg.priority |
|
|
|
self._optimizer = Adam(self._model.parameters(), lr=self._cfg.learn.learning_rate) |
|
|
|
self._gamma = self._cfg.discount_factor |
|
self._nstep = self._cfg.nstep |
|
self._kappa = self._cfg.learn.kappa |
|
|
|
|
|
self._target_model = copy.deepcopy(self._model) |
|
self._target_model = model_wrap( |
|
self._target_model, |
|
wrapper_name='target', |
|
update_type='assign', |
|
update_kwargs={'freq': self._cfg.learn.target_update_freq} |
|
) |
|
self._learn_model = model_wrap(self._model, wrapper_name='argmax_sample') |
|
self._learn_model.reset() |
|
self._target_model.reset() |
|
|
|
def _forward_learn(self, data: dict) -> Dict[str, Any]: |
|
r""" |
|
Overview: |
|
Forward and backward function of learn mode. |
|
Arguments: |
|
- data (:obj:`dict`): Dict type data, including at least ['obs', 'action', 'reward', 'next_obs'] |
|
Returns: |
|
- info_dict (:obj:`Dict[str, Any]`): Including current lr and loss. |
|
""" |
|
data = default_preprocess_learn( |
|
data, use_priority=self._priority, ignore_done=self._cfg.learn.ignore_done, use_nstep=True |
|
) |
|
if self._cuda: |
|
data = to_device(data, self._device) |
|
|
|
|
|
|
|
self._learn_model.train() |
|
self._target_model.train() |
|
|
|
ret = self._learn_model.forward(data['obs']) |
|
q_value = ret['q'] |
|
replay_quantiles = ret['quantiles'] |
|
|
|
with torch.no_grad(): |
|
target_q_value = self._target_model.forward(data['next_obs'])['q'] |
|
|
|
target_q_action = self._learn_model.forward(data['next_obs'])['action'] |
|
|
|
data_n = iqn_nstep_td_data( |
|
q_value, target_q_value, data['action'], target_q_action, data['reward'], data['done'], replay_quantiles, |
|
data['weight'] |
|
) |
|
value_gamma = data.get('value_gamma') |
|
loss, td_error_per_sample = iqn_nstep_td_error( |
|
data_n, self._gamma, nstep=self._nstep, kappa=self._kappa, value_gamma=value_gamma |
|
) |
|
|
|
|
|
|
|
|
|
self._optimizer.zero_grad() |
|
loss.backward() |
|
if self._cfg.multi_gpu: |
|
self.sync_gradients(self._learn_model) |
|
self._optimizer.step() |
|
|
|
|
|
|
|
|
|
self._target_model.update(self._learn_model.state_dict()) |
|
return { |
|
'cur_lr': self._optimizer.defaults['lr'], |
|
'total_loss': loss.item(), |
|
'priority': td_error_per_sample.abs().tolist(), |
|
|
|
|
|
} |
|
|
|
def _state_dict_learn(self) -> Dict[str, Any]: |
|
return { |
|
'model': self._learn_model.state_dict(), |
|
'target_model': self._target_model.state_dict(), |
|
'optimizer': self._optimizer.state_dict(), |
|
} |
|
|
|
def _load_state_dict_learn(self, state_dict: Dict[str, Any]) -> None: |
|
self._learn_model.load_state_dict(state_dict['model']) |
|
self._target_model.load_state_dict(state_dict['target_model']) |
|
self._optimizer.load_state_dict(state_dict['optimizer']) |
|
|