Open in Colab

Constant and Parameter

Create custom Constant parameters.

NNodely constants are static and not modified during the training process.

Create custom Parameter vectors that can also be used in various NNodely blocks.

NNodely parameters can be updated during the training process.

[1]:
# uncomment the command below to install the nnodely package
#!pip install nnodely

from nnodely import *
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>-- nnodely_v1.5.0 --<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Basic Usage

Create a Constant ‘g’ and a Parameter ‘k’

[2]:
g = Constant('g', values=[9.81])
k = Parameter('k', tw=4)

Using a Parameter inside another block

Create a parameter k of dimension 3 and use this parameter to initialize the weigths of two Fir Layers.

!! Note that the two Fir share now the same weights because they have been initialized with the same Parameter

[3]:
x = Input('x')

k = Parameter('k', dimensions=3, tw=4)

fir1 = Fir(W=k)
fir2 = Fir(3, W=k)

out = Output('out', fir1(x.tw(4))+fir2(x.tw(4)))
[check_names] The name 'k' is already in defined as NeuObj but it is overwritten.

Parametric functions

Create two parameters and use them inside a parametric function. The parameters are inizialized with custom values

[4]:
x= Input('x')

g = Parameter('g', dimensions=3, values=[[4,5,6]])
t = Parameter('t', dimensions=3, values=[[1,2,3]])

def fun(x, k, t):
    return x+(k+t)

p = ParamFun(fun, parameters_and_constants=[g,t])

out = Output('out', p(x.tw(1)))
[check_names] The name 'x' is already in defined as NeuObj but it is overwritten.
[check_names] The name 'g' is already in defined as NeuObj but it is overwritten.
[check_names] The name 'out' is already in defined as NeuObj but it is overwritten.

Arithmetic functions with Parameters and Constants

Constant and Parameter work with all the arithmetic functions. Here are summed together along with the input variable

[5]:
g = Parameter('g', sw=1, values=[[1,2,3,4]])
o = Constant('o', sw=1, values=[[1,2,3,4]])
x = Input('x', dimensions=4)
out = Output('out', x.last()+g+o)
[check_names] The name 'g' is already in defined as NeuObj but it is overwritten.
[check_names] The name 'x' is already in defined as NeuObj but it is overwritten.
[check_names] The name 'out' is already in defined as NeuObj but it is overwritten.

SampleTime

Create a constant equal to the sample time

[6]:
dt = SampleTime()
x = Input('x')
out = Output('out', x.last() + dt)
[check_names] The name 'x' is already in defined as NeuObj but it is overwritten.
[check_names] The name 'out' is already in defined as NeuObj but it is overwritten.

Initialization functions

Initialize a Parameter of dimension 4 using a constant initialization function

[7]:
p = Parameter('p', dimensions=4, init=init_constant, init_params={'value':4})