-
January 26th, 2014, 07:17 AM
#1
Why can't my perceptron determine if a point is exactly on the line?
When I use my perceptron, it classifies everything appropriately, except it always says points that are exactly on the line are above it. Any ideas as to why?
Thanks.
-
January 26th, 2014, 08:07 AM
#2
Re: Why can't my perceptron determine if a point is exactly on the line?
Code:
float error = desired - guess;
You are assuming that this will give 0 if desired is the same as guess. There is often an issue with floating point numbers doing comparison due to the way these numbers are stored internally. Two numbers that display the same may not be stored internally exactly the same so subtracting them won't give an absolute zero.
Consider something like
Code:
float error = (abs(desired - guess) < 0.0001) ? 0.0 : desired - guess;
Where the 'magic number' 0.0001 should be chosen to be appropriate.
See
http://www.cygnus-software.com/paper...ringfloats.htm
http://stackoverflow.com/questions/1...ble-comparison
http://stackoverflow.com/questions/4...compare-floats
Last edited by 2kaud; January 26th, 2014 at 08:26 AM.
Reason: References added
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 26th, 2014, 09:26 AM
#3
Re: Why can't my perceptron determine if a point is exactly on the line?
Fixed!
Changed the conditional in feedback () to:
Code:
if (sum == 3)
return 0;
else if (sum < 3 && sum > 0)
return 1;
else
return 0;
Now it gets that (2,5) is on the line, and (2,4) are below the line, but it says any point with a y-value above 5 is on the line.
-
January 26th, 2014, 09:31 AM
#4
Re: Why can't my perceptron determine if a point is exactly on the line?
Originally Posted by TSLexi
When I use my perceptron, it classifies everything appropriately, except it always says points that are exactly on the line are above it. Any ideas as to why?
http://www.parashift.com/c++-faq/floating-pt-errs.html
Unless the floating point number is a sum of inverse powers of 2, there is no exact binary representation of a decimal fractional amount. As the link states, the issue you're facing has to do with numerical analysis and computer science, not programming. If you used Java, C#, C, Basic, Delphi/Pascal, etc. you will find the same problem with floating point numbers.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; January 26th, 2014 at 09:39 AM.
-
January 26th, 2014, 09:35 AM
#5
Re: Why can't my perceptron determine if a point is exactly on the line?
Originally Posted by TSLexi
Fixed!
Changed the conditional in feedback () to:
What is "sum"? Is it a floating point variable? If so, if the value of "sum" is computed, then there is no guarantee that sum will be exactly 3.
but it says any point with a y-value above 5 is on the line.
I think it's time to use the debugger. Learning to debug your programs is a mandatory step in learning how to program.
Regards,
Paul McKenzie
-
January 26th, 2014, 09:56 AM
#6
Re: Why can't my perceptron determine if a point is exactly on the line?
sum is type float - so shouldn't really be compared directly with 3 (see earlier posts from myself and Paul). However, looking at the code, it seems to return 1 only if sum is < 3 and > 0. This can be re-coded as
Code:
return (sum > 0.0 && sum < 3.0);
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 26th, 2014, 12:44 PM
#7
Re: Why can't my perceptron determine if a point is exactly on the line?
I actually think it's a algorithm issue.
-
January 26th, 2014, 01:51 PM
#8
Re: Why can't my perceptron determine if a point is exactly on the line?
Originally Posted by TSLexi
Fixed!
Changed the conditional in feedback () to:
Code:
if (sum == 3)
return 0;
else if (sum < 3 && sum > 0)
return 1;
else
return 0;
Now it gets that (2,5) is on the line, and (2,4) are below the line, but it says any point with a y-value above 5 is on the line.
Not that it matters much, but that's more complicated than it needs to be. This line does the same thing
return (sum > 0 && sum < 3);
-
January 26th, 2014, 02:24 PM
#9
Re: Why can't my perceptron determine if a point is exactly on the line?
Originally Posted by GCDEF
Not that it matters much, but that's more complicated than it needs to be. This line does the same thing
return (sum > 0 && sum < 3);
See my post #6
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 26th, 2014, 03:03 PM
#10
Re: Why can't my perceptron determine if a point is exactly on the line?
Originally Posted by 2kaud
See my post #6
Oops.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|