在MATLAB中从头开始编程基本神经网络

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

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

到目前为止我所做的完全错了.我正在跟随Andrew Y. Ng教授的在线斯坦福机器学习课程,并试图尽我所能地实施他所教授的内容.

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

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

前馈部分的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
Run Code Online (Sandbox Code Playgroud)

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

      a1
x1 o--o--o y1      (all weights equal 1)
    \/ \/
    /\ /\
x2 o--o--o y2
      a2
Run Code Online (Sandbox Code Playgroud)

如果我们投入:

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

Y = feedforward2(X,W1,W2)
Run Code Online (Sandbox Code Playgroud)

我们得到了输出:

Y = [0.5,0.5]
Run Code Online (Sandbox Code Playgroud)

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

平方误差成本函数的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
Run Code Online (Sandbox Code Playgroud)

例如,如果培训数据是:

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

我们从初始随机权重开始

W1=[2,3;     W2=[3,2]
    4,1]
Run Code Online (Sandbox Code Playgroud)

如果我们投入:

Y= feedforward2([6,2],W1,W2)
Run Code Online (Sandbox Code Playgroud)

我们得到了

Y = 0.9933 
Run Code Online (Sandbox Code Playgroud)

这远远不是训练数据应该是什么(8/57 = 0.1404).所以初始随机权重W1和W2的猜测很差.

为了准确测量随机权重的猜测有多糟糕/好,我们使用成本函数:

C= cost(W1,W2,Xtrain,Ytrain)
Run Code Online (Sandbox Code Playgroud)

这给出了价值:

C = 6.6031e+003
Run Code Online (Sandbox Code Playgroud)

最大限度地降低成本函数

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

但是当我使用代码时:

 [W1,W2]=fminsearch(cost(W1,W2,Xtrain,Ytrain),[W1,W2])
Run Code Online (Sandbox Code Playgroud)

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


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

谢谢!!!

Pur*_*reW 2

您的神经网络似乎没问题,尽管如果您正在针对标记数据进行训练,那么您尝试进行的训练效率相当低。在这种情况下,我建议研究反向传播

关于您训练时的错误:您的错误消息暗示了问题:dimensions are not consistent

x0作为优化器的fminsearch初始猜测的参数,您发送[W1, W2]但从我所看到的来看,这些矩阵没有相同的行数,因此您不能像这样将它们添加在一起。我建议修改您的成本函数以将向量作为参数,然后为该向量的不同层形成权重向量。

您也没有正确提供成本函数,fminsearch因为您只是cost就地使用 w1、w2、Xtrain 和 Ytrain 进行评估。

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

编辑:您可以表达您的权重并修改您的代码,如下所示:

global Xtrain
global Ytrain
W = [W1; W2]
fminsearch(cost, W)
Run Code Online (Sandbox Code Playgroud)

必须修改成本函数,使其不采用 Xtrain、Ytrain 作为输入,因为 fminsearch 也会尝试优化它们。像这样修改你的成本函数:

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