How can I use my own initialization function in the Neural Network Toolbox?

Illustration
beemasingh234 - 2021-06-14T12:23:27+00:00
Question: How can I use my own initialization function in the Neural Network Toolbox?

I need to create my own initialization function for my network. I would like to use something other than INITNW and INITWB.  

Expert Answer

Profile picture of Neeta Dsouza Neeta Dsouza answered . 2025-11-20

When a neural network is created with NEWFF, it creates an initialization function called INITLAY. When the following code is executed:
 
 
P = [0 1 2 3 4 5 6 7 8 9 10];

T = [0 1 2 3 4 3 2 1 2 3 4];

net = newff(P,T,5);

net.initFcn
The result is:
 
 
 ans =

 initlay
This is the function that assigns the initialization function of each layer of the network to the function:
 
 
net.layers{1}.initfcn

 ans =

 initnw
This means that layer 1 has an initialization function 'initnw'.
To assign a custom function as the initialization function for a particular layer, simply write up the function and assign it to net.layers{1}.fcn:
 
net.layers{1}.initfcn = 'myfun';
That will assign the first layer's initialization function to the custom function. The rest of the layers can also be given initialization functions in the same way.
 
Run INIT on your network to initialize it. This basically calls INITLAY, which then calls each layer's initialization function:
 
 
init(net)


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!