Error using +. Matrix dimensions must agree

4 Ansichten (letzte 30 Tage)
Oman Wisni
Oman Wisni am 11 Nov. 2018
Kommentiert: Oman Wisni am 12 Nov. 2018
Hi, I'm trying to create neural network for train my data. But I get error. I though its right but which one should I fix. Here my code
%clc;
close all;
clearvars;
% Multilayer Perceptron (MLP) neural networks.
N = 220; %sum of data
D = 25 ; %dimension
K = 11; %target
h = 500; %sum of neuron
load datatrain1.mat
Train1= cell2mat(Train);
load classtrain1.mat
CTrain1 = cell2mat(TTrain);
b1 = randn(1,h);%bias from input to hidden
W1 = randn(D,h);%weight from input to hidden
b2 = randn(1,K);%bias from hidden to output
W2 = randn(h,K);%weight from hidden to output
%feedfoward
for efoch = 1 : 1000
H = Train1*W1+b1;
Y = H*W2+b2;
end
I get error in variable H, like this Error using +. Matrix dimensions must agree.
Any help for fix my code will must appreciated. Thanks
  2 Kommentare
madhan ravi
madhan ravi am 11 Nov. 2018
upload .mat files
Oman Wisni
Oman Wisni am 11 Nov. 2018
yes sir, I already attached my file .mat. But I'm not sure it can open in matlab, cause before I ever upload and download, when I download the format of file is different. And when I upload I no get reply.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Guillaume
Guillaume am 11 Nov. 2018
Bearbeitet: Guillaume am 11 Nov. 2018
The error message is very clear, you're trying to add two matrices of different size, which is never possible. Since b1 is a 1xh vector, Train1*W1 must be a 1xh vector, and since W1 is a Dxh matrix, Train1 must be a 1xD vector for the product to be 1xh.
Therefore, your train1 is not 1xD. There is nothing we can do about that. Either use a train1 matrix that is 1xD or change your D to reflect the size of train1.
Note that it is never a good idea to hardcode the size of matrices. It is always safer to ask matlab for the actually size:
D = size(train1, 2);
would ensure it always match train1. Of course, train1 must only have one row.
edit: got confused between D and h but see comment below.
  10 Kommentare
Guillaume
Guillaume am 12 Nov. 2018
Sorry, it should have been @plus indeed.
Oman Wisni
Oman Wisni am 12 Nov. 2018
Yes sir. Thanks

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by