简体   繁体   English

MATLAB中的神经网络成本函数

[英]Neural Network Cost Function in MATLAB

How would I implement this neural network cost function in matlab: 我如何在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_{\theta}(x^{(i)})_{k} is the value of the hypothesis at output k, with weights theta, and training input i.   [a scalar number]

%note: h_{\theta}(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? 我如何在matlab中创建这个等式?

Thank you very much! 非常感谢你!

A 2 layer neural network with 2 inputs, 2 outputs, 2 hidden nodes, and 2 bias units http://imagizer.imageshack.us/v2/320x240q90/40/92bn.jpg 具有2个输入,2个输出,2个隐藏节点和2个偏置单元的2层神经网络http://imagizer.imageshack.us/v2/320x240q90/40/92bn.jpg

Note: The code has strange colours as stackoverflow doesn't know I am programing in MATLAB. 注意:代码有奇怪的颜色,因为stackoverflow不知道我在MATLAB中编程。 I have also wrote the code straight into stackoverflow, so it may have syntax errors. 我还将代码直接写入stackoverflow,因此它可能有语法错误。 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. 这就是我没有打过半冒号等原因的原因。

I've implemented neural networks using the same error function as the one you've mentioned above. 我使用与你上面提到的相同的错误函数实现了神经网络。 Unfortunately, I haven't worked with Matlab for quite some time, but I'm fairly proficient in Octave,which hopefully you can still find useful, since many of the functions in Octave are similar to those of Matlab. 不幸的是,我还没有使用过Matlab很长一段时间,但我对Octave非常熟练,希望你仍然可以找到有用的东西,因为Octave中的许多函数都与Matlab类似。

@sashkello provided a good snippet of code for computing the cost function. @sashkello为计算成本函数提供了很好的代码片段。 However, this code is written with a loop structure, and I would like to offer a vectorized implementation. 但是,这段代码是用循环结构编写的,我想提供一个矢量化实现。

In order to evaluate the current theta values, we need to perform a feed forward/ forward propagation throughout the network. 为了评估当前的θ值,我们需要在整个网络中执行前馈/ forward propagation I'm assuming you know how to write the feed forward code, since you're only concerned with the J(theta) errors. 我假设你知道如何编写前馈代码,因为你只关心J(theta)错误。 Let the vector representing the results of your forward propagation be F 让代表前向传播结果的向量为F

Once you've performed feedforward, you'll need to carry out the equation. 一旦你执行了前馈,你就需要执行这个等式。 Note, I'm implementing this in a vectorized manner. 注意,我是以矢量化的方式实现它。

J = (-1/m) * sum(sum(Y .* log(F) + (1-Y) .* log(1-F),2));

This will compute the part of the summation concerning: 这将计算有关的总和部分:

总费用的第1部分

Now we must add the regularization term, which is: 现在我们必须添加正则化术语,即:

Typically, we would have arbitrary number of theta matrices, but in this case we have 2, so we can just perform several sums to get: 通常情况下,我们会有任意数量的θ矩阵,但在这种情况下我们有2个,所以我们可以只执行几个求和:

J =J + (lambda/(2*m)) * (sum(sum(theta_1(:,2:end).^2,2)) + sum(sum(theta_2(:,2:end).^2,2)));

Notice how in each sum I'm only working from the second column through the rest. 请注意,在每个总和中,我只是从第二列到其余部分工作。 This is because the first column will correspond to the theta values we trained for the `bias units. 这是因为第一列将对应于我们为“偏置单位”训练的theta值。

So there's a vectorized implementation of the computation of J. 所以有一个J的计算的矢量化实现。

I hope this helps! 我希望这有帮助!

I think Htheta is a K*2 array. 我认为Htheta是K * 2阵列。 Note that you need to add bias ( x0 and a0 ) in the forward cost function calculation. 请注意,您需要在远期成本函数计算中添加偏差( x0a0 )。 I showed you the array dimensions in each step under the assumption that you have two nodes at input , hidden, and output layers as comments in the code. 假设您在输入,隐藏和输出层有两个节点作为代码中的注释,我向您展示了每个步骤中的数组维度。

m = size(X, 1);  
X = [ones(m,1) X]; % m*3 in your case
% W1 2*3, W2 3*2
a2 = sigmf(W1 * X');  % 2*m
a2 = [ones(m,1) a2'];  % m*3    
Htheta = sigmf(a2 * W2);  % m*2    

J = (1/m) * sum ( sum (  (-Y) .* log(Htheta)  -  (1-Y) .* log(1-Htheta) ));

t1 = W1(:,2:size(W1,2));
W2 = W2';
t2 = W2(:,2:size(W2,2));

% regularization formula
Reg = lambda  * (sum( sum ( t1.^ 2 )) + sum( sum ( t2.^ 2 ))) / (2*m);

Well, as I understand your question has nothing to do with neural networks, but basically asking how to make a nested sum in matlab. 好吧,据我所知,你的问题与神经网络无关,但基本上问如何在matlab中进行嵌套求和。 I don't really want to type in the whole equation above, but, ie, the first part of the first sum will look like: 我真的不想输入上面的整个等式,但是,即第一个总和的第一部分将如下所示:

Jtheta = 0
for i=1:m,
    for j=1:K,
        Jtheta = Jtheta + Y(i, j) * log(Htheta(X(i))(j)) 
    end
end

where Jtheta is your result. Jtheta是你的结果。

This works on any number of hidden layers: 这适用于任意数量的隐藏层:

% Allow arbitrary network architectures. Create cell array of all Theta parameters
Theta={Theta1; Theta2};

% Compute unregularised cost (J)
J = 1/m * sum(sum((-y .* log(hX) - (1 - y) .* log(1 - hX))));

% Add regularisation
for i = 1:length(Theta)
  J += lambda / 2 / m * sum(sum(Theta{i}(:,2:end) .^ 2));
end   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM