In an assignment A(I) = B, the number of elements in B and I must be the same.

1 view (last 30 days)
Hi, does anybody know can I fix this error in line "e(i) = d - y2"
for i = 1: size(data_train,2)
x= data_train(:,i);
d = d_train(i); %Pattern mode is selected not Batch mode
% forward propagation
v1 = wh*x; %data entered
y1 = tanh(v1);
y1 = [-1;y1];
v2 = wo*y1;
y2 = (v2); %output and it should be linear active function
% backward propagation:
% adjust output synaptic weights
e(i) = d - y2; %Error calculation
delta_o = e(i) * (1); %bcz deriative of y=x is 1
wo = wo + etha*delta_o* y1'; %set new weight in output layer
% adjust hidden synaptic weights
delta_h = (1-y1.^2) .* (delta_o*wo)';
wh = wh + etha * delta_h(2:end)* x';
end
And in the work space y2 is 3x1 and d is 1x1
  2 Comments

Sign in to comment.

Answers (2)

James Tursa
James Tursa on 4 Aug 2022
Edited: James Tursa on 4 Aug 2022
" And in the work space y2 is 3x1 and d is 1x1 "
Then d - y2 will be 3x1. You can't assign a 3-element vector to a 1x1 scalar which is what e(i) is.
If you could show an image of the math you are trying to use we can help you fix up the code.
  3 Comments
James Tursa
James Tursa on 4 Aug 2022
@Fatemeh Salar There are ways to store vector or matrices in iterations, as others have pointed out, but we can't be sure this is what is needed. We would like to see a description (e.g., image) of the math you are trying to implement. Do you have anything like this you can post?

Sign in to comment.


Walter Roberson
Walter Roberson on 4 Aug 2022
x= data_train(:,i);
That looks like a column of data, but we cannot tell how large it is
v1 = wh*x; %data entered
If wh is a scalar, then since x is a vector then v1 would be a vector. But we don't know -- wh could be a 2D array and x might be a scalar. v1 is probably a vector, not provably from this code.
y1 = tanh(v1);
same size as v1
y1 = [-1;y1];
That would fail if y1 is not a scalar or a column vector, so given we pass this line, we can figure that v1 is a scalar or vector. Either way, y1 would have to be a column vector after this line.
v2 = wo*y1;
we do not know what the size of wo is, but if it is not a scalar then likely the * operation would fail. So v1 is probably a column vector.
y2 = (v2); %output and it should be linear active function
Same size as v2, probably same size as y1
d = d_train(i); %Pattern mode is selected not Batch mode
scalar
e(i) = d - y2; %Error calculation
Right hand side is scalar minus (likely) column vector. Left hand side names a scalar location. Right hand side does not fit.
If you want to record all of the d-y2 values through the loop, then either assign into columns or else use a cell array.
  2 Comments
Walter Roberson
Walter Roberson on 4 Aug 2022
Regardless of whether y2 comes out as column vector or as 2D array, you cannot store it in a single location e(i)

Sign in to comment.

Categories

Find more on Deep Learning with Time Series and Sequence Data 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!

Translated by