wemos d1でxor

#概要
wemos d1でxor問題やってみた。
soft wdt resetに、引っかかるので、delay(1)入れた。


#写真

![xor.PNG](https://qiita-image-store.s3.amazonaws.com/0/18104/23ebc59b-dd40-cb7e-88cf-9e1e14f74a60.png)

#サンプルコード

```
#include <math.h>

int x[3];
int t[3];
double g[3];
double z[3];
double h[3][3];
double w[3][3];
#define sigmoid(a)	  		(1.0 / (1.0 + exp(-(a))))
void train()
{
	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]);
		}
       delay(1);
		if ((count % 10000) != 0) continue;
		Serial.print(count);
		Serial.println(error);
	}
	Serial.print("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);
	}
	Serial.println(z[0]);
	Serial.print("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);
	}
	Serial.println(z[0]);
	Serial.print("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);
	}
	Serial.println(z[0]);
	Serial.print("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);
	}
	Serial.println(z[0]);
}

void setup()
{
	Serial.begin(115200);
	while (!Serial) delay(250);
	Serial.print("ok");
	Serial.println();
	train();
}
void loop()
{
	delay(5000);
}


```

以上。