MatlabR2016B中矩阵维数误差的修正

MatlabR2016B中矩阵维数误差的修正,matlab,Matlab,我正在编写一个MATLAB代码,该代码涉及使用神经网络进行深入学习。 图像或数据以矩阵的形式输入。 但我得到了一个错误“矩阵维数必须一致”。 有人能帮我解决这个问题吗 我试图用*代替矩阵乘法*来解决这个问题,但这个方法不起作用 功能深度学习原版: 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

我正在编写一个MATLAB代码,该代码涉及使用神经网络进行深入学习。 图像或数据以矩阵的形式输入。 但我得到了一个错误“矩阵维数必须一致”。 有人能帮我解决这个问题吗

我试图用
*
代替矩阵乘法
*
来解决这个问题,但这个方法不起作用

功能<代码>深度学习原版:

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
培训网络:

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

我希望此代码能够运行,但由于错误,我无法继续。

问题在于
重塑(实际上,有两个问题)。之后

input_image = reshape(input_image(:,:,k), 25,1);
input\u image
是一个25行1列的数组,而
w2
w3
w4
只有20列。要进行矩阵乘法,
A*B
A
的列数必须与
B
的行数相同

编写的
重塑
的另一个问题是,在第一次通过循环后,
输入图像
不再是
5x5x5
数组,而是一个
25x1
数组,它只包含
输入图像(:,:,1)
的元素。有必要在赋值的左侧(以及整个循环的其余部分)使用不同的名称,以避免丢失
input\u image
的内容

希望这有帮助


JAC

谢谢你,JAC,JAC我试图解决矩阵维数错误,但我还是没有得到正确的输出,请编辑我的代码来解决这个问题。@ARJUN N.编辑你的代码没有多大帮助,主要是因为我不知道你想做什么。神经网络不是我的专业。您可以先检查数据。例如,什么是
w1
w4
?为什么尺寸不同?只要它们没有25列,就存在与重塑后的
input_图像
尺寸不匹配的情况,这将持续触发“矩阵尺寸必须一致”错误消息。首先尝试解决这部分问题。实际上,我正在尝试获取这些[1 0 0 0;0 1 0 0 0;0 0 1 0 0;0 0 0 0 1 0;0 0 0 0 0 0 1;];作为所需的输出,当我将输入图像作为输入时,好的,我将尝试对此进行修复。再次感谢。谢谢,请更正上述代码中的错误,我试图更正,但我没有得到正确的输出。。