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

How to use l2 regularization and dropout to prevent overfitting? #541

Closed
jaried opened this issue Mar 1, 2022 · 4 comments
Closed

How to use l2 regularization and dropout to prevent overfitting? #541

jaried opened this issue Mar 1, 2022 · 4 comments
Labels
question Further information is requested

Comments

@jaried
Copy link

jaried commented Mar 1, 2022

  • I have visited the source website
  • I have searched through the issue tracker for duplicates
  • I have mentioned version numbers, operating system and environment, where applicable:
tianshou:0.4.6.post1, torch:1.10.2+cu113, numpy:1.21.5, sys:3.7.0 (default, Jun 28 2018, 08:04:48) [MSC v.1912 64 bit (AMD64)], platform:win32

I solved the CartPole problem with FQF in legacy mode, and with your help, I have been able to solve it.
Other questions, how to add l2 regularization or dropout to prevent overfitting?

  1. I see that there is norm_layer in the MLP of net. Is l2 regularization added here?
  2. How to add dropout?
.\tianshou\utils\net\common.py
line 51-60
    def __init__(
        self,
        input_dim: int,
        output_dim: int = 0,
        hidden_sizes: Sequence[int] = (),
        norm_layer: Optional[Union[ModuleType, Sequence[ModuleType]]] = None,
        activation: Optional[Union[ModuleType, Sequence[ModuleType]]] = nn.ReLU,
        device: Optional[Union[str, int, torch.device]] = None,
        linear_layer: Type[nn.Linear] = nn.Linear,
    ) -> None:
@Trinkle23897

This comment was marked as spam.

@Trinkle23897 Trinkle23897 added the enhancement Feature that is not a new algorithm or an algorithm enhancement label Mar 1, 2022
@nuance1979
Copy link
Collaborator

nuance1979 commented Mar 1, 2022

  1. I see that there is norm_layer in the MLP of net. Is l2 regularization added here?

No. In PyTorch, L2 regularization (aka, "weight decay") is a function of the optimizer. See the following code for a proper example, which doesn't add L2-reg/weight decay to biases or norm layer weights:

https://github.com/huggingface/transformers/blob/master/examples/pytorch/language-modeling/run_clm_no_trainer.py#L417-L428

  1. How to add dropout?

Dropout can be added by MLP(..., norm_layer=nn.Dropout(0.1)).

@jaried
Copy link
Author

jaried commented Mar 2, 2022

Thanks for your answer. This works great for me.

@jaried jaried closed this as completed Mar 2, 2022
@Trinkle23897 Trinkle23897 added question Further information is requested and removed enhancement Feature that is not a new algorithm or an algorithm enhancement labels Mar 2, 2022
@jaried
Copy link
Author

jaried commented Mar 4, 2022

I got a reply from someone today that RL doesn't need to prevent overfitting, because RL requires accuracy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants