在MATLAB中从头开始编写基本神经网络 [英] Programming a Basic Neural Network from scratch in MATLAB

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

问题描述

过去,我曾在该网站上问过一些关于神经网络的问题,并获得了不错的答案,但我仍在努力为自己实现一个.这是一个很长的问题,但是我希望它可以作为其他人在MATLAB中创建自己的基本神经网络的指南,因此应该值得.

I have asked a few questions about neural networks on this website in the past and have gotten great answers, but I am still struggling to implement one for myself. This is quite a long question, but I am hoping that it will serve as a guide for other people creating their own basic neural networks in MATLAB, so it should be worth it.

到目前为止,我所做的一切可能是完全错误的.我正在学习Andrew Y. Ng教授的斯坦福大学在线机器学习课程,并尽我所能来实现他所教的内容.

What I have done so far could be completely wrong. I am following the online stanford machine learning course by Professor Andrew Y. Ng and have tried to implement what he has taught to the best of my ability.

您能告诉我我代码的前馈和成本函数部分是否正确,以及在最小化(优化)部分哪里出错了?

Can you please tell me if the feed forward and cost function parts of my code are correct, and where I am going wrong in the minimization (optimization) part?

我有一个前馈2层前馈神经网络.

I have a feed 2 layer feed forward neural network.

前馈部分的MATLAB代码为:

function [ Y ] = feedforward2( X,W1,W2)
%This takes a row vector of inputs into the neural net with weight matrices W1 and W2 and returns a row vector of the outputs from the neural net

%Remember X, Y, and A can be vectors, and W1 and W2 Matrices 

X=transpose(X);            %X needs to be a column vector
A = sigmf(W1*X,[1 0]);     %Values of the first hidden layer  
Y = sigmf(W2*A,[1 0]);     %Output Values of the network
Y = transpose(Y);          %Y needs to be a column vector

例如,一个具有两个输入和两个输出的两层神经网络看起来会像这样:

So for example a two layer neural net with two inputs and two outputs would look a bit like this:

      a1
x1 o--o--o y1      (all weights equal 1)
    \/ \/
    /\ /\
x2 o--o--o y2
      a2

如果我们输入:

X=[2,3];
W1=ones(2,2);
W2=ones(2,2);

Y = feedforward2(X,W1,W2)

我们得到输出:

Y = [0.5,0.5]

这代表神经网络图中显示的y1和y2值

This represents the y1 and y2 values shown in the drawing of the neural net

平方误差成本函数的MATLAB代码为:

function [ C ] = cost( W1,W2,Xtrain,Ytrain )
%This gives a value seeing how close W1 and W2 are to giving a network that represents the Xtrain and Ytrain data
%It uses the squared error cost function
%The closer the cost is to zero, the better these particular weights are at giving a network that represents the training data
%If the cost is zero, the weights give a network that when the Xtrain data is put in, The Ytrain data comes out

M = size(Xtrain,1);  %Number of training examples

oldsum = 0;

for i = 1:M,
        H = feedforward2(Xtrain,W1,W2); 
        temp = ( H(i) - Ytrain(i) )^2;
        Sum = temp + oldsum;
        oldsum = Sum;
end

C = (1/2*M) * Sum;

end

示例

例如,如果训练数据为:

So for example if the training data is:

Xtrain =[0,0;        Ytrain=[0/57;
        1,2;           3/57;
        4,1;           5/57;
        5,2;           7/57;                                                           a1    
        3,4;           7/57;    %This will be for a two input one output network  x1 o--o y1
        5,3;           8/57;                                                          \/ \_o 
        1,5;           6/57;                                                          /\ /
        6,2;           8/57;                                                      x2 o--o      
        2,1;           3/57;                                                           a2    
        5,5;]          10/57;]

我们从初始随机权重开始

We start with initial random weights

W1=[2,3;     W2=[3,2]
    4,1]

如果我们输入:

Y= feedforward2([6,2],W1,W2)

我们得到

Y = 0.9933 

与训练数据说的应该达到的值相差甚远(8/57 = 0.1404).因此初始随机权重W1和W2不好猜.

Which is far from what the training data says it should be (8/57 = 0.1404). So the initial random weights W1 and W2 where a bad guess.

要精确地估计随机权重权重是多少,我们使用成本函数:

To measure exactly how bad/good a guess the random weights weights are we use the cost function:

C= cost(W1,W2,Xtrain,Ytrain)

这给出值:

C = 6.6031e+003

最小化成本函数

如果我们通过搜索所有可能的变量W1和W2,然后选择最低的变量来最小化成本函数,则将得到最近似训练数据的网络

If we minimize the cost function by searching all of the possible variables W1 and W2 and then picking the lowest, this will give the network that best approximates the training data

但是当我使用代码时:

 [W1,W2]=fminsearch(cost(W1,W2,Xtrain,Ytrain),[W1,W2])

它给出一条错误消息.它说:使用horzcat时出错.CAT参数尺寸不一致."为什么会收到此错误,我该怎么做才能解决?

It gives an error message. It says: "Error using horzcat. CAT arguments dimensions are not consistent."Why am I getting this error and what can I do to fix it?

您能告诉我我代码的前馈和成本函数部分是否正确,以及在最小化(优化)部分哪里出错了?

Can you please tell me if the feed forward and cost function parts of my code are correct, and where I am going wrong in the minimization (optimization) part?

谢谢!

推荐答案

您的神经网络似乎还不错,尽管如果您要针对带有标签的数据进行训练,那么您尝试进行的训练效率很低在做.在这种情况下,我建议您调查反向传播

Your Neural network seems alright, although the kind of training you're trying to do is quite in-efficient if you're training against labeled data as you're doing. In that case I would suggest looking into Back-propagation

关于培训时的错误:您的错误消息提示问题:dimensions are not consistent

About your error when training: Your error message hints at the problem: dimensions are not consistent

作为fminsearch中的参数x0(这是优化程序的初始猜测),您发送[W1, W2],但是从我的观察中可以看出,这些矩阵没有相同的行数,因此可以'像这样将它们加在一起.我建议修改成本函数,以一个向量作为参数,然后从该向量形成不同层的权重向量.

As argument x0 in fminsearch which is the initial guess for the optimizer, you send [W1, W2] but from what I can see, these matrices don't have the same number of rows, and therefore you can't add them together like that. I would suggest modifying your cost-function to take a vector as argument and then form your weight-vectors for different layers from that one vector.

您还没有正确地向fminsearch提供成本函数,因为您仅使用w1,w2,Xtrain和Ytrain来评估cost.

You are also not supplying the cost-function correctly to fminsearch as you are just evaluating cost with w1, w2, Xtrain and Ytrain in-place.

根据文档(自从我使用Matlab已有好几年了),似乎您将指针传递给了成本函数 fminsearch(cost, [W1; W2])

According to the documentation (it's been years since I used Matlab) it seems like you pass the pointer to the cost-function as fminsearch(cost, [W1; W2])

您可以按以下方式表示权重并修改代码:

You could express your weights and modify your code as follows:

global Xtrain
global Ytrain
W = [W1; W2]
fminsearch(cost, W)

必须修改成本函数,以使其不接受Xtrain,Ytrain作为输入,因为fminsearch也会尝试优化这些函数.修改您的成本函数,如下所示:

Cost-function must be modified such that it doesn't take Xtrain, Ytrain as input because fminsearch will then try to optimize those too. Modify your cost-function like this:

function [ C ] = cost( W )
   W1 = W[1:2,:]
   W2 = W[3,:]
   global Xtrain
   global Ytrain
   ...

这篇关于在MATLAB中从头开始编写基本神经网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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