mirror of
https://github.com/vale981/ray
synced 2025-03-06 10:31:39 -05:00

* Remove all __future__ imports from RLlib. * Remove (object) again from tf_run_builder.py::TFRunBuilder. * Fix 2xLINT warnings. * Fix broken appo_policy import (must be appo_tf_policy) * Remove future imports from all other ray files (not just RLlib). * Remove future imports from all other ray files (not just RLlib). * Remove future import blocks that contain `unicode_literals` as well. Revert appo_tf_policy.py to appo_policy.py (belongs to another PR). * Add two empty lines before Schedule class. * Put back __future__ imports into determine_tests_to_run.py. Fails otherwise on a py2/print related error.
84 lines
3.2 KiB
Python
84 lines
3.2 KiB
Python
import numpy as np
|
|
|
|
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."""
|
|
|
|
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")
|
|
|
|
# we are using obs_flat, so take the flattened shape as input
|
|
inputs = tf.keras.layers.Input(
|
|
shape=(np.product(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="fc_value_{}".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):
|
|
model_out, self._value_out = self.base_model(input_dict["obs_flat"])
|
|
return model_out, state
|
|
|
|
def value_function(self):
|
|
return tf.reshape(self._value_out, [-1])
|