Open in Colab

Parametric Functions

Create custom parametric functions inside the neural network model.

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

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

Basic usage

Create a simple parametrc function That has two parameters p1 and p2 of size 1 and two inputs K1 and K2.

(The output dimension should be defined by the user. If not specified, the output is expected to be 1)

[2]:
def myFun(K1,K2,p1,p2):
    import torch
    return p1*K1+p2*torch.sin(K2)

x = Input('x')
F = Input('F')

parfun = ParamFun(myFun)
out = Output('out',parfun(x.last(),F.last()))

Parameter dimension

In this case the size of the parameter is specified. the first p1 is a 4 row column vector. The time dimension of the output is not defined but it depends on the input.

[3]:
def myFun(K1,K2,p1):
    import torch
    return torch.stack([K1,2*K1,3*K1,4*K1],dim=2).squeeze(-1)*p1+K2

x=Input('x')
F=Input('F')
parfun = ParamFun(myFun, parameters_and_constants = {'p1':(1,4)})
out = Output('out',parfun(x.last(),F.last()))
[check_names] The name 'x' is already in defined as NeuObj but it is overwritten.
[check_names] The name 'F' 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.

Passing custom parameters

Create a custom parameter to use inside the parametric function. Here the parametric function takes a parameter of size 1 and tw = 1 The function has two inputs, the first two are inputs and the second is a K parameter. The function creates a tensor and performs a dot product between input 1 and p1 (which is effectively the custom parameter ‘K’)

[4]:
def myFun(K1,p1):
    return K1*p1

x = Input('x')
K = Parameter('k', dimensions =  1, sw = 1,values=[[2.0]])
parfun = ParamFun(myFun, parameters_and_constants=[K])
out = Output('out',parfun(x.sw(1)))
[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.

Parametric function with different parameters

The same parametric function can be called passing various parameters.

[5]:
def myFun(K1,p1):
    return K1*p1

K = Parameter('k1', dimensions =  1, tw = 1, values=[[2.0],[3.0],[4.0],[5.0]])
R = Parameter('r1', dimensions =  1, tw = 1, values=[[5.0],[4.0],[3.0],[2.0]])

x = Input('x')
parfun = ParamFun(myFun)
out = Output('out',parfun(x.tw(1),K)+parfun(x.tw(1),R))
[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.

Parametric functions with Constants

parametric functions work also with constant values

[6]:
def myFun(K1,p1):
    return K1*p1

parfun = ParamFun(myFun)
x = Input('x')
c = Constant('c',values=[[5.0],[4.0],[3.0],[2.0]])
out = Output('out',parfun(x.sw(4),c))
[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.

Mapping over a batch

By setting the argument ‘map_over_batch’ to True, the parametric function will be mapped over the batch dimension.

[7]:
def myFun(k, p1):
    print(f'k:{k.shape}')
    print(f'p1:{p1.shape}')
    return k * p1

x = Input('x')
parfun = ParamFun(myFun, map_over_batch=True)
out = Output('out',parfun(x.sw(4)))
[check_names] The name 'x' is already in defined as NeuObj but it is overwritten.
k:torch.Size([4, 1])
p1:torch.Size([1])
[check_names] The name 'out' is already in defined as NeuObj but it is overwritten.