Loihi¶
Automatic network creation modle for SLAYER-Loihi. It reads the cofiguration form the network description file and returns a network module. It also add capability to load, export and moinitor the network during training.
-
class
slayerSNN.auto.loihi.
Network
(netParams, preHookFx=<function Network.<lambda>>, weightNorm=False, countLog=False)[source]¶ This class encapsulates the network creation from the networks described in netParams configuration. A netParams configuration is
slayerSNN.slayerParams.yamlParams
which can be initialized from a yaml config file or a dictionary.In addition to the standard network
forward
function, it also includesclamp
function for clamping delays,gradFlow
function for monitioring the gradient flow, andgenModel
function for exporting a hdf5 file which is a packs network specification and trained parameter into a single file that can be possibly used to generate the inference network specific to a hardware, with some support.- Arguments:
nOutputs
: number of output groups (Equal to the number of ouptut classes).countLog
: a flag to indicate if a log of spike count should be maintained and passed around or not.Default: False
Usage:
blk = averageBlock(self.slayer, nOutputs=10) spike = blk(spike)
-
clamp
()[source]¶ Clamp routine for delay parameters after gradient step to ensure positive value and limit the maximum value.
Usage:
net = Network(netParams) net.clamp()
-
forward
(spike)[source]¶ Forward operation of the network.
- Arguments:
spike
: Input spke tensor.
Usage:
net = Network(netParams) spikeOut = net.forward(spike)
-
genModel
(fname)[source]¶ This function exports a hdf5 encapsulated neuron parameter, network structure, the weight and delay parameters of the trained network. This is intended to be platform indepenent representation of the network. The basic protocol of the file is as follows:
|->simulation # simulation description | |->Ts # sampling time. Usually 1 | |->tSample # length of the sample to run |->layer # description of network layer blocks such as input, dense, conv, pool, flatten, average |->0 | |->{shape, type, ...} # each layer description has ateast shape and type attribute |->1 | |->{shape, type, ...} : |->n |->{shape, type, ...} input : {shape, type} flatten: {shape, type} average: {shape, type} dense : {shape, type, neuron, inFeatures, outFeatures, weight, delay(if available)} pool : {shape, type, neuron, kernelSize, stride, padding, dilation, weight} conv : {shape, type, neuron, inChannels, outChannels, kernelSize, stride, padding, dilation, groups, weight, delay(if available)} |-> this is the description of the compartment parameters |-> {iDecay, vDecay, vThMant, refDelay, ... (other additional parameters can exist)}
Usage:
net = Network(netParams) net.genModel(path_to_save)
-
gradFlow
(path)[source]¶ A method to monitor the flow of gradient across the layers. Use it to monitor exploding and vanishing gradients.
scaleRho
must be tweaked to ensure proper gradient flow. Usually monitoring it for first few epochs is good enough.Usage:
net = Network(netParams) net.gradFlow(path_to_save)
-
class
slayerSNN.auto.loihi.
averageBlock
(nOutputs, countLog=False)[source]¶ This class averages the spikes among n different output groups for population voting.
- Arguments:
nOutputs
: number of output groups (Equal to the number of ouptut classes).countLog
: a flag to indicate if a log of spike count should be maintained and passed around or not.Default: False
Usage:
blk = averageBlock(self.slayer, nOutputs=10) spike = blk(spike)
-
forward
(spike)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
-
class
slayerSNN.auto.loihi.
convBlock
(slayer, inChannels, outChannels, kernelSize, stride=1, padding=0, dilation=1, groups=1, weightScale=100, preHookFx=<function convBlock.<lambda>>, weightNorm=False, delay=False, countLog=False)[source]¶ This class creates a conv layer block with Loihi neuron. It groups the synaptic interaction, Loihi neuron response and the associated delays.
- Arguments:
slayer
(slayerLoihi.slayer
): pre-initialized slayer loihi module.inChannels
: number of input channels.outChannels
: number of output channels.kernelSize
: size of convolution kernel.stride
: size of convolution stride. Default: 1padding
: size of padding. Default: 0dialtion
: size of convolution dilation. Default: 1groups
: number of convolution groups. Default: 1weightScale
: scale factor of the defaule initialized weights. Default: 100preHoodFx
: a function that operates on weight before applying it. Could be used for quantization etc.Default: quantization in step of 2 (Mixed weight mode in Loihi)
weightNorm
: a flag to indicate if weight normalization should be applied or not. Default: Falsedelay
: a flag to inidicate if axonal delays should be applied or not. Default: FalsecountLog
: a flag to indicate if a log of spike count should be maintained and passed around or not.Default: False
Usage:
blk = convBlock(self.slayer, 16, 31, 3, padding=1) spike = blk(spike)
-
forward
(spike)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
-
class
slayerSNN.auto.loihi.
denseBlock
(slayer, inFeatures, outFeatures, weightScale=100, preHookFx=<function denseBlock.<lambda>>, weightNorm=False, delay=False, countLog=False)[source]¶ This class creates a dense layer block with Loihi neuron. It groups the synaptic interaction, Loihi neuron response and the associated delays.
- Arguments:
slayer
(slayerLoihi.slayer
): pre-initialized slayer loihi module.inFeatures
: number of input features.outFeatures
: number of output features.weightScale
: scale factor of the defaule initialized weights. Default: 100preHoodFx
: a function that operates on weight before applying it. Could be used for quantization etc.weightNorm
: a flag to indicate if weight normalization should be applied or not. Default: Falsedelay
: a flag to inidicate if axonal delays should be applied or not. Default: FalsecountLog
: a flag to indicate if a log of spike count should be maintained and passed around or not.Default: False
Usage:
blk = denseBlock(self.slayer, 512, 10)
-
forward
(spike)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
-
class
slayerSNN.auto.loihi.
flattenBlock
(countLog=False)[source]¶ This class flattens the spatial dimension. The resulting tensor is compatible with dense layer.
- Arguments:
countLog
: a flag to indicate if a log of spike count should be maintained and passed around or not.Default: False
Usage:
blk = flattenBlock(self.slayer, True) spike = blk(spike)
-
forward
(spike)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
-
class
slayerSNN.auto.loihi.
poolBlock
(slayer, kernelSize, stride=None, padding=0, dilation=1, countLog=False)[source]¶ This class creates a pool layer block with Loihi neuron. It groups the synaptic interaction, Loihi neuron response and the associated delays.
- Arguments:
slayer
(slayerLoihi.slayer
): pre-initialized slayer loihi module.kernelSize
: size of pooling kernel.stride
: size of pooling stride. Default: None(same askernelSize
)padding
: size of padding. Default: 0dialtion
: size of convolution dilation. Default: 1countLog
: a flag to indicate if a log of spike count should be maintained and passed around or not.Default: False
Usage:
blk = poolBlock(self.slayer, 2) spike = blk(spike)
-
forward
(spike)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.