Skip to content

Commit 30bf8e4

Browse files
authored
[rllib] Use nested scope in custom loss example
1 parent df9beb7 commit 30bf8e4

File tree

3 files changed

+14
-15
lines changed

3 files changed

+14
-15
lines changed

ci/jenkins_tests/run_rllib_tests.sh

+3
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,9 @@ docker run --rm --shm-size=${SHM_SIZE} --memory=${MEMORY_SIZE} $DOCKER_SHA \
353353
docker run --rm --shm-size=${SHM_SIZE} --memory=${MEMORY_SIZE} $DOCKER_SHA \
354354
/ray/python/ray/rllib/tests/run_silent.sh examples/cartpole_lstm.py --stop=200 --use-prev-action-reward
355355

356+
docker run --rm --shm-size=${SHM_SIZE} --memory=${MEMORY_SIZE} $DOCKER_SHA \
357+
/ray/python/ray/rllib/tests/run_silent.sh examples/custom_loss.py --iters=2
358+
356359
docker run --rm --shm-size=${SHM_SIZE} --memory=${MEMORY_SIZE} $DOCKER_SHA \
357360
/ray/python/ray/rllib/tests/run_silent.sh examples/custom_metrics_and_callbacks.py --num-iters=2
358361

python/ray/rllib/examples/custom_loss.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,17 @@
3131
type=str,
3232
default=os.path.join(
3333
os.path.dirname(os.path.abspath(__file__)),
34-
"../test/data/cartpole_small"))
34+
"../tests/data/cartpole_small"))
3535

3636

3737
class CustomLossModel(Model):
3838
"""Custom model that adds an imitation loss on top of the policy loss."""
3939

4040
def _build_layers_v2(self, input_dict, num_outputs, options):
4141
self.obs_in = input_dict["obs"]
42-
self.fcnet = FullyConnectedNetwork(input_dict, self.obs_space,
43-
num_outputs, options)
42+
with tf.variable_scope("shared", reuse=tf.AUTO_REUSE):
43+
self.fcnet = FullyConnectedNetwork(input_dict, self.obs_space,
44+
num_outputs, options)
4445
return self.fcnet.outputs, self.fcnet.last_layer
4546

4647
def custom_loss(self, policy_loss, loss_inputs):
@@ -49,12 +50,10 @@ def custom_loss(self, policy_loss, loss_inputs):
4950
input_ops = reader.tf_input_ops()
5051

5152
# define a secondary loss by building a graph copy with weight sharing
52-
with tf.variable_scope(
53-
self.scope, reuse=tf.AUTO_REUSE, auxiliary_name_scope=False):
54-
logits, _ = self._build_layers_v2({
55-
"obs": restore_original_dimensions(input_ops["obs"],
56-
self.obs_space)
57-
}, self.num_outputs, self.options)
53+
logits, _ = self._build_layers_v2({
54+
"obs": restore_original_dimensions(input_ops["obs"],
55+
self.obs_space)
56+
}, self.num_outputs, self.options)
5857

5958
# You can also add self-supervised losses easily by referencing tensors
6059
# created during _build_layers_v2(). For example, an autoencoder-style

python/ray/rllib/offline/input_reader.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,9 @@ def tf_input_ops(self, queue_size=1):
4545
... def custom_loss(self, policy_loss, loss_inputs):
4646
... reader = JsonReader(...)
4747
... input_ops = reader.tf_input_ops()
48-
... with tf.variable_scope(
49-
... self.scope, reuse=tf.AUTO_REUSE,
50-
... auxiliary_name_scope=False):
51-
... logits, _ = self._build_layers_v2(
52-
... {"obs": input_ops["obs"]},
53-
... self.num_outputs, self.options)
48+
... logits, _ = self._build_layers_v2(
49+
... {"obs": input_ops["obs"]},
50+
... self.num_outputs, self.options)
5451
... il_loss = imitation_loss(logits, input_ops["action"])
5552
... return policy_loss + il_loss
5653

0 commit comments

Comments
 (0)