Arrays have incompatible sizes for this operation..
6 views (last 30 days)
Show older comments
Arrays have incompatible sizes for this operation. Error is n2 = W2'.*a1 + b2; a2 = sig2(n2);
My code:
for epoch = 1:N_ep
mixup = randperm(N_train);
for j = 1:N_train
i = mixup(j);
% get X_train(:,i) as an input to the network
a1 = X_train(:,i);
% forward prop to the next layer, activate it, repeat
n2 = W2'.*a1 + b2; a2 = sig2(n2);
n3 = W3'.*a2 + b3; a3 = sig3(n3);
n4 = W4'.*a3 + b4; a4 = sig4(n4);
n5 = W5'.*a4 + b5; a5 = sig5(n5);
% this is then the output
y = a5;
4 Comments
Walter Roberson
on 8 Aug 2022
A_1 = readmatrix('MNIST_train_1000.csv');
We do not know what size that is returning so we cannot calculate the size of X_train so we cannot calculate the size of a1
Ni = 784; % number of input nodes
No = 10; % number of output nodes
% set up weights and biases
W2 = 0.5-rand(784,24);b2 = zeros(24,1);
Is it a coincidence that the magic number 784 in the definition of W2 happens to be the same as the value of Ni?
X_train has to have that magic 784 rows for this code to work, but that is not tested anywhere. Instead Ntrain is calculated, implying that the size is not fixed (since it is not immediately followed by testing against 784 or Ni.) You should do one of the following:
- reject the file if X_train is not exactly (hard-coded) 784 rows
- reject the file if it is not exactly Ni rows
- reject the file if it has fewer than 784 or Ni rows, and use only the first 784 or Ni rows if it is larger
- set Ni to Ntrain and create the matrices with Ni rows instead of 784.
Answers (0)
See Also
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!