1. Engineering
  2. Computer Science
  3. i been told i need to call outputsummary with the...

Question: i been told i need to call outputsummary with the...

Question details

I been told I need to call outputSummary with the proper arguments. I have been having issues trying to do this. Currently I have no errors running this but no output. Can you help me out?

clear all;

commandwindow;

learningRate = 0.5;

totalIterations = 500;

inputLayerNumber = 2;

hiddenLayerNumber = 2;

%Define the input and hidden layer:

inputLayer = zeros(inputLayerNumber, 1);

hiddenLayer = zeros(hiddenLayerNumber, 1);

inputLayerWithBias = zeros(inputLayerNumber + 1, 1);

hiddenLayerWithBias = zeros(hiddenLayerNumber + 1, 1);

%Define the output layer:

outputLayer = 0;

%Randomly assign the weights to the input and hidden layer:

inputLayerWeights = rand( (inputLayerNumber + 1) ,hiddenLayerNumber) - .5 ;

hiddenLayerWeights = rand( (hiddenLayerNumber + 1), 1) - .5;

%Define the input data:

inputLayer = [0 0; 0 1; 1 0; 1 1];

%Define the target output for the input layer:

ANDtargetOutput = [0; 0; 0; 1];

targetOutput = ANDtargetOutput;

%the variable 'm' as the number of samples:

m = size(targetOutput, 1);

inputLayerWithBias = [ones(m,1) inputLayer];

for iter=1:totalIterations

for i = 1:m

hiddenLayerActivation = inputLayerWithBias(i, :) * inputLayerWeights;

hiddenLayer = sigmoid(hiddenLayerActivation);

hiddenLayerWithBias = [1, hiddenLayer];

outputLayer = sigmoid(hiddenLayerWithBias * hiddenLayerWeights);

%Calculate the error:

deltaOutput = targetOutput(i) - outputLayer;

deltaHidden(1) = (deltaOutput * hiddenLayerWeights(1)) .* ((hiddenLayerWithBias(1) * (1.0 - hiddenLayerWithBias(1))));

deltaHidden(2) = (deltaOutput * hiddenLayerWeights(2)) .* ((hiddenLayerWithBias(2) * (1.0 - hiddenLayerWithBias(2))));

deltaHidden(3) = (deltaOutput * hiddenLayerWeights(3)) .* ((hiddenLayerWithBias(3) * (1.0 - hiddenLayerWithBias(3))));

% Fixed Step Gradient Descent - Update the weights

hiddenLayerWeights(1) = hiddenLayerWeights(1) + (learningRate * (deltaOutput * hiddenLayerWithBias(1)));

hiddenLayerWeights(2) = hiddenLayerWeights(2) + (learningRate * (deltaOutput * hiddenLayerWithBias(2)));

hiddenLayerWeights(3) = hiddenLayerWeights(3) + (learningRate * (deltaOutput * hiddenLayerWithBias(3)));

inputLayerWeights(1,1) = inputLayerWeights(1,1) + (learningRate * deltaHidden(2) * inputLayerWithBias(i, 1));

inputLayerWeights(1,2) = inputLayerWeights(1,2) + (learningRate * deltaHidden(3) * inputLayerWithBias(i, 1));

inputLayerWeights(2,1) = inputLayerWeights(2,1) + (learningRate * deltaHidden(2) * inputLayerWithBias(i, 2));

inputLayerWeights(2,2) = inputLayerWeights(2,2) + (learningRate * deltaHidden(3) * inputLayerWithBias(i, 2));

inputLayerWeights(3,1) = inputLayerWeights(3,1) + (learningRate * deltaHidden(2) * inputLayerWithBias(i, 3));

inputLayerWeights(3,2) = inputLayerWeights(3,2) + (learningRate * deltaHidden(3) * inputLayerWithBias(i, 3));

end

end

%Create the sigmoid function:

function a = sigmoid(z)

a = 1.0 ./ (1.0 + exp(-z));

end

%Create the cost function:

function [averageCost] = costFunction(inputLayerWithBias,inputLayerWeights, hiddenLayerWeights, targetOutput)

%Sum of square errors cost function

m = 4;

hiddenLayer = sigmoid(inputLayerWithBias * inputLayerWeights);

hiddenLayerWithBias = [ones(m,1) hiddenLayer];

outputLayer = sigmoid(hiddenLayerWithBias * hiddenLayerWeights);

for i=1:m

cost(i) = (1/2) * ((outputLayer(i) - targetOutput(i)) .^ 2);

end

%Sum up all of the individual costs

totalCost = sum(cost);

%average them out

averageCost = totalCost * (1/m);

%end

%Create a function that will summarize the output of the 4 samples:

function outputSummary(inputLayerWithBias, inputLayerWeights,hiddenLayerWeights, targetOutput, totalIterations)

cost = costFunction(inputLayerWithBias, inputLayerWeights,hiddenLayerWeights, targetOutput);

hiddenLayer = sigmoid(inputLayerWithBias * inputLayerWeights);

hiddenLayerWithBias = [ones(size(targetOutput,1),1) hiddenLayer];

actualOutput = sigmoid(hiddenLayerWithBias * hiddenLayerWeights);

fprintf(' ========================================= ');

fprintf('Output Summary (after %d iterations): ', totalIterations);

fprintf('Total Cost: [%f] ', cost);

for i=1:length(actualOutput)

if(actualOutput(i) > 0.5)

thresholdedValue = 1;

else

thresholdedValue = 0;

end

if(thresholdedValue == targetOutput(i))

   fprintf('Sample [%d]: Target = [%f} Thresholded Value = [%f] Actual= [%f] ',i, targetOutput(i), thresholdedValue, actualOutput(i));

   else % else print the error in red

   fprintf(2,'Sample[%d]: Target = [%f] Thresholded Value = [%f] Actual= [%f] ', i, targetOutput(i), thresholdedValue, actualOutput(i));

end

end

  

fprintf('========================================= ');

end

end

I believe the issue is around here...

申function outputsummary (inputLayerWithBias, cost= costFunction ( inputLayerWithBias, 100 inputLayerweights , hiddenLayerweights, inputLayerweights,hiddenLayerweights, totaliterations) targetoutput, targetOutput); 02- | hiddenLayer = signo id (inputLayerWithBias * inputLayerweights); 103 | %we have multiple samples, so we need to add the bias to each of them 104 105 106- actualOutput = sigmo id (hiddenLayerWithBias * hiddenLayerweights); 107- fprintf(\n\n= 108- fprintf(Output Summary (after %d iterations) : \n, totaliterations); hiddenLayerWithBias = [ones ( size ( targetOutput , 1 ) , 1) hiddenLayer1: 109- fprintf Total Cost: [sfl\n, cost); 110 111- 112- 申for i=1: length (actua!Output) if ( actual 0utput ( i ) 0、5 )

I need to figure out the answer to question 16 output using ‘fprintf’ commands in the above script.

The output should allow me to

Which of the above target outputs does it have the hardest time learning and why?

ANDtargetOutput = [0; 0; 0; 1];

   ORtargetOutput = [0; 1; 1; 1];
   NANDtargetOutput = [1; 1; 1; 0];
   NORtargetOutput = [1; 0; 0; 0];
   XORtargetOutput = [0; 1; 1; 0];

I just cant seem to get any output from my code.

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution