function [f_a_3, E_W, W_1, W_2, result] = mainMLP(iteration_number, learning_rate, ytrain, W_1, W_2, Xtest, Xtraining, ytest)
Y_trainNEW = transpose(ytrain); <-------------- Error
X_trainingNEW = transpose(Xtraining);
X_testNEW = transpose(Xtest);
face = reshape(X_testNEW(:,999),48,48);
imagesc(face);
colormap(gray);
for iteration = 1 : iteration_number %
% %
disp(['Iteration: ' num2str(iteration) ' / ' num2str(iteration_number)]); %
% %
[E_W, grad_E_W_1, grad_E_W_2] = error(Y_trainNEW, W_1, W_2, X_trainingNEW); %
% %
disp(['Error: ' num2str(E_W)]); %
% After each traning iteration we update the weights based on learning
% rate and the gradient of the error function
W_1 = W_1 - learning_rate * grad_E_W_1; %
W_2 = W_2 - learning_rate * grad_E_W_2; %
% %
end
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Kuis 4 SC %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
[~, f_a_3] = forwardprop(W_1, W_2, X_testNEW); %
results = round(f_a_3.');
totalerror = numel(find(results~=ytest));
%representations = col2im(f_a_3(:,1) , [1 1000], [1000 1000], 'distinct');
%imagesc(representations);
%colormap(gray);
end
% You should now implement the sigmoid function. Your implementation should
% compute and return the output of the sigmoid function and its gradient.
function [f_a, grad_f_a] = sigmoid(a)
% First, compute the output of the sigmoid function (i.e. f_a). Write your
% code below:
% -------------------------------------------------------------------------
f_a = 1 ./ (1+exp(-a));
% -------------------------------------------------------------------------
% Once you have computed f_a, use it to compute the gradient of the sigmoid
% function (i.e. grad_f_a). Write your code below:
% -------------------------------------------------------------------------
grad_f_a = f_a .* (1-f_a);
%grad_f_a = ((exp(-a))/((1+exp(-a)).^2));
% -------------------------------------------------------------------------
end
% You should now implement the forward propagation function. Your
% implementation should compute and return the outputs of the second and
% third layer units as well as their gradients.
function [f_a_2, f_a_3, grad_f_a_2, grad_f_a_3] = forwardprop(W_1, W_2, X)
% First, compute the inputs of the second layer units (i.e. a_2). Write
% your code below:
% -------------------------------------------------------------------------
a_2 = W_1 * X; % where X is Xtrain and W_1 are weights of first layer
% -------------------------------------------------------------------------
% Once you have computed a_2, use it with the sigmoid function that you
% have implemented (i.e. sigmoid) to compute the outputs of the second
% layer units (i.e. f_a_2) and their gradients (i.e. grad_f_a_2). Write
% your code below:
% -------------------------------------------------------------------------
[f_a_2, grad_f_a_2] = sigmoid(a_2);
% -------------------------------------------------------------------------
% Then, compute the inputs of the third layer units (i.e. a_3). Write your
% code below:
% -------------------------------------------------------------------------
a_3 = W_2 * f_a_2;
% -------------------------------------------------------------------------
% Once you have computed a_3, use it with the sigmoid function that you
% have implemented (i.e. sigmoid) to compute the outputs of the third layer
% units (i.e. f_a_3) and their gradients (i.e. grad_f_a_3). Write your code
% below:
% -------------------------------------------------------------------------
[f_a_3, grad_f_a_3] = sigmoid(a_3);
% -------------------------------------------------------------------------
end
% You should now implement the back propagation function. Your
% implementation should compute and return the gradients of the error
% function.
function [grad_E_W_1, grad_E_W_2] = backprop(f_a_2, f_a_3, grad_f_a_2, grad_f_a_3, T, W_2, X)
% First, compute the errors of the second and third layer units (i.e.
% delta_2 and delta_3). Write you code below:
% -------------------------------------------------------------------------
delta_3 = grad_f_a_3 .* (f_a_3 - T);
delta_2 = grad_f_a_2 .* (W_2.' * delta_3);
% -------------------------------------------------------------------------
% Once you have computed delta_2 and delta_3, use them to compute the
% gradients of the error function (i.e. grad_E_W_1 and grad_E_W_2). Write
% your code below:
% -------------------------------------------------------------------------
%[~, grad_E_W_1] = sigmoid(delta_3);
%[~, grad_E_W_2] = sigmoid(delta_2);
grad_E_W_1 = delta_2 * X';
grad_E_W_2 = delta_3 * f_a_2';
% -------------------------------------------------------------------------
end
% You should now implement the error function. Your implementation should
% compute and return the output of the error function and its gradient.
function [E_W, grad_E_W_1, grad_E_W_2] = error(T, W_1, W_2, X)
% First, use the forward propagation function that you have implemented
% (i.e. forwardprop) to compute the outputs of the second and third layer
% units (i.e. f_a_2 and f_a_3) as well as their gradients (i.e. grad_f_a_2
% and grad_f_a_3). Write your code below:
% -------------------------------------------------------------------------
[f_a_2, f_a_3, grad_f_a_2, grad_f_a_3] = forwardprop(W_1, W_2, X);
% -------------------------------------------------------------------------
% Once you have computed f_a_3, use it to compute the output of the error
% function (i.e. E_W). Write your code below:
% -------------------------------------------------------------------------
E_W = 0.5 * sum(sum((f_a_3-T) .^2));
% -------------------------------------------------------------------------
% Once you have computed f_a_2, f_a_3, grad_f_a_2 and grad_f_a_3, use them
% with the back propagation function that you have implemented (i.e.
% backprop) to compute the gradients of the error function (i.e. grad_E_W_1
% and grad_E_W_2). Write your code below:
% -------------------------------------------------------------------------
[grad_E_W_1, grad_E_W_2] = backprop(f_a_2, f_a_3, grad_f_a_2, grad_f_a_3, T, W_2, X);
% -------------------------------------------------------------------------
end
can someone fix it???

 採用された回答

Walter Roberson
Walter Roberson 2020 年 9 月 30 日

0 投票

When you press the green Run button to execute your code, what value do you expect MATLAB to use for ytrain ? Where are you expecting it to get the value for ytrain from? Why should it use that particular value for ytrain, and not (for example) 42 or -pi ?

11 件のコメント

Daffa Muhammad Fadhil
Daffa Muhammad Fadhil 2020 年 9 月 30 日
can you write the code??
Walter Roberson
Walter Roberson 2020 年 9 月 30 日
In order for me to write the code, I need the answer to the questions I asked.
When you press the green Run button to run your code, where would you like MATLAB to look to find the value to use for ytrain?
Daffa Muhammad Fadhil
Daffa Muhammad Fadhil 2020 年 10 月 1 日
Image procesaing
Walter Roberson
Walter Roberson 2020 年 10 月 1 日
I am not clear on that, but one interpretation would be be that when you press the green Run button, that you expect that MATLAB will examine the current webcam image and use optical character recognition to interpret the Sanskrit writing that is in view, in order to determine the value of iteration number, ytrain, and so on. Do I have that right, or is the camera focused on Chinese?
Daffa Muhammad Fadhil
Daffa Muhammad Fadhil 2020 年 10 月 2 日
編集済み: Daffa Muhammad Fadhil 2020 年 10 月 2 日
Yeh youre right its only identify face. which is happy or sad face reaction but, cause file too large i cant upload here file size almost 70mb
Walter Roberson
Walter Roberson 2020 年 10 月 2 日
Okay, so you press the green Run button, and you expect MATLAB to examine the current webcam image, and based upon how long the person holds smiles and frowns, that information is used to input the iteration number, ytrain, and so on? Like hold a smile for 7 seconds to enter the digit 7, and frown to indicate that it is time to move on to entering the next of the several hundred ytrain values?? Could you remind me which facial expression should be used to indicate that it is time to move on to the next variable?
Daffa Muhammad Fadhil
Daffa Muhammad Fadhil 2020 年 10 月 2 日
編集済み: Walter Roberson 2020 年 10 月 2 日
Daffa Muhammad Fadhil
Daffa Muhammad Fadhil 2020 年 10 月 2 日
can you analize the code too?
Walter Roberson
Walter Roberson 2020 年 10 月 2 日
The files are not relevant:
The question is what your expectation is that MATLAB will do when you press the green Run button when you are viewing the source code for a function that expects parameters.
So, I repeat: is your expectation that when you press the green Run button for a function, that MATLAB will access the webcam and use the information about how long you hold smiles in order to input the decimal digits of the data that is needed by the function in order to operate?
If that is not the case, then as you have already indicated that you expect that MATLAB will identify happy or sad face reaction in order to figure out the information such as iteration number, then we need to define exactly what you do expect MATLAB to process happy or sad face reactions in order to figure out what numbers it should use in the function.
Daffa Muhammad Fadhil
Daffa Muhammad Fadhil 2020 年 10 月 2 日
編集済み: Daffa Muhammad Fadhil 2020 年 10 月 2 日
Yeah i want know how to identify both of reaction what iteration number can make smile and what number i must input to make it sad
Walter Roberson
Walter Roberson 2020 年 10 月 2 日
Sigh.
Every time that I try to get someone to talk through exactly where they expect MATLAB to go looking for the values of parameters when they press the green Run button, they miss the point. I keep hoping that someone will have a good and well-considered explanation of where they expected MATLAB to find the values of the variables (and why they expected MATLAB to look there), but no-one ever does. The only suggestions for where it could reasonably look, come from people who already understand why it doesn't look in those places. I was hoping that you actually had a particular source of variables in mind. Why do people keep making this mistake? Where do they think the data is supposed to come from? No-one can ever seem to answer. Sigh.
====
So then, having given up trying to talk you through this:
When you run a function that needs a parameter named iteration_number then MATLAB has strict rules about where it will try to look for the value.
The rules are really very simple for variables that have been named in the function statement as input parameters: namely that if you did not pass in a value for the parameter at the time you invoked the function, and you try to use the value of the variable inside the function, then MATLAB will cause an error message.
You might have defined a value for the variable in a function that you called mainMLP from, but MATLAB will never try to look in the calling environment for a variable that is named in the parameter list.
You might have defined a value for the variable in the base workspace and you called mainMLP from the command line or from a script that is not inside any function, but MATLAB will never try to look in the base workspace for a variable that is named in the parameter list.
You might have defined a value for the variable as a global variable using the "global" statement, but MATLAB will never look at global variables to try to resolve a variable that is named in a function parameter list.
You might be using a nested function, and the variable iteration_number might be defined in the outer context and so generally be available as a shared variable, but MATLAB will never look at shared variables to try to resolve a variable that is named in a function parameter list.
You either passed something in at the appropriate relative position in the parameter invocation -- or you did not. If you passed in enough arguments then the variable will exist; if you did not pass in enough arguments, then the variable will not exist and MATLAB will complain when you need its value.
When you press the green Run button, that is the same as to going down to the command line and typing in the name of the file, and pressing return. If the file is a script, that executes the script. If the file is a function, that executes the function, without passing in any parameters.
When you do this, MATLAB will not look at the parameter list for the function and attempt to locate variables with those names somewhere and provide those values into the function. As far as MATLAB is concerned, if you have
function twice = times2(x)
twice = 2*x;
end
and you press the green Run button, then that is the same as if you had deliberately invoked
times2()
indicating that you did not wish to pass in any parameters.
Thus, when you press that green Run button, MATLAB has no idea what value it should use for iteration_number or xtrain or ytrain or the other variables. MATLAB will just see that you did not pass in enough parameters to provide values for those variables, and will error out at the point where a value for one of those variables is needed.
What is the solution for your particular code?
Easy:
edit runMLP and press the green Run button when you are there.
runMLP.m is a script that loads data and initializes variables and passes the variables to the function mainMLP .
I would, however, recommend that you comment out the disp() before and the disp() after the line
[E_W, grad_E_W_1, grad_E_W_2] = error(Y_trainNEW, W_1, W_2, X_trainingNEW); %
You have 1000 iterations of it displaying two lines that are of low value.
In my copy of the code, I converted the disp() calls into appropriate calls to waitbar() so that you can see the progress of the iterations.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

製品

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by