Clear Filters
Clear Filters

??? Error using ==> mtimes MTIMES is not fully supported for integer classes. At least one input must be scalar. (How can I solve this??)

2 views (last 30 days)
%user specified values
hidden_neurons = 100;
epochs = 1000000000;
% ------- load in the data -------
% for getting row image into a single variable
%for i=1:3
%tg_in=imread(1);
%r_in=reshape(tg_in,4800,1);
%tg_out()=r_in();
%end
temp1=imread('im1.JPG');
temp2=imread('im2.JPG');
tr1=reshape(temp1,1,4800);
tr2=reshape(temp2,1,4800);
%train_inp = [192 210 123 143;132 168 124 136];
%train_out = [1;2];
train_inp=[tr1;tr2];
train_out=[0;1];
% check same number of patterns in each
if size(train_inp,1) ~= size(train_out,1)
disp('ERROR: data mismatch')
return
end
%standardise the data to mean=0 and standard deviation=1
%inputs problem
mu_inp = mean(train_inp);
sigma_inp = std(double(train_inp));
train_inp = (train_inp(:,:) - mu_inp(:,1)) / sigma_inp(:,1);
%outputs
train_out = train_out';
mu_out = mean(train_out);
sigma_out = std(double(train_out));
train_out = (train_out(:,:) - mu_out(:,1)) / sigma_out(:,1);
train_out = train_out';
%read how many patterns
patterns = size(train_inp,1);
%add a bias as an input
bias = ones(patterns,1);
train_inp = [train_inp bias];
%read how many inputs
inputs = size(train_inp,2);
%---------- data loaded ------------
%--------- add some control buttons ---------
%add button for early stopping
hstop = uicontrol('Style','PushButton','String','Stop', 'Position', [5 5 70 20],'callback','earlystop = 1;');
earlystop = 0;
%add button for resetting weights
hreset = uicontrol('Style','PushButton','String','Reset Wts', 'Position', get(hstop,'position')+[75 0 0 0],'callback','reset = 1;');
reset = 0;
%add slider to adjust the learning rate
hlr = uicontrol('Style','slider','value',.1,'Min',.01,'Max',1,'SliderStep',[0.01 0.1],'Position', get(hreset,'position')+[75 0 100 0]);
% ---------- set weights -----------------
%set initial random weights
weight_input_hidden = (randn(inputs,hidden_neurons) - 0.5)/10;
weight_hidden_output = (randn(1,hidden_neurons) - 0.5)/10;
%-----------------------------------
%--- Learning Starts Here! ---------
%-----------------------------------
%do a number of epochs
for iter = 1:epochs
%get the learning rate from the slider
alr = get(hlr,'value');
blr = alr / 10;
%loop through the patterns, selecting randomly
for j = 1:patterns
%select a random pattern
patnum = round((rand * patterns) + 0.5);
if patnum > patterns
patnum = patterns;
elseif patnum < 1
patnum = 1;
end
%set the current pattern
this_pat = train_inp(patnum,:);
act = train_out(patnum,1);
%calculate the current error for this pattern
hval = (tanh(this_pat*weight_input_hidden))';
pred = hval'*weight_hidden_output';
error = pred - act;
% adjust weight hidden - output
delta_HO = error.*blr .*hval;
weight_hidden_output = weight_hidden_output - delta_HO';
% adjust the weights input - hidden
delta_IH= alr.*error.*weight_hidden_output'.*(1-(hval.^2))*this_pat;
weight_input_hidden = weight_input_hidden - delta_IH';
end
% -- another epoch finished
%plot overall network error at end of each epoch
pred = weight_hidden_output*tanh(train_inp*weight_input_hidden)';
error = pred' - train_out;
err((iter)) = (sum(error.^2))^0.5;
figure(1);
plot(err)
%reset weights if requested
if reset
weight_input_hidden = (randn(inputs,hidden_neurons) - 0.5)/10;
weight_hidden_output = (randn(1,hidden_neurons) - 0.5)/10;
fprintf('weights reaset after %d epochs\n',iter);
reset = 0;
end
%stop if requested
if earlystop
fprintf('stopped at epoch: %d\n',iter);
break
end
%stop if error is small
if err(iter) < 0.01
fprintf('converged at epoch: %d\n',iter);
break
end
end
%-----FINISHED---------
%display actual,predicted & error
fprintf('state after %d epochs\n',iter);
a = (train_out* sigma_out(:,1)) + mu_out(:,1);
b = (pred'* sigma_out(:,1)) + mu_out(:,1);
Result=[b]

Accepted Answer

Walter Roberson
Walter Roberson on 7 Jun 2015
Replace
temp1=imread('im1.JPG');
temp2=imread('im2.JPG');
with
temp1 = double(imread('im1.JPG'));
temp2 = double(imread('im2.JPG'));
I address the issue there rather than later because you had other places in the code where it was going to be a problem that you were working with uint8();
  2 Comments
amenah mwuafaq
amenah mwuafaq on 27 Feb 2020
I got the same error, and I tried this solution.
temp1 = double(imread('im1.JPG'));
The problem was solved, but the image was distorted. I want the image to remain without distortion. Is there another solution?

Sign in to comment.

More Answers (1)

Vishnupriya C
Vishnupriya C on 25 Sep 2016
when i have used double to solve this matrix multiplication error it showed the error as out of memory.can anyone suggest a solution?
  2 Comments
Walter Roberson
Walter Roberson on 25 Sep 2016
Remember that when you use A * B, that the size of the output array will be size(A,1) by size(B,2) -- the number of rows of the first by the number of columns of the second. That might require more memory than you have available. The temporary memory required for the calculation should be at most a vector max(size(A,1),size(B,1)) long.
You should check to see if what you need is instead A .* B, which is element-by-element multiplication, A(I,J) * B(I,J)
Generally when you do matrix multiplication using * the precision required for the output exceeds the precision of the input. If you are working with large matrices such that double() of the output will be too large for your memory but in native precision it would not be too large, and you are certain that the result of multiplication will not exceed the original precision, then you can do the multiplication in "chunks". For example instead of A * B you can do
out = zeros(size(A,1), size(B,2), class(A));
dB = double(B);
for row = 1 : size(A,1)
out(row,:) = double(A(row,:)) * dB;
end
clear dB
This reduces the amount that has to be converted to double precision at any one time to be size(B) together with a vector size(A,2) . If your B is notably bigger than your A then you can use a similar kind of manipulation to instead take double(A) as your working matrix.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!