2017-07-17 01:58:54 -07:00
|
|
|
from ray.rllib.models.model import Model
|
2020-04-15 13:25:16 +02:00
|
|
|
from ray.rllib.models.tf.misc import normc_initializer
|
2018-12-08 16:28:58 -08:00
|
|
|
from ray.rllib.utils.annotations import override
|
2020-04-29 12:12:59 +02:00
|
|
|
from ray.rllib.utils.deprecation import deprecation_warning
|
2020-04-15 13:25:16 +02:00
|
|
|
from ray.rllib.utils.framework import get_activation_fn, try_import_tf
|
2019-05-10 20:36:18 -07:00
|
|
|
|
|
|
|
tf = try_import_tf()
|
2017-07-17 01:58:54 -07:00
|
|
|
|
|
|
|
|
2019-07-27 02:08:16 -07:00
|
|
|
# Deprecated: see as an alternative models/tf/fcnet_v2.py
|
2017-07-17 01:58:54 -07:00
|
|
|
class FullyConnectedNetwork(Model):
|
2017-08-24 12:43:51 -07:00
|
|
|
"""Generic fully connected network."""
|
2017-07-17 01:58:54 -07:00
|
|
|
|
2018-12-08 16:28:58 -08:00
|
|
|
@override(Model)
|
2018-06-27 22:51:04 -07:00
|
|
|
def _build_layers(self, inputs, num_outputs, options):
|
2018-10-20 15:21:22 -07:00
|
|
|
"""Process the flattened inputs.
|
|
|
|
|
|
|
|
Note that dict inputs will be flattened into a vector. To define a
|
|
|
|
model that processes the components separately, use _build_layers_v2().
|
|
|
|
"""
|
2020-04-29 12:12:59 +02:00
|
|
|
# Soft deprecate this class. All Models should use the ModelV2
|
|
|
|
# API from here on.
|
|
|
|
deprecation_warning(
|
|
|
|
"Model->FullyConnectedNetwork",
|
|
|
|
"ModelV2->FullyConnectedNetwork",
|
|
|
|
error=False)
|
2018-10-20 15:21:22 -07:00
|
|
|
|
2018-10-16 15:55:11 -07:00
|
|
|
hiddens = options.get("fcnet_hiddens")
|
|
|
|
activation = get_activation_fn(options.get("fcnet_activation"))
|
2017-07-26 12:29:00 -07:00
|
|
|
|
2019-09-19 12:10:31 -07:00
|
|
|
if len(inputs.shape) > 2:
|
|
|
|
inputs = tf.layers.flatten(inputs)
|
|
|
|
|
2017-07-17 01:58:54 -07:00
|
|
|
with tf.name_scope("fc_net"):
|
2017-07-26 12:29:00 -07:00
|
|
|
i = 1
|
|
|
|
last_layer = inputs
|
|
|
|
for size in hiddens:
|
2019-07-03 15:59:47 -07:00
|
|
|
# skip final linear layer
|
|
|
|
if options.get("no_final_linear") and i == len(hiddens):
|
|
|
|
output = tf.layers.dense(
|
|
|
|
last_layer,
|
|
|
|
num_outputs,
|
|
|
|
kernel_initializer=normc_initializer(1.0),
|
|
|
|
activation=activation,
|
|
|
|
name="fc_out")
|
|
|
|
return output, output
|
|
|
|
|
2018-01-18 19:51:31 -08:00
|
|
|
label = "fc{}".format(i)
|
2019-05-16 22:12:07 -07:00
|
|
|
last_layer = tf.layers.dense(
|
2018-07-19 15:30:36 -07:00
|
|
|
last_layer,
|
|
|
|
size,
|
2019-05-16 22:12:07 -07:00
|
|
|
kernel_initializer=normc_initializer(1.0),
|
|
|
|
activation=activation,
|
|
|
|
name=label)
|
2017-07-26 12:29:00 -07:00
|
|
|
i += 1
|
2019-07-03 15:59:47 -07:00
|
|
|
|
2019-05-16 22:12:07 -07:00
|
|
|
output = tf.layers.dense(
|
2018-07-19 15:30:36 -07:00
|
|
|
last_layer,
|
|
|
|
num_outputs,
|
2019-05-16 22:12:07 -07:00
|
|
|
kernel_initializer=normc_initializer(0.01),
|
|
|
|
activation=None,
|
2019-07-03 15:59:47 -07:00
|
|
|
name="fc_out")
|
2017-07-26 12:29:00 -07:00
|
|
|
return output, last_layer
|