Skip to content

Commit 78f2dae

Browse files
committed
make a keyword frozen, and print it
1 parent adfe6e4 commit 78f2dae

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

src/adjust.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ julia> Optimisers.update!(s, m, (x = ([pi], 10pi), y = [100pi])); # with fake g
2222
julia> m
2323
(x = ([1.0], 2.0), y = [-0.14159258336972558])
2424
25-
julia> s # Leaf(..., true) means frozen
26-
(x = (Leaf(Momentum{Float32}(0.01, 0.9), [0.0], true), ()), y = Leaf(Momentum{Float32}(0.01, 0.9), [3.14159]))
25+
julia> s
26+
(x = (Leaf(Momentum{Float32}(0.01, 0.9), [0.0], frozen=true), ()), y = Leaf(Momentum{Float32}(0.01, 0.9), [3.14159]))
2727
2828
julia> Optimisers.thaw!(s)
2929
30-
julia> s.x[1]
31-
Leaf(Momentum{Float32}(0.01, 0.9), [0.0])
30+
julia> s.x
31+
(Leaf(Momentum{Float32}(0.01, 0.9), [0.0]), ())
3232
```
3333
"""
3434
freeze!(tree) = (fmapstructure(freeze!, tree; exclude = x -> x isa Leaf); nothing)

src/interface.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ mutable struct Leaf{R,S} # mutable so that its identity encodes parameter shari
1515
state::S
1616
frozen::Bool # ... and to allow freeze! to act on this.
1717
end
18-
Leaf(rule, state) = Leaf(rule, state, false)
18+
Leaf(rule, state; frozen::Bool = false) = Leaf(rule, state, frozen)
1919

2020
@functor Leaf
2121

@@ -44,12 +44,12 @@ function _setup(rule, x; cache)
4444
end
4545
end
4646

47-
function Base.show(io::IO, ℓ::Leaf) # show method is mostly to hide its long type!
47+
function Base.show(io::IO, ℓ::Leaf; colour =.frozen ? :cyan : :green)
4848
ioc = IOContext(io, :compact => true)
49-
print(ioc, "Leaf(", ℓ.rule, ", ")
49+
str = sprint(show, ℓ.rule; context = ioc)
50+
printstyled(io, "Leaf(", str, ", "; color = colour)
5051
show(ioc, ℓ.state)
51-
.frozen && print(ioc, ", true")
52-
print(ioc, ")")
52+
printstyled(io, ℓ.frozen ? ", frozen=true)" : ")"; color = colour)
5353
end
5454

5555
###

0 commit comments

Comments
 (0)