### Translated by

このページのコンテンツは英語から自動翻訳されています。自動翻訳をオフにする場合は「<a class="turn_off_mt" href="#">ここ</a>」をクリックしてください。

## Given feed back that, I need to call outputSummary with the proper arguments.

G. Nardi

### G. Nardi (view profile)

さんによって質問されました 2019 年 1 月 30 日

### G. Nardi (view profile)

さんによって 編集されました 2019 年 1 月 31 日
Luna

### Luna (view profile)

さんの 回答が採用されました
I am new to Matlab and still an undergrad student.
I can't seem to get an output from my code. I was given feedback that I need to call out outputSummary with the proper arguments.
I don't see what I did wrong...this is my function for my code.
%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);
%we have multiple samples, so we need to add the bias to each of them
hiddenLayerWithBias = [ones(size(targetOutput,1),1) hiddenLayer];
actualOutput = sigmoid(hiddenLayerWithBias * hiddenLayerWeights);
fprintf('\n\n=========================================\n');
fprintf('Output Summary (after %d iterations):\n', totalIterations);
fprintf('Total Cost: [%f]\n', 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]\n',i, targetOutput(i), thresholdedValue, actualOutput(i));
else % else print the error in red
fprintf(2,'Sample[%d]: Target = [%f] Thresholded Value = [%f] Actual= [%f]\n', i, targetOutput(i), thresholdedValue, actualOutput(i));
end
end
fprintf('=========================================\n\n\n');
end
end

Geoff Hayes

### Geoff Hayes (view profile)

2019 年 1 月 30 日
G - please clarify what you mean by I was given feedback that I need to call out outputSummary with the proper arguments. Was the feedback from a teacher (or assistant), or did you observe a MATLAB error? If the the latter, please copy and paste the full error message to this question. Also, please show us how you are calling the function outputSummary function Presumably you are passing in five parameters when calling this function?
G. Nardi

### G. Nardi (view profile)

2019 年 1 月 30 日

サインイン to comment.

R2018b

## 1 件の回答

2019 年 1 月 30 日

### Luna (view profile)

2019 年 1 月 30 日
採用された回答

This code is so badly written. There are too many nested functions.
outputSummary function is defined inside the costFunction. theCostFunction is called inside the outputSummary.
You can not call outputSummary from outside because it is already nested inside the costFunction.
What is the purpose of this code what it is expected to do?
I just changed couple of things and moved your outputSummary to the outside.
I called outputSummary inside your for loop because it seems that this function uses the inputs which are defined inside the for loop iterations.
Check this:
clear all;
commandwindow;
%Using Matlab, create a multi-layer perceptron with 3 layers: input layer,
%hidden layer, output layer (using a sigmoid function).
%Define the learning rate and total iterations
learningRate = 0.5;
totalIterations = 500;
%Define the size of the input layer and the hidden layer:
inputLayerNumber = 2;
hiddenLayerNumber = 2;
%Define the input and hidden layer:
inputLayer = zeros(inputLayerNumber, 1);
hiddenLayer = zeros(hiddenLayerNumber, 1);
%Add the bias to the input and hidden layer:
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;
%Define the variable 'm' as the number of samples:
m = size(targetOutput, 1);
inputLayerWithBias = [ones(m,1) inputLayer];
%Create a for loop, that will step through each of the samples one at a time
for iter=1:totalIterations
for i = 1:m
hiddenLayerActivation = inputLayerWithBias(i, :) * inputLayerWeights;
hiddenLayer = sigmoid(hiddenLayerActivation);
%Add the bias to the hiddenLayer
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)));
%update each weight according to the part that they played
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));
outputSummary(inputLayerWithBias, inputLayerWeights,hiddenLayerWeights, targetOutput, totalIterations)
end
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);
%we have multiple samples, so we need to add the bias to each of them
hiddenLayerWithBias = [ones(size(targetOutput,1),1) hiddenLayer];
actualOutput = sigmoid(hiddenLayerWithBias * hiddenLayerWeights);
fprintf('\n\n=========================================\n');
fprintf('Output Summary (after %d iterations):\n', totalIterations);
fprintf('Total Cost: [%f]\n', 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]\n',i, targetOutput(i), thresholdedValue, actualOutput(i));
else % else print the error in red
fprintf(2,'Sample[%d]: Target = [%f] Thresholded Value = [%f] Actual= [%f]\n', i, targetOutput(i), thresholdedValue, actualOutput(i));
end
end
fprintf('=========================================\n\n\n');
end
%Create the sigmoid function:
function a = sigmoid(z)
a = 1.0 ./ (1.0 + exp(-z));
end
%Create the cost function:
% This function will only work for NN with just one output (k = 1)
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);
% Step through all of the samples and calculate the cost at each one
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
end

#### 0 件のコメント

サインイン to comment.

Translated by