Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I wrote following code but while loop has not any effect on w[i,j]
what's wrong with this?
help me please
thanks

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;
            int pp = 0;
            while (pp < 15)
            {
                
                //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]);
                    if (zin1 >= 0)
                    {
                        z1 = 1;
                    }
                    else
                    {
                        z1 = -1;
                    }
                    if (zin2 >= 0)
                    {
                        z2 = 1;
                    }
                    else
                    {
                        z2 = -1;
                    }
                    yin = b[2] + z1 * w[2, 0] + z2 * w[2, 1];
                    if (yin >= 0)
                    {
                        y = 1;
                    }
                    else
                    {
                        y = -1;
                    }
                    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;
                            }
                        }
                    }
                }
                pp = pp + 1;   
            }
            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]);
            if (zin1 >= 0)
                z1 = 1;
            else
            z1 = -1;
            if (zin2 >= 0)
                z2 = 1;
            else
            z2 = -1;

            yin = b[2] + w[2, 0] * z1 + w[2, 1] * z2;

            if (yin >= 0)
                y = 1;
            else
            y = -1;

            if (y == 1)
            {
                Console.WriteLine("1");
            }
            else
            {
                Console.WriteLine("-1");
            }
            Console.ReadLine();
        }

  //private static void f(double zin, int y)
  //      {
  //          if (zin >= 0)
  //              y = 1;
  //          else
  //              y = -1;
  //      }
    }
}
Posted
Updated 17-Jul-17 8:43am
Comments
W Balboos, GHB 17-Jul-17 14:03pm    
I strongly suggest you step through your code with a debugger and see if it even tries to do anything to w[i,j]. If not, why not.

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
 
Looks like a repost of What's wrong with my NN code?[^]
Quote:
I wrote following code but while loop has not any effect on w[i,j]
what's wrong with this?

The debugger will show you what your code is doing and values of variables.

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
 

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