Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of parentheses.
2 views (last 30 days)
Show older comments
Hello guys, this is the code taken from Nuruzzaman Faruki, from this video: https://www.youtube.com/watch?v=WoLBeWclYxo
It gives error down below. There is only 3 .m files, it is very short. Please help me. Exact code worked in his video but doesn't work in mine. I am using r2019b by the way.
This is Sigmoid.m:
function y= Sigmoid(x)
y= 1/(1+exp^(-x));
end
This is SGD_method.m:
function Weight= SGD_method(Weight, input, correct output)
alpha=0.9;
N=4;
end
for k=1:N
transposed_Input= input(k, :)';
d=correct_Output(k);
weighted_Sum=Weight*transposed_Input;
output=Sigmoid(weighted_Sum);
error= d - output;
delta= output* ( 1 - output) * error;
dWeight = alpha * delta * transposed_Input;
Weight(1)= Weight(1)+dWeight(1);
Weight(2)=Weight(2)+dWeight(2);
Weight(3)=Weight(3)+dWeight(3);
end
end
And this is the Training.m:
input=[0 0 1;
0 1 1;
1 0 1;
1 1 1;
];
correct_Output=[0
0
1
1
];
Weight=2*rand(1, 3) - 1;
for epoch = 1:10000
Weight= SGD_method(Weight, input, correct_Output);
end
It gives this error:
>> Training
Error: File: SGD_method.m Line: 5 Column: 1
This statement is not inside any function.
(It follows the END that terminates the definition of the function "SGD_method".)
Error in Training (line 13)
Weight= SGD_method(Weight, input, correct_Output);
1 Comment
Stephen23
on 23 Mar 2020
Your code is badly aligned. Badly aligned code is one way that beginners hide basic bugs and errors in their code (e.g. the cause of the error in your code). You should align your code consistently, it would make it much easier to spot basic errors like this one.
I recommend using the default MATLAB Editor settings. You can also align existing code using the Editor: select the code text, press ctrl+i.
Answers (1)
Stephen23
on 23 Mar 2020
function Weight= SGD_method(...)
alpha=0.9;
N=4;
end % <--------- DELETE THIS LINE!
for k=1:N
...
2 Comments
Steven Lord
on 24 Mar 2020
There's no call to save anywhere in the code you posted, so I would have been surprised had it created a MAT-file.
It sounds a bit like you're trying to learn MATLAB by watching videos on YouTube. If that's the case, might I suggest that you take the free two hour MATLAB Onramp tutorial available from the Tutorials item on the Support section of this website? Click on the link Support at the top of this page then go to Tutorials. The MATLAB Onramp tutorial is an interactive course designed to teach you the fundamentals of working with MATLAB
See Also
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!