network object custom weights initialization
3 views (last 30 days)
Show older comments
Question: How can I initialize the weights with a custom function?
So far, I've understood that I need to set ffnet.initFcn = 'initLay' to enable the layers init functions; then I need to set for each layer i ffnet.layers{i}.initFcn='initwb' to enable the weights init functions; but how should I continue this?
ffnet.initFcn = 'initLay';
for layer_index=1:size(ffnet.layers, 1)
ffnet.layers{layer_index}.initFcn = 'initwb';
end
What is the signature of a well design weights initialization function?
This is the code I produced so far, but I don't think you will need, anyway I will post it:
clear all
close all
load('Xprostate_data.mat')
X=Xprostate(:,1:8);
Y=table2array(Xprostate(:,9));
ind=table2array(Xprostate(:,10));
n=size(X,1);
Xp = zscore(table2array(X));
Xtrain=Xp(ind==1,:); Ytrain=Y(ind==1); %dataset for training
Xtest=Xp(ind==0,:); Ytest=Y(ind==0); %dataset for test
%%%
ffnet = feedforwardnet(netconf);
ffnet = train(ffnet, Xtrain', Ytrain');
ffnet.trainFcn = 'traingd';
ffnet.trainParam.lr = 0.001;
net.initFcn = 'initLay'; %initLay
for layer_index=1:size(net.layers, 1)
net.layers{layer_index}.initFcn = 'initwb';
end
ffnet = init(ffnet);
ffnet = train(ffnet, Xtrain', Ytrain');
function weights = randomNormalWeights(sz, mean, std)
weights = mean + randn(sz) * std;
end
0 Comments
Answers (1)
Neha
on 5 May 2023
Hi Fabiano,
It is my understanding that you need to initialize the weights using a custom initialization function. Please refer to the code given below:
[icRows,icCols]=size(ffnet.inputConnect);
[lcRows,lcCols]=size(ffnet.layerConnect);
for i=1:icRows
for j=1:icCols
if ffnet.inputConnect(i,j)==1
ffnet.IW{i,j}= randomNormalWeights(ffnet.inputWeights{i,j}.size,1,1); %matrix of size ffnet.inputWeights{i,j}.size, here: 10x8 (number of layersxnumber of inputs)
end
end
end
for i=1:lcRows
for j=1:lcCols
if ffnet.layerConnect(i,j)==1
ffnet.LW{i,j}=randomNormalWeights(ffnet.layerWeights{i,j}.size,1,1); %matrix of size ffnet.layerWeights{i,j}.size, here: 10x1
end
end
end
For detailed explanation of the functions used, please refer to the following documentation links:
0 Comments
See Also
Categories
Find more on Sequence and Numeric Feature Data Workflows in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!