How do I display red color in true cases for a training data set which has 1000 rand values ?

Here is the program which I am using for generating true cases for green color.
% Define number of nodes in each layer
nLayers = 3;
nNodesL1 = 3;
nNodesL2 = 3;
nNodesL3 = 1;
% Initialize vectors and matrices
W1 = [ -5 5 -5; -10 20 -10; -5 5 -5 ];
W2 = [ 10 25 10 ];
b1 = [ -5 -1 -5 ]';
b2 = [-5];
% Pack the individual weights and biases into a cell array
arrayW{1} = W1;
arrayW{2} = W2;
arrayB{1} = b1;
arrayB{2} = b2;
% Define the training set
nTr = 1000;
xTrSet = rand(3,nTr);
yThresh = 0.75;
yTrSet = ( xTrSet(2,:)./ ( xTrSet(1,:).^2 + xTrSet(2,:).^2 + xTrSet(3,:).^2 ) .^ 0.5 ) > yThresh;
imshow(xTrSet(1,:));
% Plot the training set and scores
figure;
hold on
% Specify number of columns for display
nPlotCols = 40;
% Initialize row offset
yPlotOffset = -1;
for iTr = 1 : nTr
% Check if end of row has been reached
if mod(iTr-1,nPlotCols) == 0
yPlotOffset = yPlotOffset + 1;
xPlotOffset = 0;
end
% Set corner points for each patch based on row and column indices
xCorners = xPlotOffset + [ 0 1 1 0 ]';
yCorners = yPlotOffset + [ 0 0 1 1 ]';
% Set patch colour based on input colour from training case
patchColor = xTrSet(:,iTr)';
% Draw the patch
patch(xCorners,yCorners,patchColor);
% Display flag (0 or 1) indicating if the training case passes the
% "colour test"
hText = text(xPlotOffset+0.5,yPlotOffset+0.5,sprintf('%d',yTrSet(iTr)>yThresh));
% Set text alignment and size
set(hText,'HorizontalAlignment','center');
set(hText,'FontSize',8);
% Set text color to be visible (black on bright, white on dark)
if norm(patchColor) > 1
set(hText,'Color',[0 0 0]);
else
set(hText,'Color',[1 1 1]);
end
% Increment column index
xPlotOffset = xPlotOffset + 1;
end
hold off
title('Training Set')
axis('equal')
% Number of iterations
nIter = 200;
% Initialize arrays
normArray = zeros(nIter,nLayers-1);
yCorrectFrac = zeros(nIter,1);
yCorrect = zeros(nTr,1);
% Peform iterative back-propagation
for iIter = 1 : nIter
fprintf(1,'Iteration %d\n',iIter);
fprintf(1,'============\n\n');
oldArrayW = arrayW;
% Backpropagate based on the current weights and biases
[arrayW,arrayB] = BackPropagate(xTrSet,yTrSet,arrayW,arrayB);
% Check the weight and bias deltas
for iLayer = 1 : nLayers - 1
fprintf(1,'Layer %d: \n\n',iLayer);
diffArrayW = arrayW{iLayer} - oldArrayW{iLayer}
normArray(iIter,iLayer) = norm( diffArrayW , 2 );
end
% Check current weights and biases against training set for prediction accuracy
for iTr = 1 : nTr
yEst = ForwardPropagate(xTrSet(:,iTr),nLayers,arrayW,arrayB);
yCorrect(iTr) = ( yTrSet(iTr) > 0.75 ) == ( yEst > 0.5 );
end
% Determine fraction of "correct guesses"
yCorrectFrac(iIter) = sum(yCorrect) / nTr;
% Print accuracy to screen
fprintf(1,'\nAccuracy: %4.1f%%\n',100*yCorrectFrac(iIter));
end
% Plot the norm of each iterative update to the weights
figure;
plot(normArray ,'.-')
xlabel('Iteration')
ylabel('Delta');
title('Progressive Updates to Weight Matrix Norms')
grid on
% Plot the prediction accuracy iterative update to the weights
figure;
plot(100*yCorrectFrac,'.-')
xlabel('Iteration')
ylabel('Accuracy (%)');
title('Iterative Accuracy')
grid on
% Validation set
nValid = 100;
xValid = rand(nValid,3);
% Initialize arrays
yValid = zeros(nValid,1);
indValidTrue = zeros(nValid,1);
indValidFalse = zeros(nValid,1);
% Initialize counters
nIndValidTrue = 0;
nIndValidFalse = 0;
% Cycle through the validation cases and check the output
for iValid = 1 : nValid
[yValid(iValid),arrayZ,arrayA] = ForwardPropagate(xValid(iValid,:)',3,arrayW,arrayB);
if yValid(iValid) > 0.5
nIndValidTrue = nIndValidTrue + 1;
indValidTrue(nIndValidTrue) = iValid;
else
nIndValidFalse = nIndValidFalse + 1;
indValidFalse(nIndValidFalse) = iValid;
end
end
% Plot the "TRUE" cases
figure;
hold on
% Specify number of columns for display
nPlotCols = 10;
% Initialize row offset
yPlotOffset = -1;
for iInd = 1 : nIndValidTrue
if mod(iInd-1,nPlotCols) == 0
yPlotOffset = yPlotOffset + 1;
xPlotOffset = 0;
end
xCorners = xPlotOffset + [ 0 1 1 0 ]';
yCorners = yPlotOffset + [ 0 0 1 1 ]';
patchColor = xValid(indValidTrue(iInd),:);
patch(xCorners,yCorners,patchColor);
xPlotOffset = xPlotOffset + 1;
end
hold off
title('TRUE Cases')
axis('equal')
% Plot the "FALSE" cases
figure;
hold on
% Specify number of columns for display
nPlotCols = 10;
% Initialize row offset
yPlotOffset = -1;
for iInd = 1 : nIndValidFalse
if mod(iInd-1,nPlotCols) == 0
yPlotOffset = yPlotOffset + 1;
xPlotOffset = 0;
end
xCorners = xPlotOffset + [ 0 1 1 0 ]';
yCorners = yPlotOffset + [ 0 0 1 1 ]';
patchColor = xValid(indValidFalse(iInd),:);
patch(xCorners,yCorners,patchColor);
xPlotOffset = xPlotOffset + 1;
end
hold off
title('FALSE Cases')
axis('equal')

2 commentaires

Please format as text so we can get the "Copy" button to copy your code so we can easily paste it into MATLAB and try it.

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Wavelet Toolbox dans Centre d'aide et File Exchange

Produits

Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by