How would I implement this neural network cost function in matlab:
Here are what the symbols represent:
% m is the number of training examples. [a scalar number]
% K is the number of output nodes. [a scalar number]
% Y is the matrix of training outputs. [an m by k matrix]
% y^{(i)}_{k} is the ith training output (target) for the kth output node. [a scalar number]
% x^{(i)} is the ith training input. [a column vector for all the input nodes]
% h_{heta}(x^{(i)})_{k} is the value of the hypothesis at output k, with weights theta, and training input i. [a scalar number]
%note: h_{heta}(x^{(i)}) will be a column vector with K rows.
I'm having problems with the nested sums, the bias nodes, and the general complexity of this equation. I'm also struggling because there are 2 matrices of weights, one connecting the inputs to the hidden layer, and one connecting the hidden layer to the outputs. Here's my attempt so far.
Define variables
m = 100 %number of training examples
K = 2 %number of output nodes
E = 2 %number of input nodes
A = 2 %number of nodes in each hidden layer
L = 1 %number of hidden layers
Y = [2.2, 3.5 %targets for y1 and y2 (see picture at bottom of page)
1.7, 2.1
1.9, 3.6
. . %this is filled out in the actual code but to save space I have used ellipsis. there will be m rows.
. .
. .
2.8, 1.6]
X = [1.1, 1.8 %training inputs. there will be m rows
8.5, 1.0
9.5, 1.8
. .
. .
. .
1.4, 0.8]
W1 = [1.3, . . 0.4 %this is just an E by A matrix of random numbers. this is the matrix of initial weights.
. . . - 2
. . . 3.1
. . . - 1
2.1, -8, 1.2, 2.1]
W2 = [1.3, . . 0.4 %this is an A by K matrix of random numbers. this is the matrix of initial weights.
. . . - 2
. . . 3.1
. . . - 1
2.1, -8, 1.2, 2.1]
Hypothesis using these weights equals...
Htheta = sigmf( dot(W2 , sigmf(dot(W1 , X))) ) %This will be a column vector with K rows.
Cost Function using these weights equals... (This is where I am struggling)
sum1 = 0
for i = 1:K
sum1 = sum1 + Y(k,i) *log(Htheta(k)) + (1 - Y(k,i))*log(1-Htheta(k))
I just keep writing things like this and then realising it's all wrong. I can not for the life of me work out how to do the nested sums, or include the input matrix, or do any of it. It's all very complicated.
How would I create this equation in matlab?
Thank you very much!
Note: The code has strange colours as stackoverflow doesn't know I am programing in MATLAB. I have also wrote the code straight into stackoverflow, so it may have syntax errors. I am more interested in the general idea of how I should go about doing this rather than just having a code to copy and paste. This is the reason I haven't bothered with semi colons and such.
See Question&Answers more detail:
os