Reference

Reference for ModelUtils.jl

Index

Public Interface

ModelUtils.IterLayers — Function
IterLayers(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{...}]
source
ModelUtils.IterModels — Function
IterModels(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{...}}]
source
ModelUtils.Model — Type
Model(layer)

Wrapper for Flux layer that recursively wraps children layers.

Use model.layer to access wrapped layer

Use children(model) to access children wrappers

source
ModelUtils.Hook — Type
Hook(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

source
ModelUtils.addhook — Function
addhook(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

source
ModelUtils.mapmodel — Function
mapmodel(f, model::M, [constructor])

Applies f to every layer, resulting in a new model.

Warning

The model layer structs are copied in the process, but the fields are not, hence the resulting model will have the same parameters!

Warning

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...)

source
ModelUtils.paramsdict — Function
paramsdict(model::Model) -> Dict{Symbol, Array}

Return a Dict with mapping parameter name => parameter value.

Useful for inspecting the distribution of parameters.

source
ModelUtils.gradientsdict — Function
gradientsdict(model::Model, gs::Grads) -> Dict{Symbol, Array}

Return a Dict with mapping parameter name => gradient value

Useful for inspecting the distribution of gradients.

source
ModelUtils.Initialization — Type
Initialization(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.

source
Missing docstring.

Missing docstring for init_zeros. Check Documenter's build log for details.

ModelUtils.init! — Function
init!(a, initializer)

Initialize an array a with initializer.

Example

layer = Conv((3, 3), 3 => 16)
init!(layer.weight, init_kaiming_normal)
source
ModelUtils.initmodel! — Function
initmodel!(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)
])
source