Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

how i can save the output of hidden layer of neural network

Asked by Ahmed almansory on 12 Apr 2013

after i complete my program,i want to save the output,weight and bias of hidden layer of neural network, can any one help me to do this? thanks

0 Comments

Ahmed almansory

2 Answers

Answer by Greg Heath on 15 Apr 2013
Accepted answer

close all, clear all, clc, plt=0;

 delete h.mat
 delete LW.mat
 delete b2.mat
 delete tsettings.mat
 [ x, t ] = simplefit_dataset;
 net   = fitnet;
 rng(4151941) 
 [ net tr y0 ] = train( net, x, t);
 plt=plt+1,figure(plt)                                   % figure 1
 hold on
 plot(x,t,'b--','LineWidth',2)
 plot(x,y0,'r.','LineWidth',2)
 legend('TARGET','NN OUTPUT')
 xlabel('INPUT')
 ylabel('TARGETAND NN OUTPUT')
 title('SIMPLEFIT DATASET')

%Create tsettings, h, LW, b2

 [xn xsettings] = mapminmax(x);
 [tn tsettings]  = mapminmax(t);     % SAVE tsettings
 b1  = cell2mat(net.b(1))
 IW = cell2mat(net.IW)
 [ I N ]  = size(xn)
 B1  = b1*ones(1,N);
 % B1 = repmat(b,1,N);             % Alternate
 h    = tanh(B1+IW*xn);             % SAVE h
 LW = cell2mat(net.LW)           % SAVE LW
 b2 = cell2mat(net.b(2))            % SAVE b2
 clc
 whos h LW b2 tsettings
 dir
% save FILENAME ... is the command form of the syntax
% for convenient  saving from the command line.  With 
% command syntax, you do not need to  enclose strings in 
% single quotation marks.  Separate inputs with spaces
% instead of commas.  Do not use command syntax if 
% inputs such as FILENAME are variables.
 save h
 save LW
 save b2
 save tsettings
 dir
 whos N h LW b2 tsettings
 disp('BEFORE CLEARING SAVED HIDDEN VARIABLES')
 disp('ENTER TO CONTINUE')
 pause
 dir
 clear     h LW b2 tsettings
 whos N h LW b2 tsettings
 disp('AFTER CLEARING SAVED HIDDEN VARIABLES')
 disp('ENTER TO CONTINUE')
 pause
 load      h.mat
 load      LW b2 tsettings
 whos N h LW b2 tsettings
 disp('AFTER RELOADING SAVED HIDDEN VARIABLES')
 disp('ENTER TO CONTINUE')
 pause
 yn = b2 + LW*h;
 y  =  mapminmax('reverse',yn,tsettings);
 reloadingerror = max(abs(y-y0))
 break

% Hope this helps

Thank you for formally accepting my answer

Greg

1 Comment

Greg Heath
Answer by Greg Heath on 13 Apr 2013
Edited by Greg Heath on 13 Apr 2013
 clear all, clc
 [ x, t ] = simplefit_dataset;
 net0    = fitnet( 10 );
 [ net0 tr0 y0 ] = train( net0, x, t);
 whos

%You can save the output and net

 save y0 net0

%Delete them

 clear y0 net0
 whos 

%and retrieve them

 load y0 net0
 whos

%or save, delete and and retrieve the weights

 W0 = getwb(net0)
 save y0 W0
 whos
 clear y0 W0
 whos 
 load y0 W0
 whos

Hope this helps.

Thank you for formally accepting my answer

Greg

1 Comment

Ahmed almansory on 13 Apr 2013

thanks for your replay my problem is : i design feed forward network for image compression consist of three layer (input,hidden,output)the number of neurons in both input and output layers is equal ,hidden layer contain less number of neuron for compression issue ,i get good result from output layer (reconstructed image) but i cant save or show the compressed copy of image(output of hidden layer)and the final weights of hidden layer please help me?

Greg Heath

Contact us