Skip to content
New issue

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

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

Already on GitHub? # to your account

filtering a subset of parameters #939

Closed
lihebi opened this issue Nov 19, 2019 · 3 comments
Closed

filtering a subset of parameters #939

lihebi opened this issue Nov 19, 2019 · 3 comments

Comments

@lihebi
Copy link

lihebi commented Nov 19, 2019

Hi, a related question, is it possible to filter a subset of parameters? The use case is to apply L2 decay on weights but not bias.

@lihebi lihebi closed this as completed Nov 19, 2019
@lihebi
Copy link
Author

lihebi commented Nov 19, 2019

Sorry, miss-click

@MikeInnes
Copy link
Member

You should be able to use filter as normal if you just want to filter matrices vs. vectors. It'd be more robust if you just write a function to pull the weights out of dense layers explicitly, though.

@lihebi lihebi changed the title subset filtering a subset of parameters Nov 20, 2019
@lihebi
Copy link
Author

lihebi commented Nov 20, 2019

Thanks for the reply. This relates to #160, and I accidentally hit Ctrl-Enter way before I wanted.

The params returned by Flux.params() does not seem to contain information about which layers they come from. I managed to pull weights from dense and conv layers:

function weight_params(m::Chain, ps=Flux.Params())
    map((l)->weight_params(l, ps), m.layers)
    ps
end
weight_params(m::Dense, ps=Flux.Params()) = push!(ps, m.W)
weight_params(m::Conv, ps=Flux.Params()) = push!(ps, m.weight)
weight_params(m::ConvTranspose, ps=Flux.Params()) = push!(ps, m.weight)
weight_params(m, ps=Flux.Params()) = ps

bors bot added a commit that referenced this issue Mar 10, 2021
1444: define modules function r=CarloLucibello a=CarloLucibello

A function returning an iterator over non-leaf parameters. 
Mainly motivated by the need to apply L2 regularization to weights only (see #1284 , #939),
but may be of more general use. 

Fix #1294, Fix #1284

### PR Checklist

- [ ] Tests are added
- [ ] Entry in NEWS.md
- [x] Documentation, if applicable
- [ ] Final review from `@dhairyagandhi96` (for API changes).


Co-authored-by: Carlo Lucibello <carlo.lucibello@gmail.com>
Co-authored-by: Carlo Lucibello <carlo.lucibello@unibocconi.it>
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants