matrices subtraction of different dimensions

My apologies for not defining the question in detailed format. Here is the problem which I am trying to solve for neural network optimization. The dataset A has 500 observations with 9 features variables making a matrix of 500 X 9. For better optimization, couple of parameters are calculated which is defined as
NumberofHiddenNeurons = 10
NumberofInputNeurons = 9 %(No, of features of dataset A)
InputWeight=rand(NumberofHiddenNeurons,NumberofInputNeurons)*2-1;
NumberofTrainingData = 500 %A = 500 X 9
for j=1:NumberofHiddenNeurons
for M=1:NumberofInputNeurons
C(j,M)= -(InputWeight(j,M)/2*(InputWeight(j,NumberofInputNeurons)));
end
end
%%subtraction of C from original A
for g= 1:NumberofHiddenNeurons
for k = 1:NumberofTrainingData
tempH(g,k)=norm(bsxfun(@minus,(A(g,k)),C(g,:))).^2;
end
end
Now the problem is C 10 X 9 as 10 hidden nodes for 9 features and original dataset is 500 X 9 features. The output as tempH should be 10 X 500 as 10 outputs from 10 hidden neurons for 500 observations. But calculation of tempH variable gives an error because C and A are of different dimensions. Is any way to subtract these two matrices C and A to get desirable output as tempH of 10 X 500. Please help.

2 Comments

thanks walter for editing as I am new to Matlab answers and will look around the tutorial for better formatting.

Sign in to comment.

 Accepted Answer

In your statement
InputWeight=rand(NumberofHiddenNeurons,NumberofInputNeurons)*2-1;
you are generating a complete matrix of random values of size NumberofHiddenNeurons by NumberofInputNeurons . But then in the next line, you use one particular value,
InputWeight1(j,M)
and a different particular value,
InputWeight1(j,NumberofInputNeurons+1)
Notice second column number, NumberOfInputNeurons+1, is 1 more than number of columns in the randomly generated matrix, so that is likely to get you an index out of range error.
Then for the next iteration of the innermost loop, you throw away the random matrix and build another random matrix. What was the point of that? Why not just generate two random scalars and use them? Or one random scalar even? With a pair of random values you do skew the resulting distribution considerably, but we cannot tell what you are wanting to do.
Oh wait, you generate the random values into the variable InputWeight, but you pull values out of InputWeight1, which is not defined. No obvious point here...
Perhaps what you want is to, exactly once before the double nested loop, use
InputWeight1 = rand(NumberofHiddenNeurons, NumberofInputNeurons+1)*2-1;
and then do not have any rand() inside the loops, just the assignment to C(j,M) ? If that is the case, then the inner loop could be vectorized away.

7 Comments

In your second set of loops, you have as a sub-expression
bsxfun(@minus,(A(g,k)),C(g,:))
The first of those is a scalar, so there is no point in using bsxfun() there. More efficient would be
(A(g,k) - C(g,:))
Again sorry for my mistake in code as I copied my testing code with Inputweight1, but now I update the code and double check precisely. Please suggest the strategies to compute tempH as output
To check: when M gets to NumberofInputNeurons then you want C(j,M) to be
- InputWeight(j,NumberofInputNeurons).^2 / 2;
?
that's correct of no point of using the bsxfun(), but the problem is if I use (A(g,k) - C(g,:)), then it give a dimension error, as A= 9 X 500, and C will be 10 X 9, even if I transpose C, it will still not able to subtract due to different dimensions.
C(j,M)= -((InputWeight(j,M))/2*(InputWeight(j,NumberofInputNeurons+ x)));
Now here, the x is the parameter which need to be defined,on which I am working. The addition of x change the C(j,M) values but not changes the dimensions.
Are you sure it shouldn't instead be InputWeight(j,NumberofInputNeurons) + x ? If you are going to add x to the index then your InputWeight matrix needs to be larger.
Might I suggest that before the loop,
IWj = InputWeight(:, NumberOfInputNeurons + x);
or however else it should be initialized (taking into account the array bounds). Then inside the loop,
C(j,M)= -((InputWeight(j,M))/2*IWj(j));
In turn that would allow you to vectorize the "j" loop out of existance, replacing it with
C(:,M) = -InputWeight(:,M) / 2 .* IWj;
thanks for suggesting for skipping the loop for C(j), but the issue is still same for subtraction with different dimension matrices as C will come as 15 X 9 and our data A = 500 X 9. The suggested output need to be 10 X 500. I am using this propsed formula, please suggest according to this formula is it subtraction or something else.
Hj = ‖A-Cj‖^2 where j = number of hidden neurons

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!