MATLAB NN toolbox: Error using trainlm - matlab

I have a 90×8 dataset that I feature-extracted (by summing 1's in every 10×10 cell) from 90 character images i.e. digits 1-9. Every row represents an image.
I am trying to use following code to train a neural network and recognize new input images(that are digits between 1 and 9 inclusive):
net.trainFcn='traingdx';
net.performFcn='sse';
net.trainParam.goal=0.1;
net.trainParam.show=20;
net.trainParam.epochs=5000;
net.trainParam.mc=0.95;
net =newff(minmax(datasetNormalized'),[20 9],{'logsig' 'logsig'});
T=reshape(repmat([1:9],10,1),1,90);
[net,tr]=train(net,datasetNormalized,T);
Afterwards I want to use the following to recognize new images using the trained network. m is an image character that has also been feature extracted.
[a,m]=max(sim(net,m));
disp(b);
I am getting the following errors and I don't have any idea how to solve it:
Error using trainlm (line 109)
Inputs and targets have different numbers of samples.
Error in network/train (line 106) [net,tr] =
feval(net.trainFcn,net,X,T,Xi,Ai,EW,net.trainParam);
Error in Neural (line 55) [net,tr]=train(net,datasetNormalized,T);
Note: datasetNormalized is my dataset normalized in [0,1].
Which part causes the problem?

Inputs and targets have different numbers of samples. it seems to be the problem
T=reshape(repmat([1:9],10,1),1,90) --> T=reshape(repmat([1:9],10,1),90,1)
[net,tr]=train(net,datasetNormalized,T); --> [net,tr]=train(net,datasetNormalized',T);

T is to be used as target for the network; Therefore, following a friend's advice, I defined T as a 9*90 array in such a way that the first 10 columns have 1 in their first row-other rows being zero, the second 10 columns have 1 in their second row, and so on
T=zeros(9,90);
for j=1:90
i=ceil(j/10);
T(i,j)=1;
end
[net,tr]=train(net,datasetNormalized',T);
This solved the error I was getting upon training network, though I'm not still sure how it's going to be mapped to input characters and determine them.

Related

How to combine imagedatastores for training two-input neural network on MATLAB?

I have two sets of 58 images each which I have to feed as input to a multiple-input network.
To do so, I first read each of the sets onto a different Image Datastore, as shown on the images
Reading the first dataset:
And then, the second dataset:
Then I merge the two datastores by using 'combine'. However, according to the documentation available here, I should be reading a three-column cell array, wherein the third column would be the labels. I get only two columns when reading though:
If I try to give this combined dataset as input to my network (which has two inputs and one output) I get the following error message:
>> net = trainNetwork(imdsCombined,lgraph_1,options);
Error using trainNetwork (line 170)
Invalid training data for multiple-input network. For a network with 2 inputs and 1 output, the datastore read function must
return an M-by-3 cell array, but it returns an M-by-2 cell array.
I am using R2019b by the way.
So, is there a way to insert the labels column onto the cell arrays read from the image datastore? Or is there something else I need to do?
Thanks for all the help.
Regards,

Error during rm ANOVA (Matlab) with random factor

I am trying to run a repeated measures ANOVA in Matlab with 4 factors including one factor representing my subjects which I want as a random factor.
The code I have is as follows:
[p,table,stats] = anovan(COORDS_SUBJ_II,{group_hand,group_stim,group_time,group_subs},'random',4,'varnames',{'HAND','STIM','TIME','SUBS'});
Here, all variables have the same dimension, which is 1350x1(all types are 'double'). I checked my code with some proposed code on the net and it matches, yet I keep getting the following error...
Error using chi2inv (line 3)
P and V must be of common size or scalars
Error in anovan>varcompest (line 838)
L = msTerm .* dfTerm ./ chi2inv(1-alpha/2,dfTerm);
Error in anovan>getRandomInfo (line 811)
[varest,varci] = varcompest(ems,randomterms,msTerm,dfTerm,alpha);
Error in anovan (line 296)
getRandomInfo(msterm,dfterm,mse,dfe,emsMat,randomterm,...
My dependent variable (COORDS_SUBJ_II) has a couple of NaN's in it, although I ran the code once where I replaced those NaN's with random numbers and it still gives me the same error. I am kind of lost right now and would appreciate any help.
Best
ty
Found it out. A toolbox I downloaded a while ago also had the chi2inv command and this prompted the error =)

averaging images, code doesn't work

I have this matlab code for creating averages of several images:
cd('C:\images')
all_images=dir('*.jpg');
[sj,xx]=size(all_images);
j=1;
file_name=char(all_images(j).name);
tmp=imread(file_name);
[m,n]=size(tmp);
template=zeros(m,n);
for i=1:length(all_images)
file_name=char(all_images(i).name);
tmp=double(imread(file_name));
template=template+tmp;
end
template=template/length(all_images);
imagesc(template)
imwrite(uint8(template),'template.jpg','jpg')
The case is that I obtained the following error:
Error
using + Matrix dimensions must agree.
Error
in averagetemplate (line 15) template=template+tmp;
Any idea? I have to say that I am newbie in matlab coding.
Thanks
Just change [m,n]=size(tmp) in line 6 to [m,n,~]=size(tmp)and your problem will be solved.
In the below code :
[m,n]=size(tmp)
Matlab is computing n*3 as the column of matrix so you will get the dimension error in next few line.
You are overwriting your temp variable with what appears to be a single value here:
tmp=double(imread(file_name));
and it may be different dimensions M x N regardless. Matlab is saying to add matricies they have to have same number of rows and cols.

how to compare the expected values with actual values using matlab's neural network toolbox?

I have been using matlab's neural network toolbox lately for my research. I created some neural networks using fitting tool of this toolbox. Now I encountered a problem while testing the network with new data. Basically, I want to test the network with some new data, which do not have the same sample number as the network. For example, I have created the network with the input of 12x36 matrix (12 variables, 36 samples) and output of 1x36 vector (1 variable, 36 samples). Now, I want to get the results for a new data (12x11520 (12 variables and 11520 samples)). However, when I put this data into the network and get the results, I only get an output vector of 1x36 (1 variable 36 samples) while I was expecting to get the results as 1x11520 (1 variable and 11520 samples). I am using the below line to get the results from the neural network named as network.
output_estimate = network(input');
I also tried the line below to get the results but the result did not change.
output_estimate = sim(network,input');
Could you please help me understand this and get the output as the same sample length so that I can compare the expected and actual results for the new data.
Thank you very much in advance.
Irem

Calling function and getting - not enough input arguments, even though syntax is correct

I'm teaching myself classification, I read and understood the MatLab online help of the simple LDA classifier which uses the fisher iris dataset.
I have now moved to SVM. But even though I use the exact syntax from the help page I get an error of either not enough or too many input arguments.
I have made trained my SVMClassifier using svmtrain via the command:
SVMStruct = svmtrain(training,labels);
Where training is a 207 by 900 training matrix. There are 207 samples and 900 HoG descriptors or features. Similarly labels is a 207 by 1 column vector consisting of either +1 or -1 for their respective samples.
I then wanted to test it and see if this works by calling:
Group = svmclassify(SVMStruct,sample,'Showplot',true)
Where sample is a 2 by 900 matrix containing 2 test samples. I was expecting to get +1 and -1 as these are what the test samples should be labelled. But I get the error:
Too many input arguments.
And when I use the command
Group = svmclassify(SVMStruct,sample)
I get the error
Not enough input arguments.
You might have overloaded svmclassify function.
try
>> which svmclassify
to verify that you are actually calling the right function.
In case that you overloaded the function (that is, created a different function with the same name svmclassify) and it is located higher in your path then you'll need to rename the overloaded function and run svmclassify again.