解决matlab syntax errors in single layer neural network
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 neuralnetworkedited 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 offtopic by Daniel Daranas, Ander Biguri, Schorsch, Eric Renouf, Brian Knight Oct 22 '15 at 17:55
This question appears to be offtopic. 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 ontopic 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
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 CanonicalView 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 mfilecalculateOutput.m
. If all of the code is actually inside a larger function, thencalculateOutput
is a nested function and should work fine (assuming you have ended the larger enclosing function with anend
).  You have a number of apparent typos for variable names:
weight
vs.weights
(as per phoffer's answer)Count
vs.count
(MATLAB is casesensitive)calculateOutput
vs.calculateoutput
(again, casesensitivity)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 1by3 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 %# forloop
end %# whileloop
%# 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 neuralnetwork 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
相关阅读排行
相关内容推荐
 1微信公众号文章采集,并发布到WordPress
 2Matlab函数拟合之Neural Network方法记录
 3A Neural Network in 11 lines of Python (Part 1) DNN编程入门 特详细 很有深度
 4【深度学习论文笔记】Recover CanonicalView Faces in the Wild with Deep Neural Network
 5人群计数：SingleImage Crowd Counting via MultiColumn Convolutional Neural Network(CVPR2016)
 6[置顶] ufldl学习笔记与编程作业：MultiLayer Neural Network(多层神经网络+识别手写体编程)