MATLAB 中的神经网络成本函数 [英] Neural Network Cost Function in MATLAB

查看:30
本文介绍了MATLAB 中的神经网络成本函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我在嵌套总和、偏置节点和这个方程的一般复杂性方面遇到了问题.我也很挣扎,因为有 2 个权重矩阵,一个将输入连接到隐藏层,另一个将隐藏层连接到输出.到目前为止,这是我的尝试.

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.

定义变量

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.

我将如何在 matlab 中创建这个方程?

How would I create this equation in matlab?

非常感谢!

注意:代码有奇怪的颜色,因为 stackoverflow 不知道我在 MATLAB 中编程.我也直接把代码写进了stackoverflow,所以它可能有语法错误.我更感兴趣的是我应该如何做这件事的一般想法,而不仅仅是复制和粘贴代码.这就是我没有打扰分号之类的原因.

推荐答案

我已经使用与您上面提到的相同的误差函数实现了神经网络.不幸的是,我已经有一段时间没有使用 Matlab,但我对 Octave 相当精通,希望你仍然会发现它有用,因为 Octave 中的许多功能与 Matlab 的相似.

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.

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

@sashkello provided a good snippet of code for computing the cost function. However, this code is written with a loop structure, and I would like to offer a vectorized implementation.

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

In order to evaluate the current theta values, we need to perform a feed forward/ forward propagation throughout the network. I'm assuming you know how to write the feed forward code, since you're only concerned with the J(theta) errors. Let the vector representing the results of your forward propagation be 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));

这将计算求和的部分:

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

Now we must add the regularization term, which is:

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

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:

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

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

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.

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

So there's a vectorized implementation of the computation of J.

我希望这会有所帮助!

这篇关于MATLAB 中的神经网络成本函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆