Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add with_bias parameter to create_mlp #1188

Merged
merged 6 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/misc/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Breaking Changes:
New Features:
^^^^^^^^^^^^^
- Introduced mypy type checking
- Added ``with_bias`` argument to ``create_mlp``

SB3-Contrib
^^^^^^^^^^^
Expand Down
8 changes: 5 additions & 3 deletions stable_baselines3/common/torch_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def create_mlp(
output_dim: int,
net_arch: List[int],
activation_fn: Type[nn.Module] = nn.ReLU,
with_bias: bool = True,
qgallouedec marked this conversation as resolved.
Show resolved Hide resolved
squash_output: bool = False,
) -> List[nn.Module]:
"""
Expand All @@ -111,23 +112,24 @@ def create_mlp(
The length of this list is the number of layers.
:param activation_fn: The activation function
to use after each layer.
:param with_bias: If set to False, the layers will not learn an additive bias
:param squash_output: Whether to squash the output using a Tanh
activation function
:return:
"""

if len(net_arch) > 0:
modules = [nn.Linear(input_dim, net_arch[0]), activation_fn()]
modules = [nn.Linear(input_dim, net_arch[0], bias=with_bias), activation_fn()]
else:
modules = []

for idx in range(len(net_arch) - 1):
modules.append(nn.Linear(net_arch[idx], net_arch[idx + 1]))
modules.append(nn.Linear(net_arch[idx], net_arch[idx + 1], bias=with_bias))
modules.append(activation_fn())

if output_dim > 0:
last_layer_dim = net_arch[-1] if len(net_arch) > 0 else input_dim
modules.append(nn.Linear(last_layer_dim, output_dim))
modules.append(nn.Linear(last_layer_dim, output_dim, bias=with_bias))
if squash_output:
modules.append(nn.Tanh())
return modules
Expand Down