MATLAB: How i can save the output of hidden layer of neural network

Deep Learning Toolboximage compressionneural network

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

Best 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