pytorch / rl

A modular, primitive-first, python-first PyTorch library for Reinforcement Learning.
https://pytorch.org/rl
MIT License
2.27k stars 302 forks source link

[BUG] Surprising `MLP` default behavior #2328

Closed chnyutao closed 2 months ago

chnyutao commented 2 months ago

Describe the bug

When depth and num_cells are both not specified, MLP will by default create a fully-connected neural network with three hidden layers, each with 32 neurons.

This is somewhat surprising, as I would imaging leaving depth and num_cells unspecified would lead to a neural network with no hidden layer at all. Not sure why use depth=3 and num_cells=32 as default here :)

This is not really a bug report, just IMHO a weird design choice which is also not documented. Would appreciate if we can discuss and reconsider this.

To Reproduce

>>> from torchrl.modules import MLP
>>> MLP(in_features=1024, out_features=512)
MLP(
  (0): Linear(in_features=1024, out_features=32, bias=True)
  (1): Tanh()
  (2): Linear(in_features=32, out_features=32, bias=True)
  (3): Tanh()
  (4): Linear(in_features=32, out_features=32, bias=True)
  (5): Tanh()
  (6): Linear(in_features=32, out_features=512, bias=True)
)

Expected behavior

>>> MLP(in_features=1024, out_features=512)
MLP(
  (0): Linear(in_features=1024, out_features=512, bias=True)
)

System info

Reason and Possible fixes

https://github.com/pytorch/rl/blob/0063741839a3e5e1a527947945494d54f91bc629/torchrl/modules/models/models.py#L182-L188

Checklist

vmoens commented 2 months ago

I agree, it's more a historical debt than anything meaningful. I will make a PR to progressively deprecate this. In the future, I can see two default behaviours for your example:

chnyutao commented 2 months ago

Thanks, I personally would prefer the former behavior (no depth implies depth=0).