LoginSignup
0
0

More than 5 years have passed since last update.

jsdoでtensorflow.js その8

Posted at

概要

jsdoでtensoefllow.jsやってみた。
tensorflow.jsで学習した、ウェイトとバイアスを使って、wemosでsin回帰してみた。

写真

image

サンプルコード


double w1[12] = {0.6203325, -1.027319, 1.8841791, -3.2817535, 1.2214272, 0.9679314, 1.4196726, 7.171823, 1.3714954, 3.7268815, 0.0598045, 1.1944624};
double b1[12] = {1.1656082, -2.0180476, -6.9387307, 3.3653135, -1.7032033, -2.8969588, 0.0565524, -9.5316219, -5.7146306, -2.8060255, -0.3085915, -6.3642092};
double w2[12] = {-1.2655445, -1.0401167, 0.2071615, -0.4695872, -2.6633599, -0.5835096, 1.2490041, 1.2825323, -1.5088843, 0.6767025, 0.0224434, 1.6849496};
double b2 = -0.2928229;
int i;

double tanh(double x) 
{
    if (x > 20) return 1.0;
    else if (x < -20) return -1.0;
    else
    {
        double a = exp(x);
        double b = exp(-x);
        return (a - b) / (a + b);
    }
}

void setup()
{
    Serial.begin(115200);
    i = 0;
}
void loop()
{
  int j;
    i++;
    if (i > 20) i = 0;
    double x = i / 3.0;
    double sum = 0;
    double d[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    for (j = 0; j < 12; j++)
    {
        d[j] += w1[j] * x + b1[j];
    }
    for (j = 0; j < 12; j++)
    {    
        d[j] = tanh(d[j]);
    }
    for (j = 0; j < 12; j++)
    {
        sum += w2[j] * d[j];
    }
    sum += b2;
    sum = tanh(sum);
    Serial.println(sum);
}



以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0