@@ -68,20 +68,6 @@ for epoch in 1:1000
6868 Flux.train!((m,x,y) -> (m(x) - y)^2, model, data, optim)
6969end
7070```
71-
72- You may also specify a `name` for the model, which will
73- be used instead of the default printout, which gives a verbatim
74- representation of the code used to construct the model:
75-
76- ```
77- model = @compact(w=rand(3), name="Linear(3 => 1)") do x
78- sum(w .* x)
79- end
80- println(model) # "Linear(3 => 1)"
81- ```
82-
83- This can be useful when using `@compact` to hierarchically construct
84- complex models to be used inside a `Chain`.
8571"""
8672macro compact(_exs... )
8773 # check inputs, extracting function expression fex and unprocessed keyword arguments _kwexs
@@ -108,16 +94,6 @@ macro compact(_exs...)
10894 kwexs2 = map(ex -> Expr(:kw, ex. args... ), _kwexs) # handle keyword arguments provided before semicolon
10995 kwexs = (kwexs1... , kwexs2... )
11096
111- # check if user has named layer:
112- name = findfirst(ex -> ex. args[1 ] == :name, kwexs)
113- if name != = nothing && kwexs[name]. args[2 ] != = nothing
114- length(kwexs) == 1 && error(" expects keyword arguments" )
115- name_str = kwexs[name]. args[2 ]
116- # remove name from kwexs (a tuple)
117- kwexs = (kwexs[1 : name- 1 ]. .. , kwexs[name+ 1 : end ]. .. )
118- name = name_str
119- end
120-
12197 # make strings
12298 layer = " @compact"
12399 setup = NamedTuple(map(ex -> Symbol(string(ex. args[1 ])) => string(ex. args[2 ]), kwexs))
@@ -136,7 +112,7 @@ macro compact(_exs...)
136112 fex = supportself(fex, vars)
137113
138114 # assemble
139- return esc(:($ CompactLayer($ fex, $ name, ($ layer, $ input, $ block), $ setup; $ (kwexs... ))))
115+ return esc(:($ CompactLayer($ fex, ($ layer, $ input, $ block), $ setup; $ (kwexs... ))))
140116end
141117
142118function supportself(fex:: Expr , vars)
@@ -155,12 +131,11 @@ end
155131
156132struct CompactLayer{F,NT1<: NamedTuple ,NT2<: NamedTuple }
157133 fun:: F
158- name:: Union{String,Nothing}
159134 strings:: NTuple{3,String}
160135 setup_strings:: NT1
161136 variables:: NT2
162137end
163- CompactLayer(f:: Function , name :: Union{String,Nothing} , str:: Tuple , setup_str:: NamedTuple ; kw... ) = CompactLayer(f, name , str, setup_str, NamedTuple(kw))
138+ CompactLayer(f:: Function , str:: Tuple , setup_str:: NamedTuple ; kw... ) = CompactLayer(f, str, setup_str, NamedTuple(kw))
164139(m:: CompactLayer )(x... ) = m. fun(m. variables, x... )
165140CompactLayer(args... ) = error(" CompactLayer is meant to be constructed by the macro" )
166141Flux. @functor CompactLayer
179154
180155function Flux. _big_show(io:: IO , obj:: CompactLayer , indent:: Int = 0 , name= nothing )
181156 setup_strings = obj. setup_strings
182- local_name = obj. name
183- has_explicit_name = local_name != = nothing
184- if has_explicit_name
185- if indent != 0 || length(Flux. params(obj)) <= 2
186- _just_show_params(io, local_name, obj, indent)
187- else # indent == 0
188- print(io, local_name)
189- Flux. _big_finale(io, obj)
190- end
191- else # no name, so print normally
192157 layer, input, block = obj. strings
193158 pre, post = (" (" , " )" )
194- println(io, " " ^ indent, isnothing(name) ? " " : " $name = " , layer , pre)
159+ println(io, " " ^ indent, " @compact " , pre)
195160 for k in keys(obj. variables)
196161 v = obj. variables[k]
197162 if Flux. _show_leaflike(v)
@@ -220,7 +185,6 @@ function Flux._big_show(io::IO, obj::CompactLayer, indent::Int=0, name=nothing)
220185 else
221186 println(io, " ," )
222187 end
223- end
224188end
225189
226190# Modified from src/layers/show.jl
0 commit comments