wemosd1에서 xor

4416 단어 XORWemos
개요
wemosd1에서 xor문제를 시험해 봤어요.
softwdt reset에 걸려있어서 delay(1)를 넣었습니다.
사진.

샘플 코드
#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);
}


이상.

좋은 웹페이지 즐겨찾기