2018-10-21 23:43:57 -07:00
|
|
|
import logging
|
2019-01-03 13:48:33 +08:00
|
|
|
import numpy as np
|
2017-11-12 00:20:33 -08:00
|
|
|
|
2019-07-25 11:02:53 -07:00
|
|
|
from ray.rllib.models.torch.torch_modelv2 import TorchModelV2
|
2019-07-03 15:59:47 -07:00
|
|
|
from ray.rllib.models.torch.misc import normc_initializer, SlimFC, \
|
2019-01-03 13:48:33 +08:00
|
|
|
_get_activation_fn
|
|
|
|
from ray.rllib.utils.annotations import override
|
2019-12-30 15:27:32 -05:00
|
|
|
from ray.rllib.utils import try_import_torch
|
|
|
|
|
|
|
|
_, nn = try_import_torch()
|
2018-10-21 23:43:57 -07:00
|
|
|
|
2019-01-03 13:48:33 +08:00
|
|
|
logger = logging.getLogger(__name__)
|
2017-11-12 00:20:33 -08:00
|
|
|
|
2018-05-30 10:48:11 -07:00
|
|
|
|
2019-07-27 02:08:16 -07:00
|
|
|
class FullyConnectedNetwork(TorchModelV2, nn.Module):
|
2019-01-03 13:48:33 +08:00
|
|
|
"""Generic fully connected network."""
|
2017-11-12 00:20:33 -08:00
|
|
|
|
2019-07-25 11:02:53 -07:00
|
|
|
def __init__(self, obs_space, action_space, num_outputs, model_config,
|
|
|
|
name):
|
2019-07-27 02:08:16 -07:00
|
|
|
TorchModelV2.__init__(self, obs_space, action_space, num_outputs,
|
|
|
|
model_config, name)
|
|
|
|
nn.Module.__init__(self)
|
2019-07-25 11:02:53 -07:00
|
|
|
|
|
|
|
hiddens = model_config.get("fcnet_hiddens")
|
|
|
|
activation = _get_activation_fn(model_config.get("fcnet_activation"))
|
2019-01-03 13:48:33 +08:00
|
|
|
logger.debug("Constructing fcnet {} {}".format(hiddens, activation))
|
2017-11-12 00:20:33 -08:00
|
|
|
layers = []
|
2019-01-03 13:48:33 +08:00
|
|
|
last_layer_size = np.product(obs_space.shape)
|
2017-11-12 00:20:33 -08:00
|
|
|
for size in hiddens:
|
2018-05-30 10:48:11 -07:00
|
|
|
layers.append(
|
|
|
|
SlimFC(
|
|
|
|
in_size=last_layer_size,
|
|
|
|
out_size=size,
|
|
|
|
initializer=normc_initializer(1.0),
|
|
|
|
activation_fn=activation))
|
2017-11-12 00:20:33 -08:00
|
|
|
last_layer_size = size
|
|
|
|
|
2019-01-03 13:48:33 +08:00
|
|
|
self._hidden_layers = nn.Sequential(*layers)
|
2017-11-12 00:20:33 -08:00
|
|
|
|
2019-01-03 13:48:33 +08:00
|
|
|
self._logits = SlimFC(
|
2018-05-30 10:48:11 -07:00
|
|
|
in_size=last_layer_size,
|
|
|
|
out_size=num_outputs,
|
2017-11-12 00:20:33 -08:00
|
|
|
initializer=normc_initializer(0.01),
|
|
|
|
activation_fn=None)
|
2019-01-03 13:48:33 +08:00
|
|
|
self._value_branch = SlimFC(
|
2018-05-30 10:48:11 -07:00
|
|
|
in_size=last_layer_size,
|
|
|
|
out_size=1,
|
2017-11-12 00:20:33 -08:00
|
|
|
initializer=normc_initializer(1.0),
|
|
|
|
activation_fn=None)
|
2019-07-25 11:02:53 -07:00
|
|
|
self._cur_value = None
|
2017-11-12 00:20:33 -08:00
|
|
|
|
2019-07-25 11:02:53 -07:00
|
|
|
@override(TorchModelV2)
|
|
|
|
def forward(self, input_dict, state, seq_lens):
|
|
|
|
obs = input_dict["obs_flat"]
|
2019-01-03 13:48:33 +08:00
|
|
|
features = self._hidden_layers(obs.reshape(obs.shape[0], -1))
|
|
|
|
logits = self._logits(features)
|
2019-07-25 11:02:53 -07:00
|
|
|
self._cur_value = self._value_branch(features).squeeze(1)
|
|
|
|
return logits, state
|
|
|
|
|
|
|
|
@override(TorchModelV2)
|
|
|
|
def value_function(self):
|
|
|
|
assert self._cur_value is not None, "must call forward() first"
|
|
|
|
return self._cur_value
|