class MXNet::Gluon::Block

Overview

Base class for all neural network layers and models. Your models should subclass this class.

Direct Known Subclasses

Defined in:

mxnet/gluon/block.cr

Constructors

Instance Method Summary

Macro Summary

Constructor Detail

def self.new(prefix = nil, params = nil) #

[View source]

Instance Method Detail

def call(inputs : Array(T)) : Array(T) forall T #

Calls #forward.

Only accepts positional arguments.

Parameters

  • inputs (Array(NDArray)) Input tensors.

[View source]
def children #

Returns this block's registered children.


[View source]
def collect_params(selector = nil) #

Returns a ParameterDict containing this Block's and all of its children's Parameters. Can also return the Parameters that match some given regular expressions.

For example, collect the specified Parameters 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.

[View source]
def forward(inputs : Array(T)) : Array(T) forall T #

Override to implement forward computation using NDArray.

Only accepts positional arguments.

Parameters

  • inputs (Array(NDArray)) Input tensors.

[View source]
def get_attr(name : String) : Block | Parameter | Nil #

[View source]
def hybridize(active = true) #

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.

[View source]
def init(init = nil, ctx = nil, force_reinit = false) #

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 or Array(Context), default = nil) Desired contexts.
  • force_reinit (Bool, default = false) Whether to force re-initialization if parameter is already initialized.

[View source]
def load_parameters(fname, ctx = MXNet.cpu, allow_missing = false, ignore_extra = false) #

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 or Array(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.

[View source]

Returns this block's parameter dictionary.

Does not include its children's parameters.


[View source]
def prefix : String #

Prefix of this block.


[View source]
def register_child(block, name = nil) #

Registers block as a child of self. Blocks assigned as attributes will be registered automatically.


[View source]
def register_parameter(param, name = nil) #

Registers parameter on self. Parameters assigned as attributes will be registered automatically.


[View source]
def save_parameters(fname) #

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.

[View source]

Scope of this block.


[View source]
def set_attr(name : String, value : Block | Parameter | Nil) #

[View source]
def with_name_scope(&) #

Enters a name scope managing block names.

self.with_name_scope do
  self.dense = MXNet::Gluon::NN.Dense(20)
end

[View source]

Macro Detail

macro attribute(*names) #

Creates accessors for declared attributes.


[View source]