class MXNet::Gluon::Block
- MXNet::Gluon::Block
- Reference
- Object
Overview
Base class for all neural network layers and models. Your models should subclass this class.
Direct Known Subclasses
Defined in:
mxnet/gluon/block.crConstructors
Instance Method Summary
-
#call(inputs : Array(T)) : Array(T) forall T
Calls
#forward
. -
#children
Returns this block's registered children.
-
#collect_params(selector = nil)
Returns a
ParameterDict
containing thisBlock
's and all of its children'sParameter
s. -
#forward(inputs : Array(T)) : Array(T) forall T
Override to implement forward computation using
NDArray
. - #get_attr(name : String) : Block | Parameter | Nil
-
#hybridize(active = true)
Activates or deactivates
HybridBlock
children recursively. -
#init(init = nil, ctx = nil, force_reinit = false)
Initializes parameters of this block and its children.
-
#load_parameters(fname, ctx = MXNet.cpu, allow_missing = false, ignore_extra = false)
Loads parameters from a file.
-
#params : MXNet::Gluon::ParameterDict
Returns this block's parameter dictionary.
-
#prefix : String
Prefix of this block.
-
#register_child(block, name = nil)
Registers block as a child of self.
-
#register_parameter(param, name = nil)
Registers parameter on self.
-
#save_parameters(fname)
Saves parameters to a file.
-
#scope : MXNet::Gluon::BlockScope?
Scope of this block.
- #set_attr(name : String, value : Block | Parameter | Nil)
-
#with_name_scope(&)
Enters a name scope managing block names.
Macro Summary
-
attribute(*names)
Creates accessors for declared attributes.
Constructor Detail
Instance Method Detail
Calls #forward
.
Only accepts positional arguments.
Parameters
- inputs (
Array(NDArray)
) Input tensors.
Returns a ParameterDict
containing this Block
's and all of
its children's Parameter
s. Can also return the Parameter
s
that match some given regular expressions.
For example, collect the specified Parameter
s for
"conv1_weight", "conv1_bias", "fc_weight" and "fc_bias":
model.collect_params(/conv1_weight|conv1_bias|fc_weight|fc_bias/)
or, alternatively, collect all parameters whose names end with "weight" or "bias":
model.collect_params(/.*weight|.*bias/)
Parameters
- selector (
Regex
) Regular expressions to match parameters.
Override to implement forward computation using NDArray
.
Only accepts positional arguments.
Parameters
- inputs (
Array(NDArray)
) Input tensors.
Activates or deactivates HybridBlock
children
recursively. Has no effect on non-hybrid blocks.
Parameters
- active (
Bool
, default =true
) Whether to turn hybridization on or off.
Initializes parameters of this block and its children.
Equivalent to self.collect_params.init(...)
.
Parameters
- init (
Initializer
, default =nil
) The initializer to use. - ctx (
Context
orArray(Context)
, default =nil
) Desired contexts. - force_reinit (
Bool
, default =false
) Whether to force re-initialization if parameter is already initialized.
Loads parameters from a file.
For reference see: "Saving and Loading Gluon Models" (https://mxnet.incubator.apache.org/tutorials/gluon/save_load_params.html).
Parameters
- fname (
String
) Path to file. - ctx (
Context
orArray(Context)
, default = cpu) Context(s) to initialize loaded parameters on. - allow_missing (
Bool
, default =false
) Whether to silently skip parameters not present in the file. - ignore_extra (
Bool
, default =false
) Whether to silently ignore parameters not present in this block.
Returns this block's parameter dictionary.
Does not include its children's parameters.
Registers block as a child of self. Blocks assigned as attributes will be registered automatically.
Registers parameter on self. Parameters assigned as attributes will be registered automatically.
Saves parameters to a file.
Note that this method only saves parameters, not model
structure. If you want to save model structures, use
HybridBlock#export
.
For reference see: "Saving and Loading Gluon Models" (https://mxnet.incubator.apache.org/tutorials/gluon/save_load_params.html).
Parameters
- fname (
String
) Path to file.
Enters a name scope managing block names.
self.with_name_scope do
self.dense = MXNet::Gluon::NN.Dense(20)
end