Set initial weights of neural networks

5 views (last 30 days)
Hamza Ali
Hamza Ali on 5 May 2017
Commented: Santhana Raj on 5 May 2017
Hello everyone !
I would like to set the initial weights of artificial neural network in order to define relevant number of hidden nodes. So, I wrote the code below but it does not work. Could you correct it for me? Thank you.
% Solve an Input-Output Fitting problem with a Neural Network
function [performance] = NN(x,t,i)
% Choose a Training Function
% For a list of all training functions type: help nntrain
% 'trainlm' is usually fastest.
% 'trainbr' takes longer but may be better for challenging problems.
% 'trainscg' uses less memory. NFTOOL falls back to this in low memory situations.
[xn,xs] = mapminmax(x);% Normalization of inputs
[tn,ts] = mapminmax(t); % Normalization of outputs
%dx = randperm(size(xn,2)); % Inputs permutation
%xn = xn(:,dx); % Inputs permutation
%dx = randperm(size(tn,2)); % Output normalization
%tn = tn(:,dx); % Output normalization
trainFcn = 'trainlm'; % Levenberg-Marquardt
% Create a Fitting Network
H = i ;
net = fitnet(H, trainFcn);
I = size(x,1);
N = size(x,2);
O = size(t,1);
net = configure(net, xn, tn);
% Define initial conditions (transferFn, weights, biases...)
% Define transfer Function for each layer
net.layers{1}.transferFcn = 'tansig';
net.layers{2}.transferFcn = 'logsig';
rng(0);
IW = 0.001 * randn(H,I);
LW = 0.001 * randn(O,H);
b1 = 0.001 * randn(H,1);
b2 = 0.001 * randn(O,1);
% Show weights and biases before training
net.IW{1,1}
net.LW{2,1}
net.b{1}
net.b{2}
% Setup Division of Data for Training, Validation, Testing
%net.divideParam.trainRatio = 70/100;
%net.divideParam.valRatio = 15/100;
%net.divideParam.testRatio = 15/100;
net.divideFcn = 'dividerand';
% Train the Network
[net] = train(net,xn,tn);
% Show weights and biases after training
net.IW{1,1} = IW;
net.LW{2,1} = LW;
net.b{1} = b1;
net.b{2} = b2;
% Test the Network
y = net(xn);
e = gsubtract(tn,y);
performance = perform(net,tn,y)
  1 Comment
Santhana Raj
Santhana Raj on 5 May 2017
What do you mean by not working???
If you are getting error, then post the error.
If it is not able to train a specific data, then post the data also.
If you are expecting some results and the results are different, then post the data along with the expected results.

Sign in to comment.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!