CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Dec 2013
    Posts
    75

    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.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    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)

  3. #3
    Join Date
    Dec 2013
    Posts
    75

    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.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Why can't my perceptron determine if a point is exactly on the line?

    Quote Originally Posted by TSLexi View Post
    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.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Why can't my perceptron determine if a point is exactly on the line?

    Quote Originally Posted by TSLexi View Post
    Fixed!

    Changed the conditional in feedback () to:

    Code:
    if (sum == 3)
    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

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    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)

  7. #7
    Join Date
    Dec 2013
    Posts
    75

    Re: Why can't my perceptron determine if a point is exactly on the line?

    I actually think it's a algorithm issue.

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Why can't my perceptron determine if a point is exactly on the line?

    Quote Originally Posted by TSLexi View Post
    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);

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Why can't my perceptron determine if a point is exactly on the line?

    Quote Originally Posted by GCDEF View Post
    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)

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Why can't my perceptron determine if a point is exactly on the line?

    Quote Originally Posted by 2kaud View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured