ｃ＃でxor

#概要
c#でxorやってみた。


#サンプルコード


```
using System;

public class Hello
{
    static Random random = new Random();
    static int rand()
    {
        return random.Next();
    }
    static double sigmoid(double a)
    {
        return (1.0 / (1.0 + Math.Exp(-(a))));
    }
    public static void Main()
    {
        int[] x = new int[3];
        int[] t = new int[3];
        double[] g = new double[3];
        double[] z = new double[3];
        double[,] h = new double[3, 3];
        double[,] w = new double[3, 3];
	    double alpha = 0.06;
	    for (int j = 0; j <= 2; j++)
	    {
		    for (int i = 0; i <= 2; i++)
		    {
			    w[j, i] = (double) ((rand() % 2000) - 1000) / 1000;
		    }
	    }
	    for (int j = 0; j <= 2; j++)
	    {
		    for (int i = 0; i <= 2; i++)
		    {
			    h[j, i] = (double) ((rand() % 2000) - 1000) / 1000;
		    }
	    }
    	for (int count = 0; count < 70001; count++)
    	{
	    	x[0] = 1;
	    	x[1] = rand() % 2;
    		x[2] = rand() % 2;
    		t[0] = (x[1] ^ x[2]);
    		for (int i = 0; i <= 2; i++)
    		{
	    		double sum = 0;
	    		for (int j = 0; j <= 2; j++)
	    		{
	    			sum += w[j, i] * x[j];
	    		}
	    		g[i] = sigmoid(sum);
	    	}
	    	for (int i = 0; i < 1; i++)
	    	{
	    		double sum = 0;
	    		for (int j = 0; j <= 2; j++)
	    		{
	    			sum += h[j, i] * g[j];
	    		}
	    		z[i] = sigmoid(sum);
	    	}
	    	alpha = 0.06;
	    	for (int j = 0; j < 1; j++)
	    	{
	    		for (int i = 0; i <= 2; i++)
	    		{
	    			h[i, j] += alpha * g[i] * (t[j] - z[j]) * z[j] * (1.0 - z[j]);
	    		}
	    	}
	    	for (int k = 0; k < 1; k++)
	    	{
	    		for (int j = 0; j <= 2; j++)
	    		{
	    			double dj = (t[k] - z[k]) * z[k] * (1.0 - z[k]) * h[j, k] * g[j] * (1.0 - g[j]);
	    			for (int i = 0; i <= 2; i++)
	    			{
	    				w[i, j] += alpha * x[i] * dj;
		    		}
		    	}
		    }
		    double error = 0;
		    for (int i = 0; i < 1; i++)
		    {
			    error += (t[i] - z[i]) * (t[i] - z[i]);
		    }
		    if ((count % 10000) != 0) continue;
		    Console.WriteLine("epoc : {0} loss : {1}", count, error);
		    Console.WriteLine("{0} {1} {2}", x[1], x[2], t[0]);
	    }
	    Console.Write("0,0 = ");
	    x[1] = 0;
	    x[2] = 0;
	    for (int i = 0; i <= 2; i++)
    	{
	    	double sum = 0;
    		for (int j = 0; j <= 2; j++)
    		{
    			sum += w[j, i] * x[j];
    		}
    		g[i] = sigmoid(sum);
    	}
    	for (int i = 0; i <= 2; i++)
    	{
    		double sum = 0;
    		for (int j = 0; j <= 2; j++)
    		{
	    		sum += h[j, i] * g[j];
	    	}
	    	z[i] = sigmoid(sum);
	    }
    	Console.WriteLine("{0}", z[0]);
    	Console.Write("0,1 = ");
    	x[1] = 0;
	    x[2] = 1;
	    for (int i = 0; i <= 2; i++)
    	{
    		double sum = 0;
    		for (int j = 0; j <= 2; j++)
    		{
    			sum += w[j, i] * x[j];
    		}
	    	g[i] = sigmoid(sum);
    	}
    	for (int i = 0; i <= 2; i++)
	    {
		    double sum = 0;
	    	for (int j = 0; j <= 2; j++)
    		{
    			sum += h[j, i] * g[j];
    		}
	    	z[i] = sigmoid(sum);
	    }
    	Console.WriteLine("{0}", z[0]);
    	Console.Write("1,0 = ");
    	x[1] = 1;
    	x[2] = 0;
    	for (int i = 0; i <= 2; i++)
    	{
    		double sum = 0;
    		for (int j = 0; j <= 2; j++)
    		{
    			sum += w[j, i] * x[j];
	    	}
    		g[i] = sigmoid(sum);
	    }
	    for (int i = 0; i <= 2; i++)
    	{
    		double sum = 0;
    		for (int j = 0; j <= 2; j++)
    		{
    			sum += h[j, i] * g[j];
    		}
    		z[i] = sigmoid(sum);
    	}
    	Console.WriteLine("{0}", z[0]);
    	Console.Write("1,1 = ");
    	x[1] = 1;
    	x[2] = 1;
    	for (int i = 0; i <= 2; i++)
	    {
		    double sum = 0;
		    for (int j = 0; j <= 2; j++)
	    	{
	    		sum += w[j, i] * x[j];
		    }
	    	g[i] = sigmoid(sum);
	    }
	    for (int i = 0; i <= 2; i++)
	    {
		    double sum = 0;
		    for (int j = 0; j <= 2; j++)
		    {
			    sum += h[j, i] * g[j];
	    	}
		    z[i] = sigmoid(sum);
	    }
	    Console.WriteLine("{0}", z[0]);
        System.Console.WriteLine("Hello C#");
    }
}

```

#成果物

https://paiza.io/projects/Fa_VHwVV88z7npbmhabIug

以上。