2019-07-27 02:08:16 -07:00
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import division
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
from ray.rllib.models.tf.tf_modelv2 import TFModelV2
|
|
|
|
from ray.rllib.models.tf.misc import normc_initializer, get_activation_fn
|
|
|
|
from ray.rllib.utils import try_import_tf
|
|
|
|
|
|
|
|
tf = try_import_tf()
|
|
|
|
|
|
|
|
|
|
|
|
class FullyConnectedNetwork(TFModelV2):
|
|
|
|
"""Generic fully connected network implemented in ModelV2 API.
|
|
|
|
|
|
|
|
TODO(ekl): should make this the default fcnet in the future."""
|
|
|
|
|
|
|
|
def __init__(self, obs_space, action_space, num_outputs, model_config,
|
|
|
|
name):
|
|
|
|
super(FullyConnectedNetwork, self).__init__(
|
|
|
|
obs_space, action_space, num_outputs, model_config, name)
|
|
|
|
|
|
|
|
activation = get_activation_fn(model_config.get("fcnet_activation"))
|
|
|
|
hiddens = model_config.get("fcnet_hiddens")
|
|
|
|
no_final_linear = model_config.get("no_final_linear")
|
|
|
|
vf_share_layers = model_config.get("vf_share_layers")
|
|
|
|
|
|
|
|
inputs = tf.keras.layers.Input(
|
|
|
|
shape=obs_space.shape, name="observations")
|
|
|
|
last_layer = inputs
|
|
|
|
i = 1
|
|
|
|
|
|
|
|
if no_final_linear:
|
|
|
|
# the last layer is adjusted to be of size num_outputs
|
|
|
|
for size in hiddens[:-1]:
|
|
|
|
last_layer = tf.keras.layers.Dense(
|
|
|
|
size,
|
|
|
|
name="fc_{}".format(i),
|
|
|
|
activation=activation,
|
|
|
|
kernel_initializer=normc_initializer(1.0))(last_layer)
|
|
|
|
i += 1
|
|
|
|
layer_out = tf.keras.layers.Dense(
|
|
|
|
num_outputs,
|
|
|
|
name="fc_out",
|
|
|
|
activation=activation,
|
|
|
|
kernel_initializer=normc_initializer(1.0))(last_layer)
|
|
|
|
else:
|
|
|
|
# the last layer is a linear to size num_outputs
|
|
|
|
for size in hiddens:
|
|
|
|
last_layer = tf.keras.layers.Dense(
|
|
|
|
size,
|
|
|
|
name="fc_{}".format(i),
|
|
|
|
activation=activation,
|
|
|
|
kernel_initializer=normc_initializer(1.0))(last_layer)
|
|
|
|
i += 1
|
|
|
|
layer_out = tf.keras.layers.Dense(
|
|
|
|
num_outputs,
|
|
|
|
name="fc_out",
|
|
|
|
activation=None,
|
|
|
|
kernel_initializer=normc_initializer(0.01))(last_layer)
|
|
|
|
|
|
|
|
if not vf_share_layers:
|
|
|
|
# build a parallel set of hidden layers for the value net
|
|
|
|
last_layer = inputs
|
|
|
|
i = 1
|
|
|
|
for size in hiddens:
|
|
|
|
last_layer = tf.keras.layers.Dense(
|
|
|
|
size,
|
|
|
|
name="value_fc_{}".format(i),
|
|
|
|
activation=activation,
|
|
|
|
kernel_initializer=normc_initializer(1.0))(last_layer)
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
value_out = tf.keras.layers.Dense(
|
|
|
|
1,
|
|
|
|
name="value_out",
|
|
|
|
activation=None,
|
|
|
|
kernel_initializer=normc_initializer(0.01))(last_layer)
|
|
|
|
|
|
|
|
self.base_model = tf.keras.Model(inputs, [layer_out, value_out])
|
|
|
|
self.register_variables(self.base_model.variables)
|
|
|
|
|
|
|
|
def forward(self, input_dict, state, seq_lens):
|
2019-08-08 14:03:28 -07:00
|
|
|
model_out, self._value_out = self.base_model(input_dict["obs_flat"])
|
2019-07-27 02:08:16 -07:00
|
|
|
return model_out, state
|
|
|
|
|
|
|
|
def value_function(self):
|
|
|
|
return tf.reshape(self._value_out, [-1])
|