MATLAB NN toolbox: Error using trainlm

934 views Asked by At

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?

2

There are 2 answers

4
michaeltang On

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);
0
Mehdi Haghgoo On

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.