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

查看:206
本文介绍了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_{\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.

我在嵌套总和,偏置节点和该方程式的一般复杂性方面遇到问题.我也很努力,因为有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值,我们需要在整个网络中执行前馈/forward propagation.我假设您知道如何编写前馈代码,因为您只关心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));

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

This will compute the part of the summation concerning:

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

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.

我希望这会有所帮助!

I hope this helps!

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

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