MATLAB中的神经网络成本函数

时间:2014-01-29 20:04:25

标签: matlab machine-learning neural-network

我如何在matlab中实现这个神经网络成本函数:

Neural Network Cost Function

以下是符号所代表的内容:

% 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.

我遇到了嵌套和,偏置节点以及这个等式的一般复杂性的问题。我也在苦苦挣扎,因为有两个权重矩阵,一个将输入连接到隐藏层,另一个连接隐藏层和输出。这是我到目前为止的尝试。

定义变量

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]

使用这些权重的假设等于......

Htheta = sigmf( dot(W2 , sigmf(dot(W1 , X))) )   %This will be a column vector with K rows.

使用这些权重的成本函数等于...(这是我正在努力的地方)

  sum1 = 0
  for i = 1:K
  sum1 = sum1 + Y(k,i) *log(Htheta(k)) + (1 - Y(k,i))*log(1-Htheta(k))

我只是继续写这样的事情,然后意识到这一切都是错的。我不能为我的生活弄清楚如何做嵌套的总和,或包括输入矩阵,或做任何一个。这一切都非常复杂。

我如何在matlab中创建这个等式?

非常感谢!

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

注意:代码有奇怪的颜色,因为stackoverflow不知道我在MATLAB中编程。我还将代码直接写入stackoverflow,因此它可能有语法错误。我对如何进行此操作的一般概念更感兴趣,而不仅仅是复制和粘贴代码。这就是我没有打过半冒号等原因的原因。

4 个答案:

答案 0 :(得分:17)

我使用与您上面提到的相同的错误功能实现了神经网络。不幸的是,我还没有使用Matlab很长一段时间,但我对Octave非常熟练,希望你仍然可以找到有用的东西,因为Octave中的许多函数都与Matlab类似。

@sashkello为计算成本函数提供了很好的代码片段。但是,这段代码是用循环结构编写的,我想提供一个矢量化实现。

为了评估当前的θ值,我们需要在整个网络中执行前馈/ forward propagation。我假设你知道如何编写前馈代码,因为你只关心J(theta)错误。让代表前向传播结果的向量为F

一旦你执行了前馈,你就需要执行这个等式。请注意,我是以矢量化方式实现的。

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

这将计算有关总和的部分:

part 1 of the total cost

现在我们必须添加正则化术语,即:

通常,我们会有任意数量的θ矩阵,但在这种情况下我们有2个,所以我们可以只执行几个求和:

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

请注意,在每个总和中,我只是从第二列到其余部分工作。这是因为第一列将对应于我们为“偏置单位”训练的theta值。

因此,有一个矢量化的J.计算实现。

我希望这有帮助!

答案 1 :(得分:6)

我认为Htheta是K * 2数组。请注意,您需要在远期成本函数计算中添加偏差(x0a0)。我假设您在输入,隐藏和输出层有两个节点作为代码中的注释,我向您展示了每个步骤中的数组维度。

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);

答案 2 :(得分:1)

嗯,据我所知,你的问题与神经网络无关,但基本上是在如何在matlab中进行嵌套求和。我并不想输入上面的整个等式,但是,第一笔的第一部分看起来像:

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

其中Jtheta是您的结果。

答案 3 :(得分:0)

这适用于任意数量的隐藏层:

% 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