# Feature Request: Consistent Dropout Implementation

**URL:** <https://discuss.pytorch.org/t/feature-request-consistent-dropout-implementation/183778>\
**Category:** reinforcement-learning\
**Created:** [July 9, 2023, 11:32am UTC](https://discuss.pytorch.org/t/feature-request-consistent-dropout-implementation/183778 "2023-07-09T11:32:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![J\_Johnson](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/j_johnson/32/55494_2.png) [@J\_Johnson](https://discuss.pytorch.org/u/J_Johnson)\
**Post date:** [July 9, 2023, 11:32am UTC](https://discuss.pytorch.org/t/feature-request-consistent-dropout-implementation/183778/1 "2023-07-09T11:32:45Z")

</div>

I would like to put in a feature request to implement Consistent Dropout, as described in this paper:

> **[Consistent Dropout for Policy Gradient Reinforcement Learning](https://arxiv.org/abs/2202.11818)**
>
> Dropout has long been a staple of supervised learning, but is rarely used in
> reinforcement learning. We analyze why naive application of dropout is
> problematic for policy-gradient learning algorithms and introduce consistent
> dropout, a simple...

**Background**

Reinforcement Learning typically does not use dropout layers as they cause instability.

What the above paper found, though, is fixing the dropout masks to be unchanged within each episode provided stable training and improved overall performance for RL networks vs. no dropout layers.

**Implementation**

One approach could be to supply an argument in `nn.Dropout` when instantiating called `freeze` which defaults to `False`.

If `freeze = True`, the masks stay fixed on those dropout layers until a manual `model.dropout_reset()` method is called.

Thank you.

---

<div class="post-metadata">

**Author:** ![vmoens](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/vmoens/32/26121_2.png) [@vmoens](https://discuss.pytorch.org/u/vmoens)\
**Post date:** [July 9, 2023, 11:44am UTC](https://discuss.pytorch.org/t/feature-request-consistent-dropout-implementation/183778/2 "2023-07-09T11:44:45Z")

</div>

Thanks for raising this.  
Indeed it would be a pretty neat feature to have.  
I think this would nicely fit in TorchRL (rather than torch core) as my understanding is that it is quite RL specific.  
Wdyt?

---

<div class="post-metadata">

**Author:** ![J\_Johnson](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/j_johnson/32/55494_2.png) [@J\_Johnson](https://discuss.pytorch.org/u/J_Johnson)\
**Post date:** [July 9, 2023, 12:17pm UTC](https://discuss.pytorch.org/t/feature-request-consistent-dropout-implementation/183778/3 "2023-07-09T12:17:43Z")

</div>

@vmoens While I think the paper demonstrates it’s usefulness in RL, I don’t see why it wouldn’t be a potential improvement in other time sequence prediction networks, such as RNNs, for the same reasons.

---

<div class="post-metadata">

**Author:** ![vmoens](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/vmoens/32/26121_2.png) [@vmoens](https://discuss.pytorch.org/u/vmoens)\
**Post date:** [July 10, 2023, 6:30pm UTC](https://discuss.pytorch.org/t/feature-request-consistent-dropout-implementation/183778/4 "2023-07-10T18:30:00Z")

</div>

The reason I was suggesting TorchRL is that we make sure that this sort of traj-consistent modules are easy to code and well standardized / tested / robust and that the proper errors / warnings are raised whenever you use them.

If you think this should be a pytorch core feature under torch.nn, feel free to open an issue on pytorch github. Given the scope of the feature, my 2 cents is that the answer will be that this is more suited for a domain library.

---

<div class="post-metadata">

**Author:** ![vmoens](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/vmoens/32/26121_2.png) [@vmoens](https://discuss.pytorch.org/u/vmoens)\
**Post date:** [September 10, 2024, 8:25am UTC](https://discuss.pytorch.org/t/feature-request-consistent-dropout-implementation/183778/5 "2024-09-10T08:25:22Z")

</div>

We merged this in torchrl.  
You can use the layer independently or within a torchrl script, here’s an example:

```python
            >>> from torchrl.modules import ConsistentDropoutModule
            >>> from tensordict.nn import TensorDictSequential as Seq, TensorDictModule as Mod
            >>> from torchrl.envs import GymEnv, StepCounter, SerialEnv
            >>> m = Seq(
            ... Mod(torch.nn.Linear(7, 4), in_keys=["observation"], out_keys=["intermediate"]),
            ... ConsistentDropoutModule(
            ... p=0.5,
            ... input_shape=(2, 4),
            ... in_keys="intermediate",
            ... ),
            ... Mod(torch.nn.Linear(4, 7), in_keys=["intermediate"], out_keys=["action"]),
            ... )
            >>> primer = get_primers_from_module(m)
            >>> env0 = GymEnv("Pendulum-v1").append_transform(StepCounter(5))
            >>> env1 = GymEnv("Pendulum-v1").append_transform(StepCounter(6))
            >>> env = SerialEnv(2, [lambda env=env0: env, lambda env=env1: env])
            >>> env = env.append_transform(primer)
            >>> r = env.rollout(10, m, break_when_any_done=False)
            >>> mask = [k for k in r.keys() if k.startswith("mask")][0]
            >>> assert (r[mask][0, :5] != r[mask][0, 5:6]).any()
            >>> assert (r[mask][0, :4] == r[mask][0, 4:5]).all()

```

> <https://github.com/pytorch/rl/pull/2399>
>
> \## Description
> 
> Introduces \[Consistent Dropout (Hausknecht & Wagener, 2022)\](h…ttps://arxiv.org/abs/2202.11818) to TorchRL.
> 
> Consists of the following changes: 
> \- \`ConsistentDropout\` PyTorch module 
> \- Companion \`TensorDictModule\`: \`ConsistentDropoutModule\`
> \- Tests for \`ConsistentDropoutModule\`
> 
> \## Motivation and Context
> 
> Addresses a \[feature request\](https://discuss.pytorch.org/t/feature-request-consistent-dropout-implementation/183778).
> Fleshes out a PR draft #1587.
> 
> \## Types of changes
> 
> What types of changes does your code introduce? Remove all that do not apply:
> 
> \- \[\] Bug fix (non-breaking change which fixes an issue)
> \- \[x\] New feature (non-breaking change which adds core functionality)
> \- \[\] Breaking change (fix or feature that would cause existing functionality to change)
> \- \[x\] Documentation (update in the documentation)
> \- \[\] Example (update in the folder of examples)
> 
> \## Checklist
> 
> Go over all the following points, and put an \`x\` in all the boxes that apply.
> If you are unsure about any of these, don't hesitate to ask. We are here to help!
> 
> \- \[x\] I have read the \[CONTRIBUTION\](https://github.com/pytorch/rl/blob/main/CONTRIBUTING.md) guide (\*\*required\*\*)
> \- \[x\] My change requires a change to the documentation.
> \- \[x\] I have updated the tests accordingly (\*required for a bug fix or a new feature\*).
> \- \[x\] I have updated the documentation accordingly.
