Assign different input using for loop
6 views (last 30 days)
Show older comments
Hi,
This is probably a simple question for most of you but I've been struggling with it for far too long. I have several lines of codes which will eventually get an output(O). When I only use a single input(in), the codes work fine but let's say I want to use a vector as an input and get an output with the same size as the input vector (each value in input vector will get corresponding output), how should I go about it? I've tried using for loop but no luck.
%%Loading variables
weight = unifrnd(-10, 10, [1 13]);
W1 = weight(1,1:4); % Input Layer Weight
W2 = weight(1,5:8); % Input Layer Bias
W3 = weight(1,9:12) % Hidden Layer Weight
W4 = weight(1,13); % Hidden Layer Bias
I.I = W1.*in % Input x weight
I.B = W2; % Bias
H.SUM = I.I+I.B; % Hidden Neuron
H = tansig (H.SUM) % Hidden Neuron Squashed
O.SUM = dot(H,W3') + W4 % Hidden Neuron x Weight + Bias
O = purelin (O.SUM) % Output Neuron
0 Comments
Answers (1)
Rahul K
on 15 Feb 2017
Edited: Rahul K
on 15 Feb 2017
I edited three lines (the commented out ones), the code now allows you to enter a row vector of inputs and returns a row vector of outputs. The variables I.I and I.B are now matrices; you could do achieve the same result with for loops, but I think it's faster this way.
%%Loading variables
weight = unifrnd(-10, 10, [1 13]);
W1 = weight(1,1:4); % Input Layer Weight
W2 = weight(1,5:8); % Input Layer Bias
W3 = weight(1,9:12); % Hidden Layer Weight
W4 = weight(1,13); % Hidden Layer Bias
%I.I = W1.*in
I.I = W1'*in % Input x weight
%I.B = W2
I.B = repmat(W2',1,length(in)) % Bias
H.SUM = I.I+I.B % Hidden Neuron
H = tansig (H.SUM) % Hidden Neuron Squashed
%O.SUM = dot(H,W3') + W4
O.SUM = W3*H + W4 % Hidden Neuron x Weight + Bias
O = purelin (O.SUM) % Output Neuron
See Also
Categories
Find more on Define Shallow Neural Network Architectures 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!