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!
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);
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};
Xin = {[1; -2], [2; -3], [3; -5], [4; -6]};
xin = cell2mat(Xin);
Xout = net(Xin);
xout = cell2mat(Xout);
Compute predictions using weights and biases
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
n_feats = net.inputs{1}.size;
ntaps = numel(net.inputWeights{1}.delays);
D = net.numInputDelays;
N1_mat = [];
ypred = [];
input_mat = [];
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 );
feats_vec = xin(:,ii);
input_mat = [input_mat, feats_vec];
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
xvec = xvec(:);
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);
N = W2*a1_vec + b2;
ypred = [ypred, N];
end
댓글 수: 0
댓글을 달려면 로그인하십시오.