how to store target into array bpnn?

if true
% code
endhi, i'm working on project of classification images. i have 4 classes. the dataset of the training in the last column is the class label where 4 class. 1-4.
how to write a code to store the target into array. i want to make if the target is 4, it will store 1000. my code below did not work.
loadinput = dlmread('Train.csv');<
x = loadinput;
tempt = loadinput;
x(:,2501)=[];
noofInputSet = length(x(:,1));
disp(noofInputSet);
noofInputNode = length(x(1,:));
noofHiddenNode = noofInputNode / 2;
noofInputBiasNode = 1;
noofHiddenBiasNode = 1;
noofOutputNode = 4;
for r=1:noofInputNode
tempt(:,1)=[];
%store target into array t
for sT=1:noofInputSet
if tempt(sT)==1,
t(sT) =1000;
elseif tempt(sT)==2,
t(sT) = 0100;
elseif tempt(sT)==3,
t(sT)=0010;
elseif tempt (sT)==4,
t(sT)=0001;
end
-can somebody help me fix this

Answers (1)

TARGET VECTORS SHOULD BE COLUMNS OF THE UNIT MATRIX eye(4)
classindices = [ 1 2 3 4 3 2 1]
target = full(ind2vec(classindices))
classindices = vec2ind(target)
Hope this helps.
Thank you for formally accepting my answer
Greg

2 Comments

thank you greg for your answer,,
my code below need 4 class. i dont know how to set the target and get the Mean Square Error. i'm done trying but when i try...the MSE is not change. it just static. i hope you can help me. below is my full code for bpnn training
loadinput = dlmread('BPNNDataTraining.csv'); x = loadinput; tempt = loadinput; x(:,2501)=[];
noofInputSet = length(x(:,1)); disp(noofInputSet); noofInputNode = length(x(1,:)); noofHiddenNode = noofInputNode / 2; noofInputBiasNode = 1; noofHiddenBiasNode = 1; noofOutputNode = 4;
for r=1:2500 tempt(:,1)=[]; end %store target into array t classtarget =[1 2 3 4]; t = full(ind2vec(classtarget)); classtarget = vec2ind(target)
%step 1: Initialize the weight and bias
v=rand(noofInputNode,noofHiddenNode); %32x16 matrix v1=zeros(noofInputNode,noofHiddenNode); %32x16 matrix for momentum purpose b1=rand(noofInputBiasNode,noofHiddenNode); %1x16 array w=rand(noofOutputNode,noofHiddenNode); %1x16 array w1=zeros(noofOutputNode,noofHiddenNode);%1x16 array for momentum purpose b2=rand(noofHiddenBiasNode,noofOutputNode);
%step 3: For each training pair SelectedPopupLeaningRate = get(handles.LearningRatePopupmenu, 'Value'); SelectedPopupmenuMomentumFactor = get(handles.MomentumFactorPopupmenu, 'Value'); SelectedPopupmenuEpoch = get(handles.EpochPopupmenu, 'Value');
switch SelectedPopupLeaningRate case 1 alpha = 0.1; case 2 alpha = 0.2; case 3 alpha = 0.3; case 4 alpha = 0.4; case 5 alpha = 0.5; case 6 alpha = 0.6; case 7 alpha = 0.7; case 8 alpha = 0.8; case 9 alpha = 0.9; end
switch SelectedPopupmenuMomentumFactor case 1 mf = 0.0; case 2 mf = 0.1; case 3 mf = 0.2; case 4 mf = 0.3; case 5 mf = 0.4; case 6 mf = 0.5; case 7 mf = 0.6; case 8 mf = 0.7; case 9 mf = 0.8; case 10 mf = 0.9;
end
switch SelectedPopupmenuEpoch case 1 finalepoch = 50; case 2 finalepoch = 100; case 3 finalepoch = 500; case 4 finalepoch = 1000; case 5 finalepoch = 2000; case 6 finalepoch = 4000; end
con=1; epoch=0;
PlotGraph = plot(handles.RMSEGraph,0); set(PlotGraph,'Color','blue','LineWidth',2) xlabel('Number of Epoch') ylabel('Root Mean Square Error (RMSE)')
while con e1=0; for I=1:noofInputSet, %input data
%Feed forward stage
%step 4: Calculate Z_in
for j=1:noofHiddenNode
zin(j)=b1(j);
for i=1:noofInputNode
zin(j)=zin(j)+x(I,i)*v(i,j);
end
%calculate the output signal
z(j)=binarysigmoid(zin(j));
end
%Step 5: Calculate Y_in
for k=1:noofOutputNode
yin(k)=b2(k);
for j=1:noofHiddenNode
yin(k)=yin(k)+z(j)*w(k,j);
end
%calculate the output signal
y(k)=binarysigmoid(yin(k));
%ty(I,k)=y(k);
end
%backpropagationtraining of Error
%step 6: Calculate the output
for k=1:noofOutputNode %lowercase delta k
delk(k)=(t(I)-y(k))*binarysigmoid1(yin(k));
end
%step 7
for j=1:noofHiddenNode %lowercase delta inj
for k=1:noofOutputNode
delinj(j)=delk(k)*w(k,j);
end
end
for j=1:noofHiddenNode%lowercase delta j
delj(j)=delinj(j)*binarysigmoid1(zin(j));
end
%Weight updation
%step 8
for i=1:noofInputNode%uppercase delta v(i,j)
for j=1:noofHiddenNode
delv(i,j)=alpha*delj(j)*x(I,i)+mf*(v(i,j)-v1(i,j));
end
end
delb1=alpha*delj;
v=v+delv;
b1=b1+delb1;
for j=1:noofHiddenNode %uppercase delta w(j,k)
for k=1:noofOutputNode
delw(k,j)=alpha*delk(k)*z(j)+mf*(w(k,j)-w1(k,j));
end
end
delb2=alpha*delk; %uppercase delta bias2(j,k)
w=w+delw;
b2=b2+delb2;
w1=w; %store in current w to w1 for momentum purpose
v1=v; %store in current v to v1 for momentum purpose
for k=1:k
mse = ((t(I)-y(k))^2);
e1=e1+mse;
end
end
e=sqrt(e1/noofInputSet);
disp(e);
%step 9
%test stopping condition
if e<0.005
con=0;
end
epoch=epoch+1;
graphMSE(epoch) = e;
if epoch==finalepoch;
con=0;
end
PlotGraph = plot(handles.RMSEGraph,graphMSE);
set(handles.uitable2,'Data',v);
set(handles.uitable3,'Data',b1);
set(handles.uitable4,'Data',w);
set(handles.uitable5,'Data',b2);
set(PlotGraph,'Color','blue','LineWidth',2)
xlabel('Number of Epoch')
ylabel('Root Mean Square Error (RMSE)')
stre = num2str(e);
set(handles.MeanSquareErrorText2,'string',stre);
strepoch = num2str(epoch);
set(handles.CurrentEpochText2,'string',strepoch);
pause(.0001);
end
You probably won't get any help until you correctly format your code.
Greg

Sign in to comment.

Asked:

on 14 Jun 2016

Commented:

on 16 Jun 2016

Community Treasure Hunt

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

Start Hunting!