How to apply the weights and biases of a network without the net function?

1 view (last 30 days)
I have trained a dynamic neural network with inputs and targets.
A vector of predictions can be computed using the following command: predictions = net(inputs)
where net is the network structure.
However, instead of using the command net(inputs), I want to use the weights, the biases, and the needed algebra to compute the predictions.
I wrote a code the should compute the predictions. But the results that I get are different from results which are computed with net(inputs).
A code that wrote is attached below.
I confirmed that "Part 1" is correct, but "Part 2" gives me a wrong results. Part 2 should be easier but I get a wrong result. Please advice. Thanks!
% Dummy DNN (with needed architecture)
xin = [1:3000; -1:-1:-3000];
xout = rand(1,3000);
Xin = num2cell(xin,1);
Xout = num2cell(xout,1);
inputDelays = [0 1];
hiddenSizes = 2;
net = timedelaynet(inputDelays, hiddenSizes);
net = train( net, xin, xout);
% Change the weight matrix and biases for easier debugging
net.IW{1,1} = [1, 2, 3, 4; 5, 6, 7, 8];
W1 = net.IW{1,1};
net.LW{2,1} = [1, 2];
W2 = net.LW{2,1};
net.b{1,1} = [0; 0];
b1 = net.b{1,1};
net.b{2,1} = 0;
b2 = net.b{2,1};
% Define inputs
Xin = {[1; -2], [2; -3], [3; -5], [4; -6]};
xin = cell2mat(Xin);
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%%%Compute predictions using net(inputs)
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Xout = net(Xin);
xout = cell2mat(Xout);
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Compute predictions using weights and biases
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
n_feats = net.inputs{1}.size; % number of features
ntaps = numel(net.inputWeights{1}.delays); % number of delays
D = net.numInputDelays; % number of delays w/o x(t)
N1_mat = []; % outputs from the hidden layer (before transfer function)
ypred = []; % computed predictions
input_mat = []; % matrix which contains the added input vectors
for ii = 1 : numel(Xout)
Part 1: Prepare W1 and xin for computing outputs of hidden layer
----------------------------------------------------------- Define matrix of features
xvec = zeros ( n_feats, ntaps );
% Compute features for a given pollcount (external function)
feats_vec = xin(:,ii); % cell2mat(Inputs(1,ii));
% Concatenate the computed feature vectors
input_mat = [input_mat, feats_vec];
% Continue to next iteration if there are not enough delays (pollcounts)
if size(input_mat,2) <= D
tmp = [input_mat, zeros( n_feats, ntaps - size(input_mat,2))];
xvec = xvec + tmp;
else
xvec = fliplr( input_mat(:,ii-D:ii) );
end
% Rearrange for multiplication with weight matrix W1
xvec = xvec(:);
% Compute output of layer 1
N_vec = W1*xvec + b1;
N1_mat = [N1_mat, N_vec];
Part 2: Compute outputs of DNN
----------------------------------------------------------- Apply transfer function
a1_vec = tansig(N_vec);
% Compute output of layer 2
N = W2*a1_vec + b2;
% Concatenate
ypred = [ypred, N];
end

Accepted Answer

Greg Heath
Greg Heath on 6 Jun 2015
Insufficient information:
What dynamic network?
The output of none of the dynamic networks can be obtained using the simple command.
output = net(input).
You must include the initial state conditions.
See the help and doc documentation for TIMEDELAYNET, NARNET and NARXNET. For example
help narxnet
doc narxnet
Hope this helps.
Thank you for formally acceptingmy answer
Greg
PS posting your code will help.

More Answers (0)

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!