ITKeyword,专注技术干货聚合推荐

注册 | 登录

解决matlab syntax errors in single layer neural network

itPublisher 分享于

2020腾讯云双十一活动,全年最低!!!(领取3500元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1073

【阿里云】双十一活动,全年抄底价,限时3天!(老用户也有),
入口地址https://www.aliyun.com/1111/home

推荐:Matlab函数拟合之Neural Network方法记录

程序如下: clear all; close all;x = [0 1 2 3 4 5 6 7 8];x1 = [0:0.1:8];t = [0 0.84 0.91 0.14 -0.77 -0.96 -0.28 0.66 0.99];% plot(x,t,'o')net =

-5

I have to implement a single layer neural network or perceptron.For this, I have 2 files data sets , one for the input and one for the output.I have to do this in matlab without using neural toolbox.The format of 2 files is given below.

 In:
    0.832 64.643
    0.818 78.843
    1.776 45.049
    0.597 88.302
    1.412 63.458


Out:
0 0 1
0 0 1
0 1 0
0 0 1
0 0 1

The target output is "1 for a particular class that the corresponding input belongs to and "0 for the remaining 2 outputs.

I tried to do this, But it is not working for me.

load in.data
load out.data
x = in(:1);
y = in(:2);

learning rate = 0.2;
max_iteration = 50;

function result = calculateOutput(weights,x, y)
s = x*(weights(1) +weight(2) +weight(3));
if s>=0
 result = 1
else:
 result = -1
end
end

Count = length(x);
weights[0] = rand();
weights[1] = rand();
weights[2] = rand();

iter = 0;
do {
  iter++;
  globalerror = 0;
  for(p=0; p<count;p++){
    output = calculateoutput(weights,x[p],y[p]);
    localerror = output[p] - output
    weights[0]+= learningrate *localerror*x[p];
    weights[1]+= learningrate *localerror*y[p];
    weights[2]+= learningrate *localerror;
    globalerror +=(localerror*localerror);
   }
}while(globalerror != 0 && iter <= max_iteration);

Where is the mistake in this algorithm??

I am referring the example given in the link below:-

Perceptron learning algorithm not converging to 0

matlab neural-network
edited Aug 11 '10 at 12:56 Amro 105k 19 171 314 asked Aug 10 '10 at 1:51 user414981 49 1 2 9

closed as off-topic by Daniel Daranas, Ander Biguri, Schorsch, Eric Renouf, Brian Knight Oct 22 '15 at 17:55

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Daniel Daranas, Ander Biguri, Schorsch, Eric Renouf, Brian Knight
If this question can be reworded to fit the rules in the help center, please edit the question.

     It's not an algorithm bug (yet). It is a syntax error Count ≢ count –  msw Aug 10 '10 at 2:09 7   It is clear from your code that you are not ready to attempt a complete solution all at once. I would recommend you start from a pseudocode algorithm and incrementally and independently implement each step. If you don't know how to write a loop or increment a variable, attempting to write a complete program is not a productive learning experience. –  Matt Mizumi Aug 10 '10 at 2:36

 | 

3 Answers
3

解决方法

Here's a list of what I see wrong:

推荐:【深度学习论文笔记】Recover Canonical-View Faces in the Wild with Deep Neural Network

文章来源:CVPR2014 作者:Zhenyao Zhu,Ping Luo,Xiaogang Wang,Xiaoou Tang (香港中文大学果然牛) 主要内容: 提出了利用深度学习(还是CNN)来进行人脸图

deep breath

  • The indexing syntax (:1) is incorrect. Perhaps you mean (:,1) as in "all rows of column 1".
  • There is no such thing as a do...while loop in MATLAB. Only FOR and WHILE loops. Also, your for loop is defined wrong. MATLAB has a different syntax for that.
  • There are no ++ or += operators in MATLAB.
  • The "not equal" operator in MATLAB is ~=, not !=.
  • Indexing of vectors and matrices needs to be done with parentheses (), not square brackets [].
  • Is all of this inside a function or a script? You can not define functions, namely calculateOutput, in a script. You would have to put that in its own m-file calculateOutput.m. If all of the code is actually inside a larger function, then calculateOutput is a nested function and should work fine (assuming you have ended the larger enclosing function with an end).
  • You have a number of apparent typos for variable names:
    • weight vs. weights (as per phoffer's answer)
    • Count vs. count (MATLAB is case-sensitive)
    • calculateOutput vs. calculateoutput (again, case-sensitivity)
    • learning rate vs. learningrate (variables can't have spaces in them)

heavy exhale ;)

In short, it needs quite a bit of work.


edited Aug 10 '10 at 3:00 answered Aug 10 '10 at 2:25 gnovice 92.2k 11 205 292 1   If calculateOutput is a nested function, the main function needs to be closed with an end –  Jonas Aug 10 '10 at 2:46      @Jonas: Thanks, I made that more explicit. –  gnovice Aug 10 '10 at 3:01

 | 

The main mistake is that this is not written using Matlab syntax. Here is an attempt to do what I think you were trying to do.

Unfortunately, there is a fundamental problem with your algorithm (see comments in the code). Also, I think you should have a look at the very good Matlab documentation. Reading the manual will tell you quickly how you format this.

function neuralNetwork

%# load data
load in.data
load out.data
x = in(:,1);
y = in(:,2);

%# set constants
learningrate = 0.2;
max_iteration = 50;

% initialize parameters
count = length(x);
weights = rand(1,3); % creates a 1-by-3 array with random weights

iter = 0;
while globalerror ~= 0 && iter <= max_iteration
  iter = iter + 1;
  globalerror = 0;
  for p = 1:count
    output = calculateOutput(weights,x(p),y(p));

    %# the following line(s) cannot possibly work
    %# output is not a vector, since the previous line
    %# assigns it to a scalar
    %# Also, arrays are accessed with parentheses
    %# and indexing starts at 1
    %# and there is no += operator in Matlab
    localerror = output[p] - output
    weights[0]+= learningrate *localerror*x[p];
    weights[1]+= learningrate *localerror*y[p];
    weights[2]+= learningrate *localerror;
    globalerror +=(localerror*localerror);
end %# for-loop
end %# while-loop


%# subfunctions in Matlab are put at the end of the file
function result = calculateOutput(weights,x, y)
s = x*(weights(1) +weight(2) +weight(3));
if s>=0
 result = 1
else:
 result = -1
end
end

answered Aug 10 '10 at 2:25 Jonas 67.7k 7 109 146      Great idea doing a rewrite of it, but MATLAB does not use += either. You have to do weights[0] = weights[0] + learningrate * localerror * x[p] –  Paul Hoffer Aug 10 '10 at 2:30      SORRY, I completely read over the last part of your comment block. I moved the comment to my answer and credited you. –  Paul Hoffer Aug 10 '10 at 2:34

 | 

On line #10, you have weights(1) +weight(2) +weight(3); but the rest of the code has weights with an s.

EDIT: Also, MATLAB does not have the ++ operator; your for loop will cause an error. In MATLAB, construct a for loop like this:

for p=0:count
    blah blah blah
end

Also, MATLAB does not use the += operator either, as Jonas pointed out in his code. You need to do this:

weights(0) = weights(0) + learningrate * localerror * x(p)


edited Aug 10 '10 at 2:32 answered Aug 10 '10 at 2:10 Paul Hoffer 5,663 5 19 34

 | 

Not the answer you're looking for? Browse other questions tagged matlab neural-network or ask your own question.

推荐:A Neural Network in 11 lines of Python (Part 1) DNN编程入门 特详细 很有深度

A Neural Network in 11 lines of Python (Part 1) A bare bones neural network implementation to describe the inner workings of backpropagation.Posted by


相关阅读排行


相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。