Click here to Skip to main content
15,880,543 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi there
I'm new to programming
I want to implement simple Madaline for xor function in neural network
I used the algorithm(see the link below)
but it does not give me the right response
it should gives right answer after 4 epochs
what's wrong with my code?
thanks.../

this is the link:
algorithm
.....

What I have tried:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace xxxxxxx
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,] x = new int[,] { { 1, 1 }, { 1, -1 }, { -1, 1 } };
            int[] t = new int[] { -1, 1, 1 };
            double[,] w = new double[,] { { 0.05, 0.1 }, { 0.2, 0.2 }, { 0.5, 0.5 } };
            double[] b = new double[] { 0.3, 0.15, 0.5 };
            bool ep = true;
            int x1;
            int x2;
            double zin1;
            double zin2;
            int z1;
            int z2;
            double yin;
            int y;
            while (ep)
            {
                ep = false;
                for (int j = 0; j < 3; j = j + 1)
                {
                    x1 = x[j, 0];
                    x2 = x[j, 1];
                    zin1 = b[0] + (x1 * w[0, 0]) + (x2 * w[1, 0]);
                    zin2 = b[1] + (x1 * w[0, 1]) + (x2 * w[1, 1]);
                    z1 = f(zin1);
                    z2 = f(zin2);
                    yin = b[2] + z1 * w[2, 0] + z2 * w[2, 1];
                    y = f(yin);
                    if (y != t[j])
                    {
                        ep = true;
                        if (t[j] == 1)
                        {
                            if (Math.Abs(zin1) < Math.Abs(zin2))
                            {
                                b[0] = b[0] + 0.5 * (1 - zin1);
                                w[0, 0] = w[0, 0] + 0.5 * (1 - zin1) * x1;
                                w[1, 0] = w[1, 0] + 0.5 * (1 - zin1) * x2;
                            }
                            else
                            {
                                b[1] = b[1] + 0.5 * (1 - zin2);
                                w[0, 1] = w[0, 1] + 0.5 * (1 - zin2) * x1;
                                w[1, 1] = w[1, 1] + 0.5 * (1 - zin2) * x2;
                            }
                        }
                        else if(t[j] == -1)
                        {
                            if (zin1 > 0 && zin2 > 0)
                            {
                                b[0] = b[0] + 0.5 * (-1 - zin1);
                                w[0, 0] = w[0, 0] + 0.5 * (-1 - zin1) * x1;
                                w[1, 0] = w[1, 0] + 0.5 * (-1 - zin1) * x2;
                                b[1] = b[1] + 0.5 * (-1 - zin2);
                                w[0, 1] = w[0, 1] + 0.5 * (-1 - zin2) * x1;
                                w[1, 1] = w[1, 1] + 0.5 * (-1 - zin2) * x2;
                            }
                            else if (zin1 > 0 && zin2 <= 0)
                            {
                                b[0] = b[0] + 0.5 * (-1 - zin1);
                                w[0, 0] = w[0, 0] + 0.5 * (-1 - zin1) * x1;
                                w[1, 0] = w[1, 0] + 0.5 * (-1 - zin1) * x2;
                            }
                            else if (zin1 < 0 && zin2 > 0)
                            {
                                b[1] = b[1] + 0.5 * (-1 - zin2);
                                w[0, 1] = w[0, 1] + 0.5 * (-1 - zin2) * x1;
                                w[1, 1] = w[1, 1] + 0.5 * (-1 - zin2) * x2;
                            }
                        }
                    }
                }
            }

            Console.WriteLine("b1:{0} , b2:{1} , b3:{2} \n w00:{3} , w10:{4} , w01:{5}, w11:{6}", b[0], b[1], b[2], w[0, 0], w[1, 0], w[0, 1], w[1, 1]);

            int[,] test = new int[,] { { -1, -1 } };
            x1 = test[0, 0];
            x2 = test[0, 1];
            zin1 = b[0] + (x1 * w[0, 0]) + (x2 * w[1, 0]);
            zin2 = b[1] + (x1 * w[0, 1]) + (x2 * w[1, 1]);
            z1 = f(zin1);
            z2 = f(zin2);
            yin = b[2] + z1 * w[2, 0] + z2 * w[2, 1];
            y = f(yin);
            if (y == 1)
            {
                Console.WriteLine("1");
            }
            else
            {
                Console.WriteLine("-1");
            }
            Console.ReadLine();
        }

  private static int f(double zin)
        {
            return (zin >= 0) ? 1 : -1;
        }
    }
}
Posted
Updated 17-Jul-17 8:25am
v8
Comments
BillWoodruff 17-Jul-17 1:02am    
Set break-points, single-step through the code observing what happens and what varies from your expectations, and you need to understand what the algorithm does in dertail.

Since you reposted the question, I'll repost the solution...


Several things:
1) Don't use single character variable names: they are easy for you to type, but impossible to read and work out what is going on. Use names that describe what the variables do, particularly if you want some other poor sod to try and read it.

2) If you don't do (1) above, document your code. In fact, document your code anyway. I have absolutely no idea looking at that what the code is supposed to do, much less what it does or doesn't do that you consider "wrong" or how I should test it to find out.

3) Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
private int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on your line:
C#
while (pp < 15)

and run your app. Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?

This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
 
Share this answer
 
Quote:
but it does not give me the right response

You forgot to tell us that results you expect and what results you actually get.
Without these informations, it is impossible to give you real answers.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
Debugging C# Code in Visual Studio - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900