How to train a neural network using validation in Matlab - matlab

I'm trying to show the difference between results of an ANN trained with and without validation...
assume that I'm trying to train a ANN how a sin function should work:
this is gonna be my training data:
x = -1:0.05:1;
t = sin(2*pi*x)+0.01*randn(size(x));
and for validation data I gonna use this:
val.X = -0.975:.05:0.975;
val.T = sin(2*pi*val.X)+0.01*randn(size(val.X));
then I configure my net as follows:
net = feedforwardnet(10,'trainlm');
net.trainParam.show = 50;
net.trainParam.epochs = 300;
then I train net without validation :
[net1, tr1] = train(net,x,t);
and for training with validation I use this code:
[net2,tr2]=train(net,x,t,[],[],val);
but it doesn't work!?
EDIT:
the error says: "Error weights EW is not a matrix or cell array."
I wonder if you could tell me how to validate training ANN by custom data!?

Related

Self Organizing Map to split datasets

I am trying to use Self Organizing Map to split datasets into training, validation and test sets.
I created the SOM model,
dimension1 = 10;
dimension2 = 10;
net = selforgmap([dimension1 dimension2],100,3,'hextop','linkdist');
[net, tr] = train(net, cancer);
however when I am trying to partition the dataset using
net.divideParam.trainRatio = 0.6;
net.divideParam.valRatio = 0.2;
net.divideParam.testRatio = 0.2;
I am getting an error
"Error in network/subsasgn>network_subsasgn (line 456)
if isempty(err), [net,err]=setDivideParam(net,divideParam); end
Error in network/subsasgn (line 10)
net = network_subsasgn(net,subscripts,v,netname);"
Could someone please provide me some guidelines how split datasets using SOM in Matlab?
Code Image
You can not use trainRatio, valRatio and testRatio in SOM.
These can be used in other neural networks such as MLP.

How to perform two group classification with deep neural network? (Matlab)

I'm new in machine learning (and to stackoverflow as well) and i want to make some classification tasks. I performed two group classifications on my data set (field of speech acoustics) with LIBSVM and Matlab's Pattern Recignition Tool from the Neural network toolbox to create a simple network with one hidden layer. In the hope of higher classification results i want to try Deep Neural Networks, and i found this code: http://www.mathworks.com/matlabcentral/fileexchange/42853-deep-neural-network
I have some difficulty understanding it.
My data is constructed of 127 samples of 19 parameters, so my input number is 19. I want to classify them in two groups: 0 and 1, so my output number is 1. The values in my data set are normalized between 0 and 1.
My code is the following:
clear all
clc
addpath('..');
load('data.mat')
inputdata = inputs;
outputdata = outputs;
datanum = 127;
outputnum = 1;
hiddennum = 3;
inputnum = 19;
% rbm = randRBM(inputnum, outputnum);
% rbm = pretrainRBM( rbm, inputdata );
dbn = randDBN([inputnum, hiddennum, outputnum]);
dbn = pretrainDBN( dbn, inputdata );
dbn = SetLinearMapping( dbn, inputdata, outputdata );
dbn = trainDBN( dbn, inputdata, outputdata );
estimate = v2h( dbn, inputdata )
[rmse AveErrNum] = CalcRmse(dbn, inputdata, outputdata)
The code runs. The rmse is 0.4183, the AveErrNum is 0.1969. What i need is the classification accuracy between my targets (stored in outputdata) and the networks predictions (Accuracy = data classified correctly / all data).
Where do i find the networks predictions after binarization?
Do I use the right type of network for my classification?
Don't I need to divide my data into Training, Validation and Testing samples (like in the case of a simple neural network with one hidden layer)?
Thanks in advance for any help!

Matlab Neural Network for Classes - Unseen Data

Say I create a neural network to separate classes:
X1; %Some data in Class 1 100x2
X2; %Some data in Class 2 100x2
classInput = [X1;X2];
negative = zeros(N, 1);
positive = ones(N,1);
classTarget = [positive negative; negative positive];
net = feedforwardnet(20);
net = configure(net, classInput, classTarget);
net = train(net, classInput, classTarget);
%output of training data
output = net(classInput);
I can plot the classes and they are correctly separated:
figure();
hold on
style = {'ro' 'bx'};
for i=1:(2*N)
plot(classInput(i,1),classInput(i,2), style{round(output(i,1))+1});
end
However, how can I apply the network that's just been trained to unseen data? There must be a model which is generated by the network that can be applied to new data?
EDIT: Using sim:
Once the network is trained, if I use sim on the training data:
[Z,Xf,Af] = sim(net,classInput);
The result is as expected. But this only works if the input is of the same size. If for example I want to evalute an individual data point:
[Z1,Xf,Af] = sim(net,[1,2]);
size(Z) == size(Z1), but this clearly doesn't make sense? Surely I can evaluate a single data point?
I'm the OP,
I had assumed that the rows of the input matrices were the data samples and the columns were the "categories", this is the other way around. Transposing the matrices before inputting them to the train() function fixes this.

Error using Zeros in Matlab while training

I am Trying to train my Neural Network using vInputs and vTargets to create a model net
But I keep getting this irritating Error no matter what I try to correct in my code:
ERROR: Error using zeros.Maximum variable size allowed by the program is exceeded.
I dont find any such big data created by my code and I have a RAM of 4GB
I have 230 images which I need to classify in 3 parts 'Sedan', 'SUV', 'Hatchback'
image size [15x26]
I used this code to convert single image's features extracted by gabor filter to Feature Vector
function IMVECTOR = im2vec (img)
load Gabor;
img = adapthisteq(img);
Features75x208 = cell(5,8);
for s = 1:5
for j = 1:8
Features75x208{s,j} = mminmax(abs(ifft2(G{s,j}.*fft2(double(img),32,32),15,26)));
end
end
Features25x70 = cell2mat(Features75x208);
Features25x70 (3:3:end,:)=[];
Features25x70 (2:2:end,:)=[];
Features25x70 (:,3:3:end)=[];
Features25x70 (:,2:2:end)=[];
IMVECTOR = reshape (Features25x70,[1750 1]);
Now after the feature vector is created I made vInputs using same function for 230 images.
Thus I got
1. vInputs=[1750x230].
2. vTargets=[4x230].
But whenever I run the Project I get error in function.
function net = create_pr_net(inputs,targets)
%CREATE_PR_NET Creates and trains a pattern recognition neural network.
% Create Network
numHiddenNeurons = 35;
net = newpr(inputs,targets,numHiddenNeurons);
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;
% Train and Apply Network
[net,tr] = train(net,inputs,targets);% <== ERROR OCCURS HERE<==
outputs = sim(net,inputs);
% Plot
plotperf(tr);
plotconfusion(targets,outputs);
save net net
Here is the complete Error:
Error using zeros
Maximum variable size allowed by the program is exceeded.
Error in nnMex2.codeHints (line 117)
hints.TEMP =
zeros(1,ceil(tempSize/8),'double');
Error in nncalc.setup2 (line 13)
calcHints = calcMode.codeHints(calcHints);
Error in network/train (line 306)
[calcLib,calcNet] = nncalc.setup2(calcMode,calcNet,calcData,calcHints);
Error in create_pr_net (line 14)
[net,tr] = train(net,inputs,targets);
Error in executeMe (line 22)
net=create_pr_net(vInputs,vTargets);
Please Help me and ask me if I missed anything and need to specify some more details.
value of tempSize:
EDIT: Well I figured it out that as I am using 32bit system so I can address a maximum of 2^32 = 4.294967e+09 at a time.
While if I was using 64bit I could be able to address about 2^64 = 9.22337e+18 address at a time.
So can you guys give me some idea about how to make it work on my system.

How can I resolve output data size error about neural network training in Matlab?

I have a input file containing (17x127) data (Force) and a target file of (3x127)(True Stress).
I have written the following code for training the neural networks:
p=Force;
t=T_Stress;
net =newff(minmax(p),[10,1],{'tansig','purelin'},'trainlm');
net.trainParam.lr = .05; %Learning Rate
net.trainParam.epochs = 300; %Max Ephocs
net.trainParam.goal = 1e-5; %Training Goal in Mean Sqared Error
net.trainParam.show = 50; %# of ephocs in display
[net,tr1] = train(net,p,t);
o1 = sim(net,p)
However I get the following errors:
??? Error using ==> trainlm at 109 Output data size does not match
net.outputs{2}.size.
Error in ==> network.train at 107
[net,tr] = feval(net.trainFcn,net,X,T,Xi,Ai,EW,net.trainParam);
How can I resolve this error?
Try to give the training parameters associated with the "trainlm" training function by looking for "trainlm" in Matlab help
By calling newff with the parameter [10,1] you created a neural network with 10 neurons on the hidden layer and 1 on the output layer.
This does not match the number of outputs you are using. (3x127)
So either change the the newff call or use a different training dataset.
As above commented "
By calling newff with the parameter [10,1] you created a neural
network with 10 neurons on the hidden layer and 1 on the output layer
" you keep the value of output layer as 3 as you have three targets. The command will be as follows:
net =newff(minmax(p),[10,3],{'tansig','purelin'},'trainlm');