【问题标题】:How to fix up the error in matrix dimensions in MATLAB R2016b如何修复 MATLAB R2016b 中的矩阵维度错误
【发布时间】:2019-01-04 17:59:29
【问题描述】:

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

我尝试通过使用.* 而不是矩阵乘法* 来解决此问题,但该方法不起作用。

函数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

训练网络:

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

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

【问题讨论】:

  • 谢谢,请纠正上面代码中的错误,我试图纠正,但我没有得到正确的输出..

标签: matlab


【解决方案1】:

问题是reshape(其实是两个问题)。之后

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

input_image 是一个 25 行 1 列的数组,而 w2w3w4 只有 20 列。要进行矩阵乘法A*BA 的列数必须与B 的行数一样多。

reshape 所写的另一个问题是,在第一次通过循环后,input_image 不再是 5x5x5 数组,而是一个仅包含 @987654334 元素的 25x1 数组@。有必要在分配的左侧(以及整个循环的其余部分)使用不同的名称,以避免丢失 input_image 的内容。

希望对你有帮助,

江淮

【讨论】:

  • 谢谢江淮,江淮我试图解决矩阵尺寸错误,但我又没有得到正确的输出,请编辑我的代码来解决这个问题。
  • @ARJUN N。这对我编辑你的代码没有多大帮助,主要是因为我不知道你在做什么。神经网络不是我的领域。您可以先尝试检查您的数据。例如w1w4 是什么?为什么它们的大小不同?只要它们每列不包含 25 列,就会出现与重塑的 input_image 的尺寸不匹配,这将继续触发“矩阵尺寸必须一致”错误消息。尝试先解决这部分问题。
  • 我实际上是在尝试获取这些 [ 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; ];作为所需的输出,当我将输入图像作为输入时,好的,我会尝试修复它。再次感谢您。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
相关资源
最近更新 更多