Skip to content

add namedparams #1144

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions docs/src/models/regularisation.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ julia> sum(norm, ans)
2.1166067f0
```

One can also add regularisation only for weights by filtering out biases using `namedparams`.

```
ps = [p for (p, name) in Flux.namedparams(m) if !occursin(r"^b", name)]
loss(x, y) = crossentropy(m(x), y) + sum(norm, p)
```

```@docs
Flux.activations
Flux.namedparams
```
33 changes: 33 additions & 0 deletions src/functor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,39 @@ function params(m...)
return ps
end

namedparams!(p, x::AbstractArray{<:Number}, name = :nothing, seen = IdSet()) = p[x] = name

function namedparams!(p, x, name = :nothing, seen = IdSet())
x in seen && return
push!(seen, x)
for (name, child) in pairs(trainable(x))
name = name isa Symbol ? string(name) : ""
namedparams!(p, child, name, seen)
end
end

"""
namedparams(m...)

Gate named parameters of the model

# Examples
```julia
julia> Flux.namedparams(Chain(LSTM(1, 1)))
IdDict{Any,String} with 5 entries:
Float32[0.0415536; 0.334981; 0.49804; 0.258496] => "Wi"
Float32[0.0] => ""
Float32[0.336735; -0.167129; -0.695561; -0.743882] => "Wh"
Float32[-0.979939, 1.0, 1.06309, -0.0981269] => "b"
Float32[0.0] => ""
```
"""
function namedparams(m...)
ps = IdDict{Any, String}()
namedparams!(ps, m, :nothing)
return ps
end

# Deprecated stuff
macro treelike(args...)
functorm(args...)
Expand Down
9 changes: 9 additions & 0 deletions test/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ end
@test size.(params(r)) == [(5, 10), (5, 5), (5,), (5,)]
end

@testset "namedparams" begin
m = Dense(10, 5)
@test Flux.namedparams(m) == Base.IdDict(m.W => "W", m.b => "b")
m = RNN(10, 5)
@test Flux.namedparams(m) == Base.IdDict(m.cell.Wi => "Wi", m.cell.Wh => "Wh", m.init => "init", m.cell.b => "b")
c = Chain(m, m)
@test Flux.namedparams(c) == Base.IdDict(m.cell.Wi => "Wi", m.cell.Wh => "Wh", m.init => "init", m.cell.b => "b")
end

@testset "Basic Stacking" begin
x = randn(3,3)
stacked = stack([x, x], 2)
Expand Down