Reference
Reference for ModelUtils.jl
Index
ModelUtils.HookModelUtils.InitializationModelUtils.ModelModelUtils.IterLayersModelUtils.IterModelsModelUtils.addhookModelUtils.gradientsdictModelUtils.init!ModelUtils.init_kaiming_normalModelUtils.init_kaiming_uniformModelUtils.initmodel!ModelUtils.mapmodelModelUtils.paramsdictModelUtils.printmodel
Public Interface
ModelUtils.IterLayers — FunctionIterLayers(model::Model)Pre-order (parents before children) iterator over all wrapped layers.
Example
> model = Model(Chain(Conv((3, 3), 3 => 16), BatchNorm(16)))
> collect(IterModels(model))
[Chain{...}, Conv{...}, BatchNorm{...}]ModelUtils.IterModels — FunctionIterModels(model::Model)Pre-order (parents before children) iterator over all Models. If you want to iterate over the wrapped layers instead, use IterLayers.
Example
> model = Model(Chain(Conv((3, 3), 3 => 16), BatchNorm(16)))
> collect(IterModels(model))
[Model{Chain{...}}, Model{Conv{...}}, Model{BatchNorm{...}}]ModelUtils.Model — TypeModel(layer)Wrapper for Flux layer that recursively wraps children layers.
Use model.layer to access wrapped layer
Use children(model) to access children wrappers
ModelUtils.Hook — TypeHook(layer, [forward, backward, state = Dict()])Wraps a Flux layer, and holds a Dict with state (Hook.state)
After the forward pass of the wrapped layer calls Hook.forward(state, layeroutput) After the gradient calculation of the wrapped layer calls Hook.backward(state, layergradient)
For adding hooks to existing models, see addhook
ModelUtils.addhook — Functionaddhook(model; [forward, backward, filter_fn])Add a Hook with callbacks forward and backward to all layers of model where filter_fn(layer) == true.
See Hook
ModelUtils.mapmodel — Functionmapmodel(f, model::M, [constructor])Applies f to every layer, resulting in a new model.
The model layer structs are copied in the process, but the fields are not, hence the resulting model will have the same parameters!
The copying is done by first recursively applying mapmodel to all children fields and then attempting to create a new object of type M with the default outer constructor M(fields...).
If your custom type does not have a constructor of that form, like e.g. Flux.Chain, you will have to extend this function with a signature like
`mapmodel(f, model::MyType, (fields) -> ...)`where the last argument results in a MyType(fields...)
ModelUtils.paramsdict — Functionparamsdict(model::Model) -> Dict{Symbol, Array}Return a Dict with mapping parameter name => parameter value.
Useful for inspecting the distribution of parameters.
ModelUtils.gradientsdict — Functiongradientsdict(model::Model, gs::Grads) -> Dict{Symbol, Array}Return a Dict with mapping parameter name => gradient value
Useful for inspecting the distribution of gradients.
ModelUtils.printmodel — Functionprintmodel(model)Print model as a tree
ModelUtils.Initialization — TypeInitialization(layertype, field, initializer)Describes an initialization for field field of all layers of type layertype to be initialized with initializer.
Use initmodel! to apply to a model.
Example
Initialization(Conv, :bias, init_zeros) -> initialize the bias of all Conv layers with zeros.
ModelUtils.init_kaiming_normal — Functioninit_normal_uniform(T, dims...; gain=sqrt(2))Kaiming normal initializer
ModelUtils.init_kaiming_uniform — Functioninit_kaiming_uniform(T, dims...; gain=sqrt(2))Kaiming uniform initializer
Missing docstring for init_zeros. Check Documenter's build log for details.
ModelUtils.init! — Functioninit!(a, initializer)Initialize an array a with initializer.
Example
layer = Conv((3, 3), 3 => 16)
init!(layer.weight, init_kaiming_normal)ModelUtils.initmodel! — Functioninitmodel!(model, inits::Vector{Initialization})Applies inits to model.
See Initialization.
Example
model = Chain(Dense(10, 10), Dense(10, 10))
initmodel!(model, [
Initialization(Dense, :W, init_kaiming_normal),
Initialization(Dense, :bias, init_zeros)
])