MATLAB中的神经网络成本函数

Blu*_*ue7 10 matlab machine-learning neural-network

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

神经网络成本函数

以下是符号代表的含义:

% 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.
Run Code Online (Sandbox Code Playgroud)

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

定义变量

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]
Run Code Online (Sandbox Code Playgroud)

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

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

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

  sum1 = 0
  for i = 1:K
  sum1 = sum1 + Y(k,i) *log(Htheta(k)) + (1 - Y(k,i))*log(1-Htheta(k))
Run Code Online (Sandbox Code Playgroud)

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

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

非常感谢你!

具有2个输入,2个输出,2个隐藏节点和2个偏置单元的2层神经网络http://imagizer.imageshack.us/v2/320x240q90/40/92bn.jpg

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

Ale*_*dro 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));
Run Code Online (Sandbox Code Playgroud)

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

总费用的第1部分

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

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

J =J + (lambda/(2*m)) * (sum(sum(theta_1(:,2:end).^2,2)) + sum(sum(theta_2(:,2:end).^2,2)));
Run Code Online (Sandbox Code Playgroud)

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

所以有一个J的计算的矢量化实现.

我希望这有帮助!

  • 这非常有用,非常感谢!是的我知道如何计算前向传播.只是一个简单的问题:在matlab中,sum命令将向量列中的所有数字相加,这与此八度代码中的数字相同,还是需要不同的命令?另外,在第一个等式的内部和中,你放了:"sum(thing_to_sum,2)".在matlab中,这将对向量的行求和.这也和八度相同吗?谢谢! (2认同)

len*_*310 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);
Run Code Online (Sandbox Code Playgroud)