ARJUN N
ARJUN N

Reputation: 13

How to fix up the error in matrix dimensions in MATLAB R2016b

I am working on a MATLAB code that involves deep learning using neural networks. The image or the data is being fed in the form of matrices. But I am getting an error "Matrix dimensions must agree". Can Someone please help me with this problem?

I tried to solve this problem by using .* in stead of matrix multiplication * but the method didn't work.

Function Deeplearningoriginal:

function [w1,w2,w3,w4] = Deeplearningoriginal(w1,w2,w3,w4,input_Image,correct_output)
alpha=0.01;
N=5;
for k = 1:N
input_Image = reshape( input_Image( :, :,k ),25 ,1);

input_of_hidden_layer1 = w1* input_Image;
output_of_hidden_layer1 = ReLU(input_of_hidden_layer1);

input_of_hidden_layer2 = w2* output_of_hidden_layer1;
output_of_hidden_layer2 = ReLU( input_of_hidden_layer2);


input_of_hidden_layer3 = w3* output_of_hidden_layer2;
output_of_hidden_layer3 = ReLU(input_of_hidden_layer3);

input_of_output_node = w4* output_of_hidden_layer3;
final_output = Softmax(input_of_output_node);

correct_output_transpose = correct_output(k,:);
error = correct_output_transpose - final_output;

delta4 = error;

error_of_hidden_layer3 = w4'* delta4;
delta3 = (input_of_hidden_layer3>0).*error_of_hidden_layer3;

error_of_hidden_layer2 = w3'* delta3;
delta2 = (input_of_hidden_layer2>0).* error_of_hidden_layer2;

error_of_hidden_layer1 = w2'*delta2;
delta1 = (input_of_hidden_layer1>0).* error_of_hidden_layer1;

adjustment_of_w4 = alpha*delta4*output_of_hidden_layer3';
adjustment_of_w3 = alpha*delta3*output_of_hidden_layer2';
adjustment_of_w2 = alpha*delta2*output_of_hidden_layer1';
adjustment_of_w1 = alpha*delta1*reshaped_input_image';

w1 = w1 + adjustment_of_w1;
  w2 = w2 + adjustment_of_w2;
    w3 = w3 + adjustment_of_w3;
      w4 = w4 + adjustment_of_w4;
end
end

Training network:

input_Image = zeros (5,5,5);

input_Image(:,:,1) = [ 1 0 0 1 1;
                   1 1 0 1 1;
                   1 1 0 1 1;
                   1 1 0 1 1;
                   1 0 0 0 1;
                   ];
input_Image(:,:,2) = [ 0 0 0 0 1;
                   1 1 1 1 0;
                   1 0 0 0 1;
                   0 1 1 1 1;
                   0 0 0 0 0;
                   ];
input_Image(:,:,3) = [ 0 0 0 0 1;
                   1 1 0 0 1;
                   1 0 1 0 1;
                   0 0 0 0 0;
                   1 1 1 0 1;
                   ];
input_Image(:,:,4) = [ 1 1 1 0 1;
                   1 1 0 0 1;
                   1 0 1 0 1;
                   0 0 0 0 0;
                   1 1 1 0 1;
                   ];
input_Image(:,:,5) = [ 0 0 0 0 0;
                   0 1 1 1 1;
                   0 0 0 0 1;
                   1 1 1 1 0;
                   0 0 0 0 1;
                   ];
correct_output    =  [ 1 0 0 0 0;
                   0 1 0 0 0;
                   0 0 1 0 0;
                   0 0 0 1 0;
                   0 0 0 0 1;
                  ];
w1 = 2* rand(20,25) -1;
w2 = 2* rand(20,20) -1;
w3 = 2* rand(20,20) -1;
w4 = 2* rand(5,20) -1;

for epoch = 1:100
  [w1,w2,w3,w4] = Deeplearningoriginal(w1,w2,w3,w4,input_Image,correct_output);
end

I expected this code to run but because of the error I am not able to proceed.

Upvotes: 1

Views: 124

Answers (1)

JAC
JAC

Reputation: 466

The problem is the reshape (actually, two problems). After the

input_image = reshape(input_image(:,:,k), 25,1);

input_image is an array with 25 rows and 1 column, whereas w2, w3, and w4 have only 20 columns. To do the matrix multiplication A*B, A must have as many columns as B has rows.

The other problem with the reshape as written is that after the first pass through the loop, input_image is no longer a 5x5x5 array, it is a 25x1 array that contains only the elements of input_image(:,:,1). It is necessary to use a different name on the left-hand-side of the assignment (and throughout the rest of the loop) to avoid loosing the content of input_image.

Hope this helps,

JAC

Upvotes: 2

Related Questions