% This program illustrates how MultiClass works and trains the neural %network max_epoch times. Once the training process has been finished, the program %enters the training data into the neural network and displays the output. We %can verify the training results via the comparison of the output with the correct %output. See Chapter 4 for more information. clear all close all clc rng(3); % The input set consists of five 5x5 pixel squares. %0: white pixel; 1: black pixel. X = zeros(5, 5, 7); %(pixel_x, pixels_y, samples) X(:, :, 1) = [0 1 1 0 0; 0 0 1 0 0; 0 0 1 0 0; 0 0 1 0 0; 0 1 1 1 0]; %1 X(:, :, 2) = [1 1 1 1 0; 0 0 0 0 1; 0 1 1 1 0; 1 0 0 0 0; 1 1 1 1 1]; %2 X(:, :, 3) = [1 1 1 1 0; 0 0 0 0 1; 0 1 1 1 0; 0 0 0 0 1; 1 1 1 1 0]; %3 X(:, :, 4) = [0 0 0 1 0; 0 0 1 1 0; 0 1 0 1 0; 1 1 1 1 1; 0 0 0 1 0]; %4 X(:, :, 5) = [1 1 1 1 1; 1 0 0 0 0; 1 1 1 1 0; 0 0 0 0 1; 1 1 1 1 0]; %5 X(:, :, 6) = [1 1 1 1 1; 1 0 0 0 0; 1 1 1 1 1; 1 0 0 0 1; 1 1 1 1 1]; %6 X(:, :, 7) = [1 1 1 1 1; 0 0 0 0 1; 0 0 0 1 0; 0 0 0 1 0; 0 0 0 1 0]; %7 % Desired outputs mapped via one-hot encoding (or 1-of-N encoding): D = [1 0 0 0 0 0 0; %1 0 1 0 0 0 0 0; %2 0 0 1 0 0 0 0; %3 0 0 0 1 0 0 0; %4 0 0 0 0 1 0 0; %5 0 0 0 0 0 1 0; %6 0 0 0 0 0 0 1]; %7 % D = eye(7); % Weights initialization: W1 = 2*rand(50, 25) - 1; %(hidden neurons) x (inputs) W2 = 2*rand( 7, 50) - 1; %(outputs) x (hidden neurons) % Training process: max_epoch = 10000; for epoch = 1:max_epoch [W1, W2] = MultiClass(W1, W2, X, D); end % Inference: N = size(X,3); y = zeros(N,size(D,2)); for k = 1:N x = reshape(X(:, :, k), 25, 1); v1 = W1*x; y1 = Sigmoid(v1); v = W2*y1; y(k,:) = Softmax(v); end disp('Results:'); disp(' [desired]:'); disp(D); disp(' [network_output]:'); disp(y) %% Showing images (comment this section before running RealMultiClass.m): % for i = 1:N % compareImages(X(:,:,i), y(i,:)); % end